diff --git a/src/client/spark_core/TestRunner/InProcessTestRunner.cs b/src/client/spark_core/TestRunner/InProcessTestRunner.cs index c64a580f5..9abf0f187 100644 --- a/src/client/spark_core/TestRunner/InProcessTestRunner.cs +++ b/src/client/spark_core/TestRunner/InProcessTestRunner.cs @@ -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 report) { + public static void run_test_synchronously(string v, TestEnumerator enumerator, Action 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 report) { + public static void run_test_synchronously(TestMethod test, Action report) { var result = new TestResult() { TestName = test.full_name, Status = TestResult.Types.Step.Setup, diff --git a/src/client/spark_core/TestRunner/TestAttributes.cs b/src/client/spark_core/TestRunner/TestAttributes.cs index 67db1a889..67728d29a 100644 --- a/src/client/spark_core/TestRunner/TestAttributes.cs +++ b/src/client/spark_core/TestRunner/TestAttributes.cs @@ -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 { diff --git a/src/client/spark_core/TestRunner/TestEnumerator.cs b/src/client/spark_core/TestRunner/TestEnumerator.cs index 2946b7ea0..36a402a03 100644 --- a/src/client/spark_core/TestRunner/TestEnumerator.cs +++ b/src/client/spark_core/TestRunner/TestEnumerator.cs @@ -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() != null) .Select(t => - new TestClass(t, + new TestClass(t, + t.GetCustomAttribute().TestType, t.GetMethods() .Where(m => m.GetCustomAttribute() != null) .Select(m => new TestMethod(t, m)) .ToList())) - .ToList(); + .ToList(); } public IReadOnlyCollection TestGroups { get; } public IEnumerable Tests => TestGroups.SelectMany(t => t.methods); + public IEnumerable get_of_type(TestType tt) { + return TestGroups.Where(wt => wt.test_type == tt).SelectMany(t => t.methods); + } + + public IEnumerable 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 methods) { + public TestClass(Type type, TestType test_type, IEnumerable 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 methods { get; } public override string ToString() { return $"[TestGroup:{name}]"; diff --git a/src/client/spark_test/System/LimitsTests.cs b/src/client/spark_test/System/LimitsTests.cs new file mode 100644 index 000000000..a37f6bce1 --- /dev/null +++ b/src/client/spark_test/System/LimitsTests.cs @@ -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(); + _admin_service = _context.locate(); + + 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 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 under_limit_order_result = await _order_service.create_order_async(new Create() { + NewOrder = order_def, + Price = 5, + Quantity = test_limit_value + }); + + Result 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 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 get_account_data (AdminService admin_service, string account_name) + { + Update 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 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 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 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 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 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 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 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 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; + } + } + + + } + + + +} diff --git a/src/client/sparkcmd/Cmd/TestCommand.cs b/src/client/sparkcmd/Cmd/TestCommand.cs index 43bddadac..24343c289 100644 --- a/src/client/sparkcmd/Cmd/TestCommand.cs +++ b/src/client/sparkcmd/Cmd/TestCommand.cs @@ -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("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)); } }