public void WhenSelectBirthCustomerWhereFirstnameIsPopo_ShouldBe2Tokens()
        {
            var context   = new FakeQueryContext(() => new List <Customer>());
            var customers = context.GetTable <Customer>();
            var selected  =
                from c in customers
                where c.Firstname == "popo"
                select c.Birth;
            var array = selected.ToArray();

            Assert.IsTrue(typeof(IEnumerable <DateTime>).IsAssignableFrom(array.GetType()));

            Assert.AreEqual(2, context.Operations.Length);
            var @select = context.Operations.OfType <Visitor.Operation.Select>().Single();

            Assert.AreEqual(typeof(DateTime), ((Visitor.SelectArgs.Fields)@select.Item).Item.Head.Field.Type);
            Assert.AreEqual("Birth", ((Visitor.SelectArgs.Fields)@select.Item).Item.Head.Field.Name);
            Assert.AreEqual(1, ((Visitor.SelectArgs.Fields)@select.Item).Item.Length);

            var @where     = context.Operations.OfType <Visitor.Operation.Where>().Single();
            var comparison = (Visitor.WhereArgs.UnaryComparison)@where.Item;

            Assert.AreEqual(typeof(string), comparison.Item.Field.Type);
            Assert.AreEqual("Firstname", comparison.Item.Field.Name);
        }
        public void WhenSelectBirthAsBornDateAndIdCustomerWhereFirstnameIsPopoAndBirthGreatherOrEqualThan02Feb1985_ShouldBe2ComparisonAnd1SelectTokens()
        {
            var context   = new FakeQueryContext();
            var customers = context.GetTable <Customer>();
            var dateTime  = new DateTime(1985, 02, 11);
            var selected  =
                from c in customers
                where c.Firstname == "popo" && c.Birth >= dateTime
                select new
            {
                c.Id,
                BornDate = c.Birth
            };
            var soql       = selected.ToString();
            var array      = selected.ToList();
            var type       = array.GetType().GenericTypeArguments.Single();
            var properties = type.GetProperties();

            Assert.AreEqual("Id", properties[0].Name);
            Assert.AreEqual(typeof(Guid), properties[0].PropertyType);
            Assert.AreEqual("BornDate", properties[1].Name);
            Assert.AreEqual(typeof(DateTime), properties[1].PropertyType);

            var @select = context.Operations.OfType <Visitor.Operation.Select>().Single();

            Assert.AreEqual(typeof(DateTime), ((Visitor.SelectArgs.Fields)@select.Item).Item[1].Field.Type);
            Assert.AreEqual("Birth", ((Visitor.SelectArgs.Fields)@select.Item).Item[1].Field.Name);
            Assert.AreEqual("BornDate", ((Visitor.SelectArgs.Fields)@select.Item).Item[1].Alias.Value);
            Assert.AreEqual(2, ((Visitor.SelectArgs.Fields)@select.Item).Item.Length);
        }
        public void WhenSelectCustomerWhereFirstnameIsPopoInnerJoinBasket_ShouldBe2Tokens()
        {
            var context   = new FakeQueryContext();
            var customers = context.GetTable <Customer>();
            var baskets   = context.GetTable <Basket>();

            var customBaskets =
                from c in customers
                where c.Firstname == "popo"
                join basket in baskets on c.Id equals basket.CustomerId
                select basket;

            var array = customBaskets.ToList();

            Assert.IsTrue(typeof(IEnumerable <Basket>).IsAssignableFrom(array.GetType()));

            Assert.AreEqual(1, context.Operations.Length);
            var @join = context.Operations.OfType <Visitor.Operation.Join>().Single();

            Assert.AreEqual("Id", @join.Item3.Name);
            Assert.AreEqual(typeof(Guid), @join.Item3.Type);
            Assert.AreEqual("CustomerId", @join.Item4.Name);
            Assert.AreEqual(typeof(Guid), @join.Item4.Type);

            var @where     = (Visitor.Operation.Where)@join.Item1.Head;
            var comparison = (Visitor.WhereArgs.UnaryComparison)@where.Item;

            Assert.AreEqual(typeof(string), comparison.Item.Field.Type);
            Assert.AreEqual("Firstname", comparison.Item.Field.Name);
            Assert.AreEqual(Visitor.ComparisonKind.Equal, comparison.Item.Kind);
            Assert.IsTrue(comparison.Item.Target.IsConstant);
            Assert.AreEqual("popo", ((Visitor.ComparisonTarget.Constant)comparison.Item.Target).Item);
        }
        public void WhenProperyHasJsonPropertyAttributes_PropertyNameShouldBeAttributeValue()
        {
            var context  = new FakeQueryContext();
            var entities = context.GetTable <Entity1>();
            var query    =
                from c in entities
                where c.Name == "popo"
                select c;
            var soql = query.ToString();

            Assert.AreEqual(@"SELECT Id, Name, Cost FROM Entity1 WHERE Name = 'popo'", soql);
        }
        public void WhenOrderedByMultipleFields_ShouldHaveMultipleOrderByPredicates()
        {
            // Arrange
            var context = new FakeQueryContext();

            // Act
            var customers = context.GetTable <Customer>();
            var query     = customers.OrderBy(x => x.Id).ThenByDescending(x => x.Lastname);

            // Assert
            var soql = query.ToString();

            Assert.AreEqual("SELECT  FROM Customer  ORDER BY Id, Lastname DESC", soql);
        }
        public void WhenPagingParameters_ShouldHaveLimitAndOffset()
        {
            // Arrange
            var context = new FakeQueryContext();

            // Act
            var customers = context.GetTable <Customer>();
            var query     = customers.Skip(10).Take(5);

            // Assert
            var soql = query.ToString();

            Assert.AreEqual("SELECT  FROM Customer  LIMIT 5 OFFSET 10", soql);
        }
