Beispiel #1
0
        public void HandleResultOperator()
        {
            var keySelector     = new SqlColumnDefinitionExpression(typeof(string), "c", "Name", false);
            var elementSelector = Expression.Constant("elementSelector");
            var resultOperator  = new GroupResultOperator("itemName", keySelector, elementSelector);

            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(keySelector, _context))
            .Return(keySelector);
            _stageMock
            .Expect(mock => mock.PrepareResultOperatorItemExpression(elementSelector, _context))
            .Return(elementSelector);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();
            Assert.That(_sqlStatementBuilder.GroupByExpression, Is.SameAs(keySelector));

            var expectedSelectProjection = new SqlGroupingSelectExpression(
                new NamedExpression("key", keySelector),
                new NamedExpression("element", elementSelector));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, _sqlStatementBuilder.SelectProjection);

            Assert.That(
                _sqlStatementBuilder.DataInfo.DataType,
                Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(IGrouping <,>).MakeGenericType(typeof(string), typeof(string)))));
        }
        public void HandleResultOperator_CreatesAndPreparesSubStatementWithNewProjection()
        {
            var resultOperator = new SkipResultOperator(Expression.Constant(0));

            _sqlStatementBuilder.Orderings.Add(_ordering);

            var expectedNewProjection = Expression.New(
                _tupleCtor,
                new Expression[] { _selectProjection, new SqlRowNumberExpression(new[] { _ordering }) },
                new MemberInfo[] { GetTupleMethod("get_Key"), GetTupleMethod("get_Value") });

            var fakePreparedProjection = GetFakePreparedProjection();

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg.Is(_context)))
            .WhenCalled(mi => SqlExpressionTreeComparer.CheckAreEqualTrees(expectedNewProjection, (Expression)mi.Arguments[0]))
            .Return(fakePreparedProjection);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, UniqueIdentifierGenerator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.SqlTables.Count, Is.EqualTo(1));
            var sqlTable = _sqlStatementBuilder.SqlTables[0];

            Assert.That(sqlTable.TableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));
            Assert.That(sqlTable.JoinSemantics, Is.EqualTo(JoinSemantics.Inner));

            var subStatementTableInfo = ((ResolvedSubStatementTableInfo)sqlTable.TableInfo);

            Assert.That(subStatementTableInfo.SqlStatement.SelectProjection, Is.SameAs(fakePreparedProjection));
            Assert.That(subStatementTableInfo.TableAlias, Is.EqualTo("q0"));
        }
