public void Multi()
        {
            MockContextFactory.SeedAndTestContextFor("FilterInterceptorTests_Multi", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var queryable = ctx.Orders.AsQueryable();

                var criteria = new QueryCriteria()
                {
                    Filters = new List <IFilter>
                    {
                        new SimpleFilter {
                            Path = "CustomerFirstName", Value = "David", Type = FilterType.Contains
                        }
                    }
                };

                var query = new QueryHandler();
                query.AddInterceptor(new MockFilterInterceptorA());
                query.AddInterceptor(new MockFilterInterceptorB());
                var result = query.Execute(queryable, criteria);

                var actual   = result.Data.Cast <Order>().ToList();;
                var expected = queryable.Where(t => t.Customer.LastName == "Norris").ToList();
                Assert.Equal(expected, actual);
            });
        }
Ejemplo n.º 2
0
        public void InterceptorsWithGrouping()
        {
            MockContextFactory.SeedAndTestContextFor("GroupTests_InterceptorsWithGrouping", TestSeeders.SeedTicketScenario, ctx =>
            {
                var criteria = new QueryCriteria()
                {
                    Groups = new List <IGroup>()
                    {
                        new Group {
                            Path = "TicketType"
                        }
                    },
                    Aggregates = new List <IAggregate>()
                    {
                        new Aggregate {
                            Type = AggregateType.Count
                        }
                    }
                };

                var interceptor  = new InterceptorsWithGrouping();
                var queryHandler = new QueryHandler(Enumerable.Empty <IQueryInterceptorProvider>());
                queryHandler.AddInterceptor(interceptor);
                var result = queryHandler.Execute <Ticket, InterceptorWithGroupingFakeModel>(ctx.Tickets, criteria, new QueryExecutionOptions
                {
                    GroupByInMemory = true
                });

                Assert.Equal(4, interceptor.Count);
                Assert.True(interceptor.Test);
                Assert.True(interceptor.Test2);
                Assert.True(interceptor.Test3);
                Assert.True(interceptor.Test4);
            });
        }
Ejemplo n.º 3
0
        public void SimpleWithExtensions2()
        {
            MockContextFactory.SeedAndTestContextFor("FilterInterceptorTests_SimpleWithExtensions2", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var queryable = ctx.Orders.AsQueryable();

                var criteria = new QueryCriteria()
                {
                    Filters = new List <IFilter>
                    {
                        new SimpleFilter {
                            Path = "CustomerFullName", Value = "Da", Type = FilterType.Contains
                        }
                    }
                };

                var query = new QueryHandler(Enumerable.Empty <IQueryInterceptorProvider>());
                query.AddInterceptor(new MockFilterInterceptorAWithExtension());
                var result = query.Execute(queryable, criteria);

                var actual   = result.Data.Cast <Order>().ToList();;
                var expected = queryable.Where(t => t.Customer.FirstName.Contains("Da") || t.Customer.LastName.Contains("Da")).ToList();
                Assert.Equal(expected, actual);
            });
        }
        public void Simple()
        {
            MockContextFactory.SeedAndTestContextFor("GroupInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var expected = ctx.Orders
                               .OrderBy(t => t.Customer.FirstName)
                               .GroupBy(t => t.Customer.FirstName)
                               .Select(t => t.Key)
                               .ToList();

                var criteria = new QueryCriteria();
                criteria.Groups.Add(new Group {
                    Path = "CustomerFirstName"
                });
                var queryHandler = new QueryHandler(Enumerable.Empty <IQueryInterceptorProvider>());
                queryHandler.AddInterceptor(new MockGroupInterceptor());
                var result = queryHandler.Execute(ctx.Orders.Include(t => t.Customer), criteria, new QueryExecutionOptions
                {
                    GroupByInMemory = true
                });

                var groupedResult = result.GroupedResult();
                var actual        = groupedResult.Groups.Select(t => t.GroupValue).ToList();
                Assert.Equal(expected, actual);
            });
        }
 public void Generic()
 {
     MockContextFactory.SeedAndTestContextFor("ConvertibleIntereceptorTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
     {
         var criteria     = new QueryCriteria();
         var queryHandler = new QueryHandler();
         queryHandler.AddInterceptor(new MockQueryConvertGenericInterceptor());
         var result = queryHandler.Execute <Customer, CustomerModel>(ctx.Customers, criteria);
         Assert.All(result.Data, t => Assert.IsType <CustomerModel>(t));
     });
 }
 public void NonGeneric()
 {
     MockContextFactory.SeedAndTestContextFor("QueryConvertInterceptorTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx =>
     {
         var criteria     = new QueryCriteria();
         var queryHandler = new QueryHandler();
         queryHandler.AddInterceptor(new MockQueryConvertInterceptor());
         var result = queryHandler.Execute(ctx.Customers, criteria);
         Assert.All(result.Data.Cast <Customer>().ToList(), t => Assert.IsType <CustomerModel>(t));
     });
 }
 public void Simple()
 {
     MockContextFactory.SeedAndTestContextFor("QueryProviderTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
     {
         // criteria
         var criteria     = new QueryCriteria();
         var queryHandler = new QueryHandler(new List <IQueryInterceptorProvider> {
             new QueryInterceptorProvider()
         });
         queryHandler.AddInterceptor(new FakeInterceptor());
         var interceptors = queryHandler.ResolveInterceptors <Order, Order>(criteria, ctx.Orders);
         Assert.Equal(1, interceptors.Count);
         Assert.True(interceptors[0].GetType() == typeof(FakeInterceptor));
     });
 }