Exemple #7
0
        public void QueryProvider_WhenSelectCustomerWhereFirstnameIsPopo_SelectQuery()
        {
            var context = new FakeQueryContext(() => new List <Customer>());

            // Prepare query expression
            var customers = context.GetTable <Customer>();
            var queryExpr = customers.Where(x => x.Firstname == "popo").Expression;

            // Execute query expression
            var provider = new QueryProvider(context, "Customer");
            var query    = (provider as IQueryProvider).CreateQuery(queryExpr);

            // Asset
            Assert.AreEqual("SELECT  FROM Customer WHERE Firstname = 'popo'", query.ToString());
        }
        public void WhenSelectCustomer_ShouldBeOnlySelectTokenWithAllProperties()
        {
            var context   = new FakeQueryContext(() => new List <Customer>());
            var customers = context.GetTable <Customer>();
            var selected  =
                from c in customers
                select c;
            var array = selected.ToArray();

            Assert.IsTrue(typeof(IEnumerable <Customer>).IsAssignableFrom(array.GetType()));

            Assert.AreEqual(1, context.Operations.Length);
            var @select = context.Operations.OfType <Visitor.Operation.Select>().Single();

            Assert.AreEqual(typeof(Customer), ((Visitor.SelectArgs.SelectType)@select.Item).Item);
        }
        public void WhenSelectBirthAsBornDateAndIdCustomerWhereFirstnameIsPopoOrBirthGreatherOrEqualThan02Feb1985OrderByDate_ShouldBe2ComparisonAnd1SelectTokens()
        {
            var context   = new FakeQueryContext();
            var customers = context.GetTable <Customer>();
            var dateTime  = new DateTime(1985, 02, 11);
            var query     =
                from c in customers
                where c.Firstname == "popo" || c.Birth >= dateTime
                orderby c.Birth descending
                select new
            {
                c.Id,
                BornDate = c.Birth
            };
            var soql = query.ToString();

            Assert.AreEqual(@"SELECT Id, Birth FROM Customer WHERE (Firstname = 'popo') OR (Birth >= 1985-02-11T00:00:00Z) ORDER BY Birth DESC", soql);
        }