Beispiel #3
0
        public void HandleResultOperator()
        {
            var predicate         = Expression.Constant(true);
            var preparedPredicate = Expression.Constant(false);
            var resultOperator    = new AllResultOperator(predicate);
            var sqlStatement      = _sqlStatementBuilder.GetSqlStatement();

            var fakePreparedSelectProjection = Expression.Constant(false);

            _stageMock
            .Expect(mock => mock.PrepareWhereExpression(
                        Arg <Expression> .Matches(e => e.NodeType == ExpressionType.Not && (((UnaryExpression)e).Operand == predicate)),
                        Arg <ISqlPreparationContext> .Matches(c => c == _context)))
            .Return(preparedPredicate);
            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Matches(e => e.NodeType == ExpressionType.Not), Arg.Is(_context)))
            .WhenCalled(
                mi =>
            {
                var selectProjection     = (Expression)mi.Arguments[0];
                var expectedSubStatement = new SqlStatementBuilder(sqlStatement)
                {
                    WhereCondition = preparedPredicate
                }.GetSqlStatement();
                var expectedExistsExpression = new SqlExistsExpression(new SqlSubStatementExpression(expectedSubStatement));
                var expectedExpression       = Expression.Not(expectedExistsExpression);

                SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, selectProjection);
            })
            .Return(fakePreparedSelectProjection);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedScalarValueInfo)));
            Assert.That(((StreamedScalarValueInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(Boolean)));

            Assert.That(_sqlStatementBuilder.SelectProjection, Is.SameAs(fakePreparedSelectProjection));
        }
        public void CreateSqlTableForSubStatement_WithOrderings_ReturnsTableWithoutOrderings_WithNewProjection()
        {
            var fakeSelectProjection = Expression.Constant(new KeyValuePair <Cook, KeyValuePair <string, string> > ());

            _stageMock
            .Expect(mock => mock.PrepareSelectExpression(Arg <Expression> .Is.Anything, Arg.Is(_context)))
            .Return(fakeSelectProjection);
            _stageMock.Replay();

            var result = _factory.CreateSqlTableForStatement(_statementWithOrderings, info => new SqlTable(info, JoinSemantics.Inner));

            _stageMock.VerifyAllExpectations();

            var tableInfo = ((SqlTable)result.SqlTable).TableInfo;

            Assert.That(tableInfo, Is.TypeOf(typeof(ResolvedSubStatementTableInfo)));

            var subStatement = ((ResolvedSubStatementTableInfo)tableInfo).SqlStatement;

            Assert.That(subStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo)));
            Assert.That(((StreamedSequenceInfo)subStatement.DataInfo).ItemExpression, Is.SameAs(subStatement.SelectProjection));
            Assert.That(((StreamedSequenceInfo)subStatement.DataInfo).DataType,
                        Is.SameAs(typeof(IQueryable <KeyValuePair <Cook, KeyValuePair <string, string> > >)));

            var expectedSubStatementBuilder = new SqlStatementBuilder(_statementWithOrderings)
            {
                SelectProjection = fakeSelectProjection,
                DataInfo         = subStatement.DataInfo
            };

            expectedSubStatementBuilder.Orderings.Clear();
            Assert.That(subStatement, Is.EqualTo(expectedSubStatementBuilder.GetSqlStatement()));

            Assert.That(result.WhereCondition, Is.Null);
        }
        public void VisitQuerySourceReferenceExpression()
        {
            var innerSequenceExpression = Expression.Constant(new[] { new Cook() });
            var joinClause = new JoinClause(
                "x",
                typeof(Cook[]),
                innerSequenceExpression,
                Expression.Constant(new Cook()),
                Expression.Constant(new Cook()));
            var groupJoinClause = new GroupJoinClause("g", typeof(Cook[]), joinClause);
            var querySourceReferenceExpression = new QuerySourceReferenceExpression(groupJoinClause);
            var fakeWhereExpression            = Expression.Constant(true);

            _stageMock
            .Expect(mock => mock.PrepareWhereExpression(Arg <Expression> .Matches(e => e is BinaryExpression), Arg.Is(_context)))
            .WhenCalled(
                mi =>
                SqlExpressionTreeComparer.CheckAreEqualTrees(
                    Expression.Equal(groupJoinClause.JoinClause.OuterKeySelector, groupJoinClause.JoinClause.InnerKeySelector),
                    (Expression)mi.Arguments[0]))
            .Return(fakeWhereExpression);
            _stageMock.Replay();

            var visitor = CreateTestableVisitor(_someOrderingExtractionPolicy);
            var result  = visitor.VisitQuerySourceReference(querySourceReferenceExpression);

            _stageMock.VerifyAllExpectations();

            Debug.Assert(visitor.FromExpressionInfo != null, "_visitor.FromExpressionInfo != null");
            var fromExpressionInfo = (FromExpressionInfo)visitor.FromExpressionInfo;

            SqlExpressionTreeComparer.CheckAreEqualTrees(
                new SqlTableReferenceExpression(fromExpressionInfo.SqlTable),
                _context.GetExpressionMapping(new QuerySourceReferenceExpression(groupJoinClause.JoinClause)));
            SqlExpressionTreeComparer.CheckAreEqualTrees(fromExpressionInfo.ItemSelector, result);
            Assert.That(((UnresolvedTableInfo)fromExpressionInfo.SqlTable.TableInfo).ItemType, Is.EqualTo(typeof(Cook)));
            Assert.That(fromExpressionInfo.WhereCondition, Is.SameAs(fakeWhereExpression));
            Assert.That(fromExpressionInfo.ExtractedOrderings.Count, Is.EqualTo(0));
        }
        public void MoveCurrentStatementToSqlTable()
        {
            var originalStatement = _statementBuilder.GetSqlStatement();

            var fakeFromExpressionInfo = CreateFakeFromExpressionInfo(new Ordering[0]);
            Func <ITableInfo, SqlTable> tableGenerator = info => new SqlTable(info, JoinSemantics.Inner);

            _stageMock
            .Expect(mock => mock.PrepareFromExpression(
                        Arg <SqlSubStatementExpression> .Is.TypeOf,
                        Arg.Is(_context),
                        Arg.Is(tableGenerator)))
            .Return(fakeFromExpressionInfo)
            .WhenCalled(mi =>
            {
                var sqlStatement = ((SqlSubStatementExpression)mi.Arguments[0]).SqlStatement;
                SqlExpressionTreeComparer.CheckAreEqualTrees(new NamedExpression(null, originalStatement.SelectProjection), sqlStatement.SelectProjection);

                Assert.That(sqlStatement.DataInfo, Is.SameAs(originalStatement.DataInfo));
                Assert.That(sqlStatement.WhereCondition, Is.SameAs(originalStatement.WhereCondition));
            });
            _stageMock.Replay();

            _handler.MoveCurrentStatementToSqlTable(_statementBuilder, _generator, _context, tableGenerator, _stageMock);

            _stageMock.VerifyAllExpectations();

            Assert.That(_statementBuilder.DataInfo, Is.SameAs(originalStatement.DataInfo));
            Assert.That(_statementBuilder.SqlTables[0], Is.SameAs(fakeFromExpressionInfo.SqlTable));
            Assert.That(_statementBuilder.SelectProjection, Is.SameAs(fakeFromExpressionInfo.ItemSelector));

            var mappedItemExpression = _context.GetExpressionMapping(((StreamedSequenceInfo)originalStatement.DataInfo).ItemExpression);

            Assert.That(mappedItemExpression, Is.Not.Null);
            Assert.That(mappedItemExpression, Is.SameAs(fakeFromExpressionInfo.ItemSelector));
        }
        public void HandleResultOperator()
        {
            var itemExpression = Expression.Constant(new Cook());
            var resultOperator = new ContainsResultOperator(itemExpression);
            var sqlStatement   = _sqlStatementBuilder.GetSqlStatement();

            var preparedExpression = Expression.Constant(new Cook(), typeof(Cook));

            _stageMock.Expect(mock => mock.PrepareResultOperatorItemExpression(itemExpression, _context)).Return(preparedExpression);
            _stageMock.Replay();

            _handler.HandleResultOperator(resultOperator, _sqlStatementBuilder, _generator, _stageMock, _context);

            _stageMock.VerifyAllExpectations();

            Assert.That(_sqlStatementBuilder.DataInfo, Is.TypeOf(typeof(StreamedScalarValueInfo)));
            Assert.That(((StreamedScalarValueInfo)_sqlStatementBuilder.DataInfo).DataType, Is.EqualTo(typeof(Boolean)));

            var expectedExpression = new SqlInExpression(preparedExpression, new SqlSubStatementExpression(sqlStatement));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, _sqlStatementBuilder.SelectProjection);
        }
        public void VisitMainFromClause_CreatesFromExpression()
        {
            var preparedSqlTable           = SqlStatementModelObjectMother.CreateSqlTable();
            var preparedFromExpressionInfo = new FromExpressionInfo(
                preparedSqlTable, new Ordering[] { }, new SqlTableReferenceExpression(preparedSqlTable), null);

            _stageMock
            .Expect(
                mock => mock.PrepareFromExpression(
                    Arg <Expression> .Matches(e => e == _mainFromClause.FromExpression),
                    Arg <ISqlPreparationContext> .Matches(c => c != _context),
                    Arg <Func <ITableInfo, SqlTable> > .Is.Anything))
            .Return(preparedFromExpressionInfo);

            _stageMock.Replay();

            _visitor.VisitMainFromClause(_mainFromClause, _queryModel);
            _stageMock.VerifyAllExpectations();

            Assert.That(_visitor.SqlStatementBuilder.SqlTables, Is.EqualTo(new[] { preparedSqlTable }));
            Assert.That(
                ((SqlTableReferenceExpression)_visitor.Context.GetExpressionMapping(new QuerySourceReferenceExpression(_mainFromClause))).SqlTable,
                Is.SameAs(preparedSqlTable));
        }