Ejemplo n.º 8
0
        public void Simple()
        {
            MockContextFactory.SeedAndTestContextFor("SortInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
            {
                // expected
                var expected = ctx.Orders.OrderBy(t => t.Customer.FirstName).ThenBy(t => t.Customer.LastName).ToList();

                // criteria
                var criteria = new QueryCriteria();
                criteria.Sorts.Add(new Sort("CustomerFullName"));
                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(new MockSortInterceptor());
                var result = queryHandler.Execute(ctx.Orders, criteria);
                Assert.Equal(expected, result.Data.Cast <Order>().ToList());
            });
        }
Ejemplo n.º 9
0
        public void SortInterceptor()
        {
            MockContextFactory.SeedAndTestContextFor("SortTests_SortInterceptor", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var shouldResult = ctx.Orders.OrderByDescending(t => t.OrderNum).ToList();

                // query handler that is empty should be the same as running to list.
                var criteria = new QueryCriteria()
                {
                    Sorts = new List <ISort>()
                    {
                        new Sort("OrderNumStr", false)
                    }
                };

                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(new MockSortInterceptor());
                var result = queryHandler.Execute(ctx.Orders, criteria);
                Assert.Equal(shouldResult, result.Data.Cast <Order>().ToList());
            });
        }
        public void Simple()
        {
            MockContextFactory.SeedAndTestContextFor("GroupInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var expected = ctx.Orders
                               .OrderBy(t => t.Customer.FirstName)
                               .GroupBy(t => t.Customer.FirstName)
                               .Select(t => t.Key)
                               .ToList();

                var criteria = new QueryCriteria();
                criteria.Groups.Add(new Group {
                    Path = "CustomerFirstName"
                });
                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(new MockGroupInterceptor());
                var result = queryHandler.Execute(ctx.Orders, criteria);
                var actual = result.Data.Cast <IGroupQueryResult>().Select(t => t.GroupValue).ToList();
                Assert.Equal(expected, actual);
            });
        }
Ejemplo n.º 11
0
        public void Simple()
        {
            MockContextFactory.SeedAndTestContextFor("AggregatorInterceptorTests_Simple", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var expected = ctx.OrderItems.GroupBy(t => true).Select(t => new
                {
                    PriceAtTheTime = t.Average(t2 => t2.Item.Price)
                }).First();

                var criteria = new QueryCriteria();
                criteria.Aggregates.Add(new Aggregate
                {
                    Type = AggregateType.Avg,
                    Path = "ItemPrice"
                });
                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(new MockAggregateInterceptor());
                var result = queryHandler.Execute(ctx.OrderItems, criteria);
                Assert.Equal(expected.PriceAtTheTime, result.Aggregates.First().Value);
            });
        }
        public void DynamicQuery_PartialSelect_Include_Fails()
        {
            var criteria = new QueryCriteria();

            criteria.Filters.Add(new SimpleFilter()
            {
                Value = 15, Path = "id", Type = FilterType.Equal
            });
            var _handler = new QueryHandler(Enumerable.Empty <IQueryInterceptorProvider>());
            IQueryable <Product> query = DbTestContext.Set <Product>();

            var _articlesStragetyInterceptor = new ProductStragetyInterceptorFail();

            _handler.AddInterceptor(_articlesStragetyInterceptor);

            var toconv = _handler.Execute <Product>(query, criteria);

            if (toconv.TotalRecords > 0)
            {
                Assert.True(toconv.Data.FirstOrDefault().AttributeValues.FirstOrDefault().value == "red");
            }
        }
