Merged in SPARK-1877-system-tests (pull request #147)
RTM SPARK-1877 system tests Approved-by: Eric Harding <eric.harding@bluetradesys.com>
This commit is contained in:
commit
daa9800af2
5 files changed
+277
-10
No files matched your search
@@ -9,12 +9,12 @@ using System.Threading.Tasks;
|
||||
namespace bts.test {
|
||||
public static class InProcessTestRunner {
|
||||
|
||||
public static void run_test_syncronously(string v, TestEnumerator enumerator, Action<TestResult> report) {
|
||||
public static void run_test_synchronously(string v, TestEnumerator enumerator, Action<TestResult> report) {
|
||||
foreach (var t in enumerator.find(v)) {
|
||||
run_test_syncronously(t, report);
|
||||
run_test_synchronously(t, report);
|
||||
}
|
||||
}
|
||||
public static void run_test_syncronously(TestMethod test, Action<TestResult> report) {
|
||||
public static void run_test_synchronously(TestMethod test, Action<TestResult> report) {
|
||||
var result = new TestResult() {
|
||||
TestName = test.full_name,
|
||||
Status = TestResult.Types.Step.Setup,
|
||||
|
||||
@@ -2,8 +2,16 @@
|
||||
|
||||
namespace bts.test {
|
||||
|
||||
public enum TestType {
|
||||
Unit, System, Shell
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class TestGroupAttribute : Attribute {
|
||||
public TestGroupAttribute() {
|
||||
this.TestType = TestType.Unit;
|
||||
}
|
||||
public TestType TestType;
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class TestAttribute : Attribute {
|
||||
|
||||
@@ -7,23 +7,29 @@ namespace bts.test {
|
||||
|
||||
public class TestEnumerator {
|
||||
public TestEnumerator(params string[] assemblies) {
|
||||
TestGroups =
|
||||
TestGroups =
|
||||
assemblies
|
||||
.Select(Assembly.Load)
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(t => t.GetCustomAttribute<TestGroupAttribute>() != null)
|
||||
.Select(t =>
|
||||
new TestClass(t,
|
||||
new TestClass(t,
|
||||
t.GetCustomAttribute<TestGroupAttribute>().TestType,
|
||||
t.GetMethods()
|
||||
.Where(m => m.GetCustomAttribute<TestAttribute>() != null)
|
||||
.Select(m => new TestMethod(t, m))
|
||||
.ToList()))
|
||||
.ToList();
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<TestClass> TestGroups { get; }
|
||||
public IEnumerable<TestMethod> Tests => TestGroups.SelectMany(t => t.methods);
|
||||
|
||||
public IEnumerable<TestMethod> get_of_type(TestType tt) {
|
||||
return TestGroups.Where(wt => wt.test_type == tt).SelectMany(t => t.methods);
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<TestMethod> find(string name) {
|
||||
foreach (var g in TestGroups) {
|
||||
foreach (var t in g.methods) {
|
||||
@@ -41,13 +47,15 @@ namespace bts.test {
|
||||
|
||||
|
||||
public class TestClass {
|
||||
public TestClass(Type type, IEnumerable<TestMethod> methods) {
|
||||
public TestClass(Type type, TestType test_type, IEnumerable<TestMethod> methods) {
|
||||
this.type = type;
|
||||
this.test_type = test_type;
|
||||
this.methods = methods;
|
||||
}
|
||||
public string name => type.Name;
|
||||
public Assembly assembly => type.Assembly;
|
||||
public Type type { get; }
|
||||
public TestType test_type { get; }
|
||||
public IEnumerable<TestMethod> methods { get; }
|
||||
public override string ToString() {
|
||||
return $"[TestGroup:{name}]";
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using bts.api;
|
||||
using bts.api.admin;
|
||||
using bts.api.core;
|
||||
using bts.api.test;
|
||||
using bts.test;
|
||||
using bts.api.orderman;
|
||||
using mbts;
|
||||
using mbts.tests;
|
||||
|
||||
namespace bts.tests
|
||||
{
|
||||
[TestGroup(TestType = TestType.System)]
|
||||
public class LimitsTests : IDisposable {
|
||||
ServiceContext _context;
|
||||
OrderService _order_service;
|
||||
AdminService _admin_service;
|
||||
public LimitsTests (){
|
||||
_context = new ServiceContext();
|
||||
|
||||
_order_service = _context.locate<OrderService>();
|
||||
_admin_service = _context.locate<AdminService>();
|
||||
|
||||
var auth = _context.login("bts","");
|
||||
auth.Wait(3000);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Disposable.dispose(ref _order_service);
|
||||
Disposable.dispose(ref _admin_service);
|
||||
Disposable.dispose(ref _context);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task test_clip_limits() {
|
||||
//Get and set limit values from the admin service
|
||||
|
||||
uint test_limit_value = 10u;
|
||||
string test_symbol = "CL";
|
||||
|
||||
var test_user = (name: "bts", id: 1u);
|
||||
var test_account = (name: "ACC A", id: 1u);
|
||||
|
||||
var account_data = await AdminUtilities.get_account_data(_admin_service, test_account.name);
|
||||
Assert.IsTrue(account_data.HasValue);
|
||||
|
||||
var old_clip_limit = AdminUtilities.get_clip_limit(account_data, test_symbol);
|
||||
|
||||
//create Orders Here
|
||||
|
||||
bts.md.MarketDataClient mdc = new bts.md.MarketDataClient();
|
||||
|
||||
SecurityImmutable? sec = await mdc.find_security_async(y => y.Symbol == test_symbol && y.Exchange == Venue.Sim);
|
||||
|
||||
uint under_limit_order_key;
|
||||
uint over_limit_order_key;
|
||||
uint no_limit_order_key;
|
||||
|
||||
var order_def = create_order_def(sec.Value.Id, Side.Buy, test_account, test_user);
|
||||
|
||||
Assert.IsTrue(sec.HasValue, "Unable to create order without security");
|
||||
await AdminUtilities.remove_clip_limit(_admin_service, test_account.name, test_symbol);
|
||||
Result<Response> no_limit_order_result = await _order_service.create_order_async(new Create() {
|
||||
NewOrder = order_def,
|
||||
Price = 5,
|
||||
Quantity = test_limit_value
|
||||
});
|
||||
|
||||
var set_success = await AdminUtilities.set_clip_limit(_admin_service, test_limit_value, account_data, test_symbol);
|
||||
Assert.IsTrue(set_success, "Failed to set limit");
|
||||
|
||||
Result<Response> under_limit_order_result = await _order_service.create_order_async(new Create() {
|
||||
NewOrder = order_def,
|
||||
Price = 5,
|
||||
Quantity = test_limit_value
|
||||
});
|
||||
|
||||
Result<Response> over_limit_order_result = await _order_service.create_order_async(new Create() {
|
||||
NewOrder = order_def,
|
||||
Price = 5,
|
||||
Quantity = test_limit_value + 1
|
||||
});
|
||||
|
||||
under_limit_order_key = under_limit_order_result.value.OrderKey;
|
||||
over_limit_order_key = over_limit_order_result.value.OrderKey;
|
||||
no_limit_order_key = no_limit_order_result.value.OrderKey;
|
||||
|
||||
var orders = _order_service.subscribei_orders().Subscribe(ords =>
|
||||
{
|
||||
IEnumerable<OrderRecordImmutable> filtered_orders = ords.all.Where(x => x.Key == under_limit_order_key || x.Key == over_limit_order_key || x.Key == no_limit_order_key);
|
||||
|
||||
foreach(var o in filtered_orders)
|
||||
{
|
||||
if(o.Key == under_limit_order_key) {
|
||||
Assert.IsTrue(o.State.Status == OrderStatus.Working, "Under Limit Order is not 'Working'");
|
||||
}
|
||||
else if(o.Key == over_limit_order_key) {
|
||||
Assert.IsTrue(o.State.Status == OrderStatus.RejectedBySpark, "Over Limit Order is not 'RejectedBySpark'");
|
||||
}
|
||||
else if(o.Key == no_limit_order_key) {
|
||||
Assert.IsTrue(o.State.Status == OrderStatus.RejectedBySpark, "No Limit Order is not 'RejectedBySpark'");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Restore the Limits to the original values
|
||||
bool success = false;
|
||||
|
||||
if(old_clip_limit.HasValue) {
|
||||
success = await AdminUtilities.set_clip_limit(_admin_service, old_clip_limit.Value, test_account.name, test_symbol);
|
||||
}
|
||||
else {
|
||||
success = await AdminUtilities.remove_clip_limit(_admin_service, test_account.name, test_symbol);
|
||||
}
|
||||
|
||||
Assert.IsTrue(success, "Failed to restore Limit");
|
||||
|
||||
|
||||
}
|
||||
|
||||
public OrderDefinition create_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user){
|
||||
return new OrderDefinition {
|
||||
SecurityKey = security_id,
|
||||
Side = Side.Buy,
|
||||
TimeInForce = TimeInForce.Day,
|
||||
CustomDef = new CustomOrderDefinition { Limit = new LimitOrderDefinition { } },
|
||||
CtiCode = CTICode.CtiCode1,
|
||||
CustomerOrFirm = CustomerOrFirm.Firm,
|
||||
ManualOrderIndicator = ManualOrderIndicator.Yes,
|
||||
Account = account.name,
|
||||
AccountId = account.id,
|
||||
User = user.name,
|
||||
UserId = user.id
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class AdminUtilities {
|
||||
|
||||
public static async Task<BTSAccountImmutable?> get_account_data (AdminService admin_service, string account_name)
|
||||
{
|
||||
Update<AdminDataImmutable, int> update = await admin_service.subscribei_admin_data().FirstAsync();
|
||||
|
||||
foreach (var acc in update.all.First().Accounts) {
|
||||
if(acc.Name == account_name) {
|
||||
return acc;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static AccountSecurityClipLimitUnitImmutable? get_clip_limit_row(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
IEnumerable<AccountSecurityClipLimitUnitImmutable> clip_limit_rows = account_data.Value.SecurityClipLimits.Limit.Where(
|
||||
r => r.Security.SecuritySymbol == symbol
|
||||
&& r.Security.Exchange == venue
|
||||
&& r.Security.SecurityType == security_type);
|
||||
return clip_limit_rows.Any() ? clip_limit_rows.First() : (AccountSecurityClipLimitUnitImmutable?) null;
|
||||
}
|
||||
|
||||
public static uint? get_clip_limit(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
var row = get_clip_limit_row(account_data,symbol,security_type,venue);
|
||||
return row.HasValue ? row.Value.ClipSize : (uint?) null;
|
||||
}
|
||||
|
||||
public static async Task<uint?> get_clip_limit(AdminService admin_service, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
var account_data = await get_account_data(admin_service, account_name);
|
||||
return get_clip_limit(account_data, symbol, security_type, venue);
|
||||
}
|
||||
|
||||
public static async Task<bool> set_clip_limit(AdminService admin_service, uint clip_value, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
var account_data = await get_account_data(admin_service, account_name);
|
||||
return await set_clip_limit(admin_service, clip_value,account_data, symbol,security_type, venue);
|
||||
}
|
||||
|
||||
public static async Task<bool> set_clip_limit(AdminService admin_service, uint clip_value, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
|
||||
AccountSecurityClipLimitUnitImmutable? origin_clip_limit_row = get_clip_limit_row(account_data,symbol,security_type,venue);
|
||||
AccountSecurityClipLimitUnitImmutable new_clip_limit_row;
|
||||
|
||||
List<AccountSecurityClipLimitUnitImmutable> clip_limits_list = account_data.Value.SecurityClipLimits.Limit.ToList();
|
||||
|
||||
if(origin_clip_limit_row.HasValue) {
|
||||
clip_limits_list.Remove(origin_clip_limit_row.Value);
|
||||
}
|
||||
|
||||
new_clip_limit_row = (AccountSecurityClipLimitUnitImmutable) new AccountSecurityClipLimitUnit {
|
||||
Security = new SecurityScope(){ SecuritySymbol = symbol, Exchange = venue, SecurityType = security_type },
|
||||
ClipSize = clip_value
|
||||
};
|
||||
|
||||
clip_limits_list.Add(new_clip_limit_row);
|
||||
|
||||
AccountSecurityClipLimits temp_scl = new AccountSecurityClipLimits();
|
||||
temp_scl.Limit.AddRange(clip_limits_list.Select(x => x.CloneToMutable()));
|
||||
|
||||
var new_account_data = account_data.Value.with(security_clip_limits: temp_scl);
|
||||
|
||||
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
|
||||
CompleteData = new_account_data.CloneToMutable()
|
||||
});
|
||||
|
||||
return update_ret.success;
|
||||
}
|
||||
|
||||
public static async Task<bool> remove_clip_limit(AdminService admin_service, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
var account_data = await get_account_data(admin_service, account_name);
|
||||
return await remove_clip_limit(admin_service, account_data, symbol, security_type, venue);
|
||||
}
|
||||
|
||||
public static async Task<bool> remove_clip_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
|
||||
|
||||
AccountSecurityClipLimitUnitImmutable? origin_clip_limit_row = get_clip_limit_row(account_data,symbol,security_type,venue);
|
||||
List<AccountSecurityClipLimitUnitImmutable> clip_limits_list = account_data.Value.SecurityClipLimits.Limit.ToList();
|
||||
|
||||
if(origin_clip_limit_row.HasValue) {
|
||||
clip_limits_list.Remove(origin_clip_limit_row.Value);
|
||||
AccountSecurityClipLimits temp_scl = new AccountSecurityClipLimits();
|
||||
temp_scl.Limit.AddRange(clip_limits_list.Select(x => x.CloneToMutable()));
|
||||
var new_account_data = account_data.Value.with(security_clip_limits: temp_scl);
|
||||
|
||||
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
|
||||
CompleteData = new_account_data.CloneToMutable()
|
||||
});
|
||||
|
||||
return update_ret.success;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -15,12 +15,17 @@ namespace sparkcmd {
|
||||
uint _parallel = 1;
|
||||
bool _list;
|
||||
|
||||
TestType test_type = TestType.Unit;
|
||||
|
||||
public TestCommand() {
|
||||
IsCommand("test", "Run managed unit tests");
|
||||
// i comes thorugh as "t" which seems daft.
|
||||
// i comes thorugh as "t" which seems daft.
|
||||
HasOption("t|timeout", "Timeout unit tests", i => _timeout = 10);
|
||||
HasOption<uint>("n|parallel", "How many tests to run in parallel", i => _parallel = i);
|
||||
HasOption("l|list", "List all tests", _ => _list = true);
|
||||
HasOption("unit","Run unit tests", _ => test_type = TestType.Unit);
|
||||
HasOption("system","Run system tests", _ => test_type = TestType.System);
|
||||
HasOption("shell","Run shell tests", _ => test_type = TestType.Shell);
|
||||
HasAdditionalArguments(null, " [Test List]");
|
||||
}
|
||||
|
||||
@@ -35,7 +40,7 @@ namespace sparkcmd {
|
||||
if (_timeout == 0 && tests.Length == 1) {
|
||||
//System.Threading.Thread.Sleep(5000);
|
||||
using (var trr = new TestResultReceiver(true)) {
|
||||
InProcessTestRunner.run_test_syncronously(tests[0], Enumerator, trr.get_reporter());
|
||||
InProcessTestRunner.run_test_synchronously(tests[0], Enumerator, trr.get_reporter());
|
||||
trr.print_summary();
|
||||
return trr.ErrorCount;
|
||||
}
|
||||
@@ -44,7 +49,7 @@ namespace sparkcmd {
|
||||
return run_tests(Enumerator.match(tests).Select(t => t.name));
|
||||
}
|
||||
else {
|
||||
return run_tests(Enumerator.Tests.Select(t => t.full_name));
|
||||
return run_tests(Enumerator.get_of_type(test_type).Select(t => t.full_name));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user