コード例 #1
0
        public void TestObjectFilters()
        {
            QueryInjector.RegisterGlobal(
                ib => ib.WhenEncountering <IQueryable <Student> >().ReplaceWith(students => students.Where(student => !student.Deleted))
                );

            var collection = new[]
            {
                new Student {
                    Deleted = false
                },
                new Student {
                    Deleted = true
                },
                new Student {
                    Deleted = false
                },
                new Student {
                    Deleted = true
                },
                new Student {
                    Deleted = false
                },
            }.AsQueryable();

            var actualQuery = collection.AsInjectableQueryable() as IInjectableQueryable;
            var expected    = collection.Where(student => !student.Deleted);

            Assert.NotNull(actualQuery);
            ExpressionEqualityComparer.AssertExpressionsEqual(expected.Expression, actualQuery.GetInjectedExpression());
        }
コード例 #2
0
        public void TestBasicInjectionString()
        {
            Expression <Func <IQueryable <string>, IQueryable <string> > > replaceExpr = s => new[] { "replaced!" }.AsQueryable();

            QueryInjector.RegisterGlobal(
                ib => ib.WhenEncountering <IQueryable <string> >().ReplaceWith(replaceExpr)
                );

            var actualQuery = new[] { "a", "b", "c" }.AsInjectableQueryable() as IInjectableQueryable;

            Assert.NotNull(actualQuery);
            ExpressionEqualityComparer.AssertExpressionsEqual(replaceExpr.Body, actualQuery.GetInjectedExpression());
        }
コード例 #3
0
        public void TestBasicInjectionOnWhereInt()
        {
            QueryInjector.RegisterGlobal(
                ib => ib.WhenEncountering <IQueryable <int> >().ReplaceWith(q => q.Where(i => i % 2 == 0))
                );

            var collection = new[] { 1, 2, 3, 4, 5 }.AsQueryable();
            var actualQuery = collection.Where(i => i > 3).AsInjectableQueryable() as IInjectableQueryable;
            var expected    = collection.Where(i => i % 2 == 0).Where(i => i > 3);

            Assert.NotNull(actualQuery);
            ExpressionEqualityComparer.AssertExpressionsEqual(expected.Expression, actualQuery.GetInjectedExpression());
        }
コード例 #4
0
        public void TestBasicInjectionStringLiteral()
        {
            Expression <Func <string, string> > replaceExpr = s => "rob";

            QueryInjector.RegisterGlobal(
                ib => ib.WhenEncountering <string>().ReplaceWith(replaceExpr)
                );

            var initialCollection = new[] { "a", "b", "c" }.AsQueryable();
            var actualQuery = initialCollection.Where(s => s == "rob").AsInjectableQueryable() as IInjectableQueryable;

            //We need to write it this way, because .Where(s => "rob" == "rob") is optimised into .Where(s => true)
            var expression    = Expression.Lambda <Func <string, bool> >(Expression.Equal(Expression.Constant("rob"), Expression.Constant("rob")), Expression.Parameter(typeof(string), "s"));
            var expectedQuery = initialCollection.Where(expression);

            Assert.NotNull(actualQuery);
            ExpressionEqualityComparer.AssertExpressionsEqual(expectedQuery.Expression, actualQuery.GetInjectedExpression());
        }
コード例 #5
0
        public void TestInjectionParameter()
        {
            var initialCollection = new[] { 1, 2, 3 }.AsQueryable();

            QueryInjector.RegisterGlobal(
                ib => ib.WhenEncountering <IQueryable <int> >().ReplaceWith(collection => collection.Where(c => c == Inject <int> .Value("testKey")))
                );

            var actualQuery =
                initialCollection
                .AsInjectableQueryable()
                .InjectWith("testKey", 5)
                as IInjectableQueryable;

            var expected = initialCollection.Where(c => c == 5);

            Assert.NotNull(actualQuery);
            ExpressionEqualityComparer.AssertExpressionsEqual(expected.Expression, actualQuery.GetInjectedExpression());
        }
コード例 #6
0
 public void Setup()
 {
     QueryInjector.ClearGlobals();
 }