Ejemplo n.º 13
0
        public void Generic()
        {
            MockContextFactory.SeedAndTestContextFor("BeforeFilterTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var criteria    = new QueryCriteria();
                var interceptor = new MockBeforeQueryFilterGenericInterceptor();

                // queryable of orders.
                var queryable = ctx.Orders.AsQueryable();

                // pass into the interceptor.
                queryable = interceptor.InterceptBeforeFiltering(criteria, queryable);

                // query handler should pass by the same interceptor.
                var queryHandler = new QueryHandler(Enumerable.Empty <IQueryInterceptorProvider>());
                queryHandler.AddInterceptor(interceptor);
                var result = queryHandler.Execute(ctx.Orders, criteria);

                // compare results.
                var expected = queryable.ToList();
                Assert.Equal(expected, result.Data);
            });
        }
Ejemplo n.º 14
0
        public void Generic()
        {
            MockContextFactory.SeedAndTestContextFor("IncludeStrategyTests_Generic", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var criteria    = new QueryCriteria();
                var interceptor = new MockIncludeStrategyGenericInterceptor();

                // queryable of orders.
                var queryable = ctx.Orders.AsQueryable();

                // pass into the interceptor.
                queryable = interceptor.InterceptIncludeStrategy(criteria, queryable);

                // query handler should pass by the same interceptor.
                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(interceptor);
                var result = queryHandler.Execute(ctx.Orders, criteria);

                // compare results.
                var expected = queryable.ToList();
                Assert.Equal(expected, result.Data.Cast <Order>().ToList());
            });
        }
Ejemplo n.º 15
0
        public void NonGeneric()
        {
            MockContextFactory.SeedAndTestContextFor("NoSortTests_NonGeneric", TestSeeders.SimpleSeedScenario, ctx =>
            {
                var criteria    = new QueryCriteria();
                var interceptor = new MockNoSortInterceptor();

                // queryable of orders.
                var queryable = ctx.Orders.AsQueryable();

                // pass into the interceptor.
                queryable = (IQueryable <Order>)interceptor.InterceptNoSort(criteria, queryable);

                // query handler should pass by the same interceptor.
                var queryHandler = new QueryHandler();
                queryHandler.AddInterceptor(interceptor);
                var result = queryHandler.Execute(ctx.Orders, criteria);

                // compare results.
                var expected = queryable.ToList();
                Assert.Equal(expected, result.Data);
            });
        }
Ejemplo n.º 16
0
        private static void Play1()
        {
            var list = new List <Person>()
            {
                new Person {
                    Id = 1, FirstName = "David", LastName = "Lebee", Sex = "Male", Age = 29
                },
                new Person {
                    Id = 2, FirstName = "Michaela", LastName = "Lebee", Sex = "Female", Age = 29
                },
                new Person {
                    Id = 3, FirstName = "Zohra", LastName = "Lebee", Sex = "Female", Age = 20
                },
                new Person {
                    Id = 4, FirstName = "Eric", LastName = "Vickar", Sex = "Male", Age = 30
                },
                new Person {
                    Id = 5, FirstName = "Susan", LastName = "Vickar", Sex = "Female", Age = 30
                },
            };

            var queryable = list.AsQueryable();
            var criteria  = new QueryCriteria();

            criteria.Page     = 1;
            criteria.PageSize = 10;

            criteria.Groups = new List <IGroup>()
            {
                new Group {
                    Path = "LastName"
                },
                new Group {
                    Path = "Sexe"
                }
            };

            criteria.Aggregates = new List <IAggregate>()
            {
                new Aggregate {
                    Type = AggregateType.Count
                },
                new Aggregate {
                    Path = "AgeStr", Type = AggregateType.Avg
                }
            };;

            var handler = new QueryHandler();

            handler.AddInterceptor(new PersonQueryInterceptor());
            var result = handler.Execute(queryable, criteria);

            var jsonSettings = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };

            jsonSettings.Converters.Add(new StringEnumConverter {
                AllowIntegerValues = false
            });

            Console.WriteLine("Request:\n");
            Console.WriteLine(JsonConvert.SerializeObject(criteria, Formatting.Indented, jsonSettings));
            Console.WriteLine("");
            Console.WriteLine("Response:\n");
            Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented, jsonSettings));
            Console.ReadKey();
        }