public void Filter() { using (var scope = TestScope.Create()) { scope.Resolve <ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestQueryDataStructureCommand.Source;" }); scope.Resolve <ISqlExecuter>().ExecuteSql(new[] { "a1", "b1", "b2", "c1" } .Select(name => "INSERT INTO TestQueryDataStructureCommand.Source (Name) SELECT N'" + name + "';")); var info = new ReadCommandInfo { DataSource = "TestQueryDataStructureCommand.Source", ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("a1, b1, b2, c1 /4", ReportCommandResult2(scope, info, true)); info.Filters = new[] { new FilterCriteria(new TestQueryDataStructureCommand.FilterByPrefix { Prefix = "b" }) }; Assert.AreEqual("b1, b2 /2", ReportCommandResult2(scope, info, true)); info.OrderByProperties = new[] { new OrderByProperty { Property = "Name", Descending = true } }; Assert.AreEqual("b2, b1 /2", ReportCommandResult2(scope, info)); info.Top = 1; Assert.AreEqual("b2 /2", ReportCommandResult2(scope, info)); info.Filters = info.Filters.Concat(new[] { new FilterCriteria { Property = "Name", Operation = "Contains", Value = "1" } }).ToArray(); Assert.AreEqual("b1 /1", ReportCommandResult2(scope, info)); } }
//================================================================ #region Sorting and paging public static IQueryable <T> SortAndPaginate <T>(IQueryable <T> query, ReadCommandInfo commandInfo) { bool pagingIsUsed = commandInfo.Top > 0 || commandInfo.Skip > 0; if (pagingIsUsed && (commandInfo.OrderByProperties == null || commandInfo.OrderByProperties.Length == 0)) { throw new ClientException("Invalid ReadCommand argument: Sort order must be set if paging is used (Top or Skip)."); } if (commandInfo.OrderByProperties != null) { foreach (var order in commandInfo.OrderByProperties) { query = Sort(query, order.Property, ascending: !order.Descending); } } Type itemType = query.GetType().GetInterface("IQueryable`1").GetGenericArguments().Single(); if (commandInfo.Skip > 0) { // "query.Skip(commandInfo.Skip)" would convert the result IQueryable<T> and not use the actual queryable generic type. var skipMethod = typeof(Queryable).GetMethod("Skip").MakeGenericMethod(itemType); query = (IQueryable <T>)skipMethod.InvokeEx(null, query, commandInfo.Skip); } if (commandInfo.Top > 0) { // "query.Take(commandInfo.Top)" would convert the result IQueryable<T> and not use the actual queryable generic type. var takeMethod = typeof(Queryable).GetMethod("Take").MakeGenericMethod(itemType); query = (IQueryable <T>)takeMethod.InvokeEx(null, query, commandInfo.Top); } return(query); }
public void NullGenericFilter() { using (var scope = TestScope.Create()) { var genericRepos = scope.Resolve <GenericRepositories>().GetGenericRepository("Common.Claim"); var readCommand = new ReadCommandInfo { DataSource = "Common.Claim", Top = 3, OrderByProperties = new[] { new OrderByProperty { Property = "ClaimResource" } }, ReadRecords = true, ReadTotalCount = true, }; var serverCommandsUtility = scope.Resolve <ServerCommandsUtility>(); var readResult = serverCommandsUtility.ExecuteReadCommand(readCommand, genericRepos); Console.WriteLine("Records.Length: " + readResult.Records.Length); Console.WriteLine("TotalCount: " + readResult.TotalCount); Assert.IsTrue(readResult.Records.Length < readResult.TotalCount); } }
private static ReadCommandResult ExecuteCommand(ReadCommandInfo commandInfo, RhetosTestContainer container) { var commands = container.Resolve <IIndex <Type, IEnumerable <ICommandImplementation> > >(); var readCommand = (ReadCommand)commands[typeof(ReadCommandInfo)].Single(); return((ReadCommandResult)readCommand.Execute(commandInfo).Data.Value); }
private static ReadCommandResult ExecuteCommand(ReadCommandInfo commandInfo, UnitOfWorkScope scope) { var commands = scope.Resolve <IIndex <Type, IEnumerable <ICommandImplementation> > >(); var readCommand = (ReadCommand)commands[typeof(ReadCommandInfo)].Single(); return((ReadCommandResult)readCommand.Execute(commandInfo).Data.Value); }
public void ReadCommand() { var entityRepos = new ImplicitReadCommandRepository(); var genericRepos = NewRepos(entityRepos); var command = new ReadCommandInfo { Filters = new[] { new FilterCriteria { Property = "Name", Operation = "StartsWith", Value = "b" } }, OrderByProperties = new[] { new OrderByProperty { Property = "Name" } }, Top = 3, Skip = 3, ReadRecords = true, ReadTotalCount = true, }; Assert.AreEqual("a1, b1, b2 / 10", Dump(NewRepos(new ExplicitReadCommandRepository()).ExecuteReadCommand(command))); Assert.AreEqual(0, entityRepos.DropQueryCount()); Assert.AreEqual("b4, b5 / 5", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(2, entityRepos.DropQueryCount()); // Paging should result with two queries: selecting items and count. command = new ReadCommandInfo { ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("a1, b1, b2, b3, b4, b5 / 6", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(1, entityRepos.DropQueryCount()); // Without paging, there is no need for two queries. command = new ReadCommandInfo { Filters = new[] { new FilterCriteria("1") }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("a1, b1 / 2", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(1, entityRepos.DropQueryCount()); // Without paging, there is no need for two queries. command = new ReadCommandInfo { Filters = new[] { new FilterCriteria("1"), new FilterCriteria { Property = "Name", Operation = "StartsWith", Value = "b" } }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("b1 / 1", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(1, entityRepos.DropQueryCount()); // Without paging, there is no need for two queries. command = new ReadCommandInfo { Filters = new[] { new FilterCriteria("1"), new FilterCriteria { Filter = "System.String", Value = "b" } }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("b1 / 1", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(1, entityRepos.DropQueryCount()); // Without paging, there is no need for two queries. command = new ReadCommandInfo { Filters = new[] { new FilterCriteria("b") }, Top = 2, Skip = 2, OrderByProperties = new[] { new OrderByProperty { Property = "Name" } }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("b3, b4 / 5", Dump(genericRepos.ExecuteReadCommand(command))); Assert.AreEqual(1, entityRepos.DropQueryCount()); // Enumerable filter will cause GenericRepository to materialize of the query, so it will be executed only once even though the paging is used. }
public RecordsAndTotalCountResult <T> GetData <T>(string filter, string fparam, string genericfilter, string filters, IDictionary <string, Type[]> filterTypesByName, int top, int skip, int page, int psize, string sort, bool readRecords, bool readTotalCount) { // Legacy interface: if (page != 0 || psize != 0) { if (top != 0 || skip != 0) { throw new ClientException("Invalid paging parameter: Use either 'top' and 'skip', or 'page' and 'psize'."); } top = psize; skip = page > 0 ? psize * (page - 1) : 0; } var readCommandInfo = new ReadCommandInfo { DataSource = typeof(T).FullName, Filters = ParseFilterParameters(filter, fparam, genericfilter, filters, filterTypesByName), Top = top, Skip = skip, ReadRecords = readRecords, ReadTotalCount = readTotalCount, OrderByProperties = ParseSortParameter(sort) }; var readCommandResult = ExecuteReadCommand(readCommandInfo); return(new RecordsAndTotalCountResult <T> { Records = readCommandResult.Records != null?readCommandResult.Records.Cast <T>().ToArray() : null, TotalCount = readCommandResult.TotalCount != null ? readCommandResult.TotalCount.Value : 0 }); }
public void ExternalFilterType() { using (var container = new RhetosTestContainer()) { var repository = container.Resolve <Common.DomRepository>(); repository.TestFilter.ExternalFilter.Delete(repository.TestFilter.ExternalFilter.Query()); repository.TestFilter.ExternalFilter.Insert( new[] { "str", "snull", "date", "dnull", "ddef" } .Select(name => new TestFilter.ExternalFilter { Name = name })); var tests = new List <Tuple <Type, object, string> > { Tuple.Create <Type, object, string>(null, "abc", "str"), Tuple.Create <Type, object, string>(typeof(string), null, "snull"), Tuple.Create <Type, object, string>(null, DateTime.Now, "date"), Tuple.Create <Type, object, string>(typeof(DateTime), null, "ddef"), // A value type instance cannot be null. Tuple.Create <Type, object, string>(typeof(DateTime), default(DateTime), "ddef"), }; var gr = container.Resolve <GenericRepository <TestFilter.ExternalFilter> >(); foreach (var test in tests) { Type filterType = test.Item1 ?? test.Item2.GetType(); string testReport = filterType.FullName + ": " + test.Item2; Console.WriteLine(testReport); { var reposReadResult = gr.Load(test.Item2, filterType); Assert.AreEqual( test.Item3, TestUtility.DumpSorted(reposReadResult, item => item.Name), "ReadRepos: " + testReport); } { var readCommand = new ReadCommandInfo { DataSource = "TestFilter.ExternalFilter", Filters = new FilterCriteria[] { new FilterCriteria { Filter = filterType.FullName, Value = test.Item2 } }, ReadRecords = true }; var commandResult = (TestFilter.ExternalFilter[])ExecuteCommand(readCommand, container).Records; Assert.AreEqual( test.Item3, TestUtility.DumpSorted(commandResult, item => item.Name), "ReadCommand: " + testReport); } } } }
public void AutoFilter_NoRedundantAutoFilter() { using (var container = new RhetosTestContainer()) { var repository = container.Resolve <Common.DomRepository>(); repository.TestFilter.AutoFilter1.Delete(repository.TestFilter.AutoFilter1.Query()); repository.TestFilter.AutoFilter2.Delete(repository.TestFilter.AutoFilter2.Query()); repository.TestFilter.AutoFilter1.Insert( new[] { "a1", "a2", "b1", "b2" } .Select(name => new TestFilter.AutoFilter1 { Name = name })); repository.TestFilter.AutoFilter2.Insert( new[] { "a1", "a2", "b1", "b2" } .Select(name => new TestFilter.AutoFilter2 { Name = name })); var gr = container.Resolve <GenericRepositories>(); // Number of 'x' characters in the Name property shows how many times the filter was applied. // Auto filter should not be applied if the filter was already manually applied. var readCommand = new ReadCommandInfo { Filters = new FilterCriteria[] { new FilterCriteria("Name2", "contains", "2"), new FilterCriteria(typeof(string)) }, OrderByProperties = new OrderByProperty[] { new OrderByProperty { Property = "Name2", Descending = true } }, ReadTotalCount = true, Top = 1 }; TestClientRead <TestFilter.AutoFilter2Browse>(container, "b2x", item => item.Name2, readCommand); // Same filter manually applied multiple times. readCommand = new ReadCommandInfo { Filters = new FilterCriteria[] { new FilterCriteria("Name2", "contains", "2"), new FilterCriteria(typeof(string)), new FilterCriteria("abc") }, OrderByProperties = new OrderByProperty[] { new OrderByProperty { Property = "Name2", Descending = true } }, ReadTotalCount = true, Top = 1 }; TestClientRead <TestFilter.AutoFilter2Browse>(container, "b2xx", item => item.Name2, readCommand); } }
public string ReadBooks() { var readCommandInfo = new ReadCommandInfo { DataSource = "Bookstore.Book", ReadTotalCount = true }; var result = processingEngine.Execute(readCommandInfo); return($"{result.TotalCount} books."); }
private ReadCommandResult ExecuteReadCommand(ReadCommandInfo commandInfo) { var result = _processingEngine.Execute(new[] { commandInfo }); CheckForErrors(result); var resultData = (ReadCommandResult)(((Rhetos.XmlSerialization.XmlBasicData <ReadCommandResult>)(result.CommandResults.Single().Data)).Value); return(resultData); }
public void TestReadNoRowPermissions() { using (var container = new RhetosTestContainer()) { var gRepository = container.Resolve <GenericRepository <NoRP> >(); gRepository.Save(Enumerable.Range(0, 50).Select(a => new NoRP() { value = a }), null, gRepository.Load()); { var all = new ReadCommandInfo() { DataSource = "TestRowPermissions.NoRP", ReadRecords = true }; var result = ExecuteReadCommand(all, container); Assert.AreEqual(50, result.Records.Count()); } { var filtered = new ReadCommandInfo() { DataSource = "TestRowPermissions.NoRP", ReadRecords = true, Filters = new FilterCriteria[] { new FilterCriteria() { Filter = "TestRowPermissions.Value30" } } }; var result = ExecuteReadCommand(filtered, container); Assert.AreEqual(19, result.Records.Count()); } { var guid = Guid.NewGuid(); gRepository.Save(new NoRP[] { new NoRP() { ID = guid, value = 51 } }, null, null); var single = new ReadCommandInfo() { DataSource = "TestRowPermissions.NoRP", ReadRecords = true, Filters = new FilterCriteria[] { new FilterCriteria() { Property = "ID", Operation = "equal", Value = guid } } }; var result = ExecuteReadCommand(single, container); Assert.AreEqual(1, result.Records.Count()); Assert.AreEqual(51, (result.Records[0] as NoRP).value); } } }
public static ReadCommandResult ExecuteReadCommand( this GenericRepository <IEntity> genericRepository, ReadCommandInfo readCommandInfo) { readCommandInfo.DataSource = typeof(ServerCommandsUtilityTest.SimpleEntity).FullName; var serverCommandsUtility = new ServerCommandsUtility(new ConsoleLogProvider(), new ApplyFiltersOnClientRead()); var commandResult = serverCommandsUtility.ExecuteReadCommand(readCommandInfo, genericRepository); return(commandResult); }
public ReadCommandResult ReadCommand(ReadCommandInfo commandInfo) { return(new ReadCommandResult { Records = new SimpleEntityList { "a1", "b1", "b2" }.ToArray(), TotalCount = 10 }); }
public void Entity() { using (var scope = TestScope.Create()) { InitializeData(scope); var info = new ReadCommandInfo { DataSource = "TestQueryDataStructureCommand.E", ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("a, b, c, d, e /5", ReportCommandResult(scope, info, true)); } }
private static string ReportFilteredBrowse(RhetosTestContainer container, ReadCommandInfo readCommandInfo) { readCommandInfo.DataSource = "TestFilter.ComposableFilterBrowse"; return(TestUtility.DumpSorted( ExecuteCommand(readCommandInfo, container).Records, item => { var x = (TestFilter.ComposableFilterBrowse)item; return x.Name + " " + (x.DebugInfoSimpleName ?? "null"); })); }
private ReadCommandResult ExecuteReadCommand(ReadCommandInfo commandInfo) { var sw = Stopwatch.StartNew(); var result = _processingEngine.Execute(new[] { commandInfo }); CheckForErrors(result); var resultData = (ReadCommandResult)(((Rhetos.XmlSerialization.XmlBasicData <ReadCommandResult>)(result.CommandResults.Single().Data)).Value); _performanceLogger.Write(sw, "RestService: ExecuteReadCommand(" + commandInfo.DataSource + ") Executed."); return(resultData); }
private IPrincipal[] ReadUsers(params string[] usernames) { var command = new ReadCommandInfo { DataSource = typeof(Common.Principal).FullName, Filters = new[] { new FilterCriteria("Name", "in", usernames) }, ReadRecords = true }; var result = Exec <ReadCommandResult>(command); return((IPrincipal[])result.Records); }
string ReadErrorData(RhetosTestContainer container, string testName) { Console.WriteLine("Test: " + testName); var readCommand = new ReadCommandInfo() { DataSource = "TestRowPermissions.ErrorData", ReadRecords = true, Filters = new[] { new FilterCriteria(testName) } }; var loaded = ExecuteReadCommand(readCommand, container).Records; string report = TestUtility.DumpSorted(loaded, item => ((ErrorData)item).Name); Console.WriteLine("Result: " + report); return(report); }
public string DemoEntity() { var readCommand = new ReadCommandInfo() { DataSource = "AspNetDemo.DemoEntity", ReadRecords = true, }; var result = rhetosProcessingEngine.Value.Execute(new List <ICommandInfo>() { readCommand }); return(JsonConvert.SerializeObject(result, Formatting.Indented)); }
public void OnGet() { var readCommand = new ReadCommandInfo() { DataSource = "AspNetDemo.DemoEntity", ReadRecords = true }; var result = rhetosProcessingEngine.Value.Execute(new List <ICommandInfo>() { readCommand }); var readCommandResult = result.CommandResults.FirstOrDefault()?.Data.Value as ReadCommandResult; DemoEntityList = readCommandResult?.Records.Cast <DemoEntity>().ToList(); }
public string ReadBooks() { var readCommandInfo = new ReadCommandInfo() { DataSource = "Bookstore.Book", ReadTotalCount = true }; var processingResult = rhetosProcessingEngine.Value.Execute(new List <ICommandInfo>() { readCommandInfo }); var result = (ReadCommandResult)processingResult.CommandResults.Single().Data.Value; return(result.TotalCount.ToString()); }
public void Sort() { var readCommand = new ReadCommandInfo { OrderByProperties = new[] { new OrderByProperty { Property = "Name", Descending = true }, new OrderByProperty { Property = "Size", Descending = false }, } }; IQueryable <Entity2> query = new[] { new Entity2 { Name = "b", Size = 2, Ignore = 1 }, new Entity2 { Name = "b", Size = 2, Ignore = 2 }, new Entity2 { Name = "b", Size = 3, Ignore = 3 }, new Entity2 { Name = "b", Size = 1, Ignore = 4 }, new Entity2 { Name = "a", Size = 1, Ignore = 5 }, new Entity2 { Name = "c", Size = 3, Ignore = 6 } }.AsQueryable(); query = query.OrderBy(item => item.Ignore); // SortAndPaginate should ignore previous ordering, not append to it. var result = GenericFilterHelper.SortAndPaginate(query, readCommand); Console.WriteLine(result.ToString()); Assert.AreEqual( "c3, b1, b2, b2, b3, a1", TestUtility.Dump(result, item => item.Name + item.Size)); }
private static string ReportCommandResult(UnitOfWorkScope scope, ReadCommandInfo info, bool sort = false) { var commands = scope.Resolve <IIndex <Type, IEnumerable <ICommandImplementation> > >(); var readCommand = (ReadCommand)commands[typeof(ReadCommandInfo)].Single(); var result = (ReadCommandResult)readCommand.Execute(info).Data.Value; var items = ((IEnumerable <TestQueryDataStructureCommand.E>)result.Records).Select(item => item.Name); if (sort) { items = items.OrderBy(x => x); } var report = string.Join(", ", items) + " /" + result.TotalCount.Value; Console.WriteLine(report); return(report); }
public void AutoFilter_Complex() { using (var container = new RhetosTestContainer()) { var repository = container.Resolve <Common.DomRepository>(); repository.TestFilter.AutoFilter1.Delete(repository.TestFilter.AutoFilter1.Query()); repository.TestFilter.AutoFilter2.Delete(repository.TestFilter.AutoFilter2.Query()); repository.TestFilter.AutoFilter1.Insert( new[] { "a1", "a2", "b1", "b2" } .Select(name => new TestFilter.AutoFilter1 { Name = name })); repository.TestFilter.AutoFilter2.Insert( new[] { "a1", "a2", "b1", "b2" } .Select(name => new TestFilter.AutoFilter2 { Name = name })); var gr = container.Resolve <GenericRepositories>(); var readCommand = new ReadCommandInfo { Filters = new FilterCriteria[] { new FilterCriteria("Name", "contains", "2") }, OrderByProperties = new OrderByProperty[] { new OrderByProperty { Property = "Name", Descending = true } }, ReadTotalCount = true, Top = 1 }; TestClientRead <TestFilter.AutoFilter1>(container, "a2", item => item.Name, readCommand); readCommand = new ReadCommandInfo { Filters = new FilterCriteria[] { new FilterCriteria("Name2", "contains", "2") }, OrderByProperties = new OrderByProperty[] { new OrderByProperty { Property = "Name2", Descending = true } }, ReadTotalCount = true, Top = 1 }; TestClientRead <TestFilter.AutoFilter2Browse>(container, "b2x", item => item.Name2, readCommand); } }
public void PagingWithoutOrder() { using (var scope = TestScope.Create()) { InitializeData(scope); var info = new ReadCommandInfo { DataSource = "TestQueryDataStructureCommand.E", Top = 3, Skip = 3, ReadRecords = true, ReadTotalCount = true }; TestUtility.ShouldFail(() => ReportCommandResult(scope, info), "Sort order must be set if paging is used"); } }
public void Filters() { using (var scope = TestScope.Create()) { InitializeData(scope); var info = new ReadCommandInfo { DataSource = "TestQueryDataStructureCommand.E", Filters = new[] { new FilterCriteria { Property = "Name", Operation = "NotEqual", Value = "c" } }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("a, b, d, e /4", ReportCommandResult(scope, info, true)); } }
public void Ordering() { using (var scope = TestScope.Create()) { InitializeData(scope); var info = new ReadCommandInfo { DataSource = "TestQueryDataStructureCommand.E", OrderByProperties = new[] { new OrderByProperty { Property = "Name", Descending = true } }, ReadRecords = true, ReadTotalCount = true }; Assert.AreEqual("e, d, c, b, a /5", ReportCommandResult(scope, info)); } }
public void LogCommandDescription() { var log = new List <string>(); using (var scope = TestScope.Create(builder => builder .ConfigureLogMonitor(log) .ConfigureIgnoreClaims())) { var processingEngine = scope.Resolve <IProcessingEngine>(); var readPrincipals = new ReadCommandInfo { DataSource = "Common.Principal", ReadTotalCount = true, Filters = new[] { new FilterCriteria { Filter = "System.Guid[]", Value = new[] { new Guid("546df18b-5df8-4ffa-9b08-8da909efe067") } } } }; var processingEngineResult = processingEngine.Execute(new[] { readPrincipals }); Assert.IsTrue(processingEngineResult.Success); var excected = new ListOfTuples <string, IEnumerable <string> >() { { "request info", new[] { "ProcessingEngine Request", "ReadCommandInfo Common.Principal count, filters: System.Guid[] \"1 items: 546df18b-5df8-4ffa-9b08-8da909efe067\"" } }, { "command xml", new[] { "ProcessingEngine Commands", "Common.Principal</DataSource>", "true</ReadTotalCount>" } }, }; foreach (var test in excected) { Assert.IsTrue(log.Any(line => test.Item2.All(pattern => line.Contains(pattern))), "Missing a log entry for test '" + test.Item1 + "'."); } var notExpected = new[] { "error info", "CommandsWithClientError", "CommandsWithClientError", "CommandsWithServerError", "CommandsWithServerError" }; foreach (var test in notExpected) { Assert.IsFalse(log.Any(line => line.Contains(test)), "Unexpected log entry for test '" + test + "'."); } } }
public string CommonLog() { var readCommand = new ReadCommandInfo() { DataSource = "Common.Log", ReadRecords = true, OrderByProperties = new [] { new OrderByProperty() { Property = "Created", Descending = true } }, Top = 100 }; var result = rhetosProcessingEngine.Value.Execute(new List <ICommandInfo>() { readCommand }); return(JsonConvert.SerializeObject(result, Formatting.Indented)); }