public void ResolveSubStatementReferenceExpression_CreatesSqlGroupingReferenceExpression_ForSqlGroupingSelectExpression()
        {
            var groupingSelectExpression = new SqlGroupingSelectExpression(
                SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression(typeof(Cook), "key"),
                new NamedExpression("element", Expression.Constant(0)),
                new[] { new NamedExpression("a0", Expression.Constant(1)) });
            var sqlStatement = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement_Resolved(typeof(Cook)))
            {
                SelectProjection = groupingSelectExpression,
                DataInfo         = new StreamedSequenceInfo(typeof(IGrouping <int, int>[]), Expression.Constant(null, typeof(IGrouping <int, int>)))
            }.GetSqlStatement();
            var tableInfo = new ResolvedSubStatementTableInfo("q0", sqlStatement);
            var sqlTable  = new SqlTable(tableInfo, JoinSemantics.Inner);

            var result = SubStatementReferenceResolver.ResolveSubStatementReferenceExpression(groupingSelectExpression, tableInfo, sqlTable, _context);

            Assert.That(result, Is.TypeOf(typeof(SqlGroupingSelectExpression)));

            var referencedKeyExpression         = ((SqlGroupingSelectExpression)result).KeyExpression;
            var expectedReferencedKeyExpression = new NamedExpression("key", new SqlEntityReferenceExpression(
                                                                          typeof(Cook),
                                                                          "q0",
                                                                          null,
                                                                          (SqlEntityExpression)groupingSelectExpression.KeyExpression));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedReferencedKeyExpression, referencedKeyExpression);

            var referencedElementExpression         = ((SqlGroupingSelectExpression)result).ElementExpression;
            var expectedReferencedElementExpression = new NamedExpression("element", new SqlColumnDefinitionExpression(typeof(int), "q0", "element", false));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedReferencedElementExpression, referencedElementExpression);

            Assert.That(((SqlGroupingSelectExpression)result).AggregationExpressions.Count, Is.EqualTo(1));
            var referencedAggregationExpression         = ((SqlGroupingSelectExpression)result).AggregationExpressions[0];
            var expectedReferencedAggregationExpression = new NamedExpression("a0", new SqlColumnDefinitionExpression(typeof(int), "q0", "a0", false));

            SqlExpressionTreeComparer.CheckAreEqualTrees(expectedReferencedAggregationExpression, referencedAggregationExpression);

            Assert.That(_context.GetReferencedGroupSource(((SqlGroupingSelectExpression)result)), Is.SameAs(sqlTable));
        }
Esempio n. 2
0
        public void ProcessNames_SqlGroupingSelectExpression()
        {
            var keyExpression            = new NamedExpression("key", Expression.Constant("key"));
            var elementExpression        = new NamedExpression("element", Expression.Constant("element"));
            var aggregationExpression    = new NamedExpression("a0", Expression.Constant("aggregation"));
            var groupingSelectExpression = new SqlGroupingSelectExpression(keyExpression, elementExpression, new[] { aggregationExpression });
            var expression = new NamedExpression("outer", groupingSelectExpression);
            var sqlTable   = SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook));

            _context.AddGroupReferenceMapping(groupingSelectExpression, sqlTable);

            var expectedResult = new SqlGroupingSelectExpression(
                new NamedExpression("outer_key", Expression.Constant("key")),
                new NamedExpression("outer_element", Expression.Constant("element")),
                new[] { new NamedExpression("outer_a0", Expression.Constant("aggregation")) });

            var result = _namedExpressionCombiner.ProcessNames(expression);

            SqlExpressionTreeComparer.CheckAreEqualTrees(result, expectedResult);
            Assert.That(_context.GetReferencedGroupSource(((SqlGroupingSelectExpression)result)), Is.SameAs(sqlTable));
        }