Exemple #10
0
        public void Visitor_MultipleOrderingDifferentDirection()
        {
            // Arrange
            var context = new FakeQueryContext(() => new List <Customer>());

            // Act
            var customers = context.GetTable <Customer>();
            var query     = customers.AsQueryable().OrderBy(x => x.Id).ThenByDescending(x => x.Lastname);
            var queryExp  = query.Expression;
            var visitor   = new Visitor.RequestExpressionVisitor(queryExp);

            visitor.Visit();

            // Assert
            var actualOperations = visitor.Operations;

            Assert.AreEqual(2, actualOperations.Count());
            AssertOrderOperation(actualOperations[0] as Visitor.Operation.Order, "Id");
            AssertOrderOperation(actualOperations[1] as Visitor.Operation.Order, "Lastname", Visitor.OrderDirection.Descending);
        }
Exemple #11
0
        public void Visitor_OrderingDescending()
        {
            // Arrange
            var context = new FakeQueryContext(() => new List <Customer>());

            // Act
            var customers = context.GetTable <Customer>();
            var query     = customers.AsQueryable().OrderByDescending(x => x.Id);
            var queryExp  = query.Expression;
            var visitor   = new Visitor.RequestExpressionVisitor(queryExp);

            visitor.Visit();

            // Assert
            var actualOperations = visitor.Operations;

            Assert.AreEqual(1, actualOperations.Count());
            var operation = actualOperations.First() as Visitor.Operation.Order;

            AssertOrderOperation(operation, "Id", Visitor.OrderDirection.Descending);
        }
        public void WhenSelectCustomerWhereFirstnameIsPopo_ShouldBe2Tokens()
        {
            var context   = new FakeQueryContext(() => new List <Customer>());
            var customers = context.GetTable <Customer>();
            var selected  =
                from c in customers
                where c.Firstname == "popo"
                select c;
            var array = selected.ToArray();

            Assert.IsTrue(typeof(IEnumerable <Customer>).IsAssignableFrom(array.GetType()));

            Assert.AreEqual(1, context.Operations.Length);
            var @where     = context.Operations.OfType <Visitor.Operation.Where>().Single();
            var comparison = (Visitor.WhereArgs.UnaryComparison)@where.Item;

            Assert.AreEqual(typeof(string), comparison.Item.Field.Type);
            Assert.AreEqual("Firstname", comparison.Item.Field.Name);
            Assert.AreEqual(Visitor.ComparisonKind.Equal, comparison.Item.Kind);
            Assert.IsTrue(comparison.Item.Target.IsConstant);
            Assert.AreEqual("popo", ((Visitor.ComparisonTarget.Constant)comparison.Item.Target).Item);
        }
        public void WhenSelectBirthAndIdCustomerWhereFirstnameIsPopoAndBirthGreatherOrEqualThan02Feb1985_ShouldBe2ComparisonAnd1SelectTokens()
        {
            var context   = new FakeQueryContext();
            var customers = context.GetTable <Customer>();
            var dateTime  = new DateTime(1985, 02, 11);
            var selected  =
                from c in customers
                where c.Firstname == "popo" && c.Birth >= dateTime
                select new
            {
                c.Id,
                c.Birth
            };
            var array      = selected.ToList();
            var type       = array.GetType().GenericTypeArguments.Single();
            var properties = type.GetProperties();

            Assert.AreEqual("Id", properties[0].Name);
            Assert.AreEqual(typeof(Guid), properties[0].PropertyType);
            Assert.AreEqual("Birth", properties[1].Name);
            Assert.AreEqual(typeof(DateTime), properties[1].PropertyType);
        }
Exemple #14
0
        public void Visitor_OData_SkipSimple()
        {
            // Arrange
            var context = new FakeQueryContext(() => new List <Customer>());
            var limit   = (int)10;

            // Act
            var customers = context.GetTable <Customer>();
            var queryExp  = ODataHelpers.Skip(customers.AsQueryable().Expression, limit, typeof(Customer), true);
            var visitor   = new Visitor.RequestExpressionVisitor(queryExp);

            visitor.Visit();

            // Assert
            var actualOperations = visitor.Operations;

            Assert.AreEqual(1, actualOperations.Count());
            var takeOperation = actualOperations.First();

            Assert.IsTrue(takeOperation.IsSkip);
            Assert.AreEqual(limit, ((Visitor.Operation.Skip)takeOperation).Item);
        }
        public void WhenSelectBirthCustomerWhereFirstnameIsPopoAndBirthGreatherOrEqualThan02Feb1985_ShouldBe2ComparisonAnd1SelectTokens()
        {
            var context   = new FakeQueryContext(() => new List <Customer>());
            var customers = context.GetTable <Customer>();
            var dateTime  = new DateTime(1985, 02, 11);
            var selected  =
                from c in customers
                where c.Firstname == "popo" && c.Birth >= dateTime
                select c.Birth;
            var array = selected.ToArray();

            Assert.IsTrue(typeof(IEnumerable <DateTime>).IsAssignableFrom(array.GetType()));

            Assert.AreEqual(2, context.Operations.Length);
            var @select = context.Operations.OfType <Visitor.Operation.Select>().Single();

            Assert.AreEqual(typeof(DateTime), ((Visitor.SelectArgs.Fields)@select.Item).Item.Head.Field.Type);
            Assert.AreEqual("Birth", ((Visitor.SelectArgs.Fields)@select.Item).Item.Head.Field.Name);
            Assert.AreEqual(1, ((Visitor.SelectArgs.Fields)@select.Item).Item.Length);

            var @where           = context.Operations.OfType <Visitor.Operation.Where>().Single();
            var binaryComparison = (Visitor.WhereArgs.BinaryComparison)@where.Item;
            var cmp1             = ((Visitor.WhereArgs.UnaryComparison)binaryComparison.Item1).Item;

            Assert.AreEqual(typeof(string), cmp1.Field.Type);
            Assert.AreEqual("Firstname", cmp1.Field.Name);

            Assert.AreEqual(Visitor.BinaryComparisonKind.And, binaryComparison.Item2);

            var cmp2 = ((Visitor.WhereArgs.UnaryComparison)binaryComparison.Item3).Item;

            Assert.AreEqual(typeof(DateTime), cmp2.Field.Type);
            Assert.AreEqual("Birth", cmp2.Field.Name);

            var value = ((Visitor.ComparisonTarget.Constant)cmp2.Target).Item;

            Assert.AreEqual(dateTime, value);
        }
Exemple #16
0
        public void Visitor_AndExpression()
        {
            // Arrange
            var context = new FakeQueryContext(() => new List <Customer>());

            // Act
            var customers = context.GetTable <Customer>();
            var query     = customers.AsQueryable().Where(x => x.Firstname == "popo" && x.Lastname == "papa");
            var queryExp  = query.Expression;
            var visitor   = new Visitor.RequestExpressionVisitor(queryExp);

            visitor.Visit();

            // Assert
            var actualOperations = visitor.Operations;

            Assert.AreEqual(1, actualOperations.Count());
            var operation        = actualOperations.First() as Visitor.Operation.Where;
            var compareOperation = operation.Item as Visitor.WhereArgs.BinaryComparison;

            AssertUnaryComparison(compareOperation.Item1, "Firstname", "popo", Visitor.ComparisonKind.Equal);
            Assert.AreEqual(Visitor.BinaryComparisonKind.And, compareOperation.Item2);
            AssertUnaryComparison(compareOperation.Item3, "Lastname", "papa", Visitor.ComparisonKind.Equal);
        }