public void VisitMemberExpression_WithInnerSqlSubStatementExpression() { var selectProjection = Expression.Constant(new Cook()); var fakeStatement = SqlStatementModelObjectMother.CreateSqlStatement( new NamedExpression("test", selectProjection), new SqlTable(new ResolvedSimpleTableInfo(typeof(Cook), "CookTable", "c"), JoinSemantics.Left)); var memberInfo = typeof(Cook).GetProperty("Name"); var queryModel = ExpressionHelper.CreateQueryModel <Cook>(); queryModel.ResultOperators.Add(new FirstResultOperator(false)); var expression = new SubQueryExpression(queryModel); var memberExpression = Expression.MakeMemberAccess(expression, memberInfo); _stageMock .Expect(mock => mock.PrepareSqlStatement(queryModel, _context)) .Return(fakeStatement); _stageMock.Replay(); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); _stageMock.VerifyAllExpectations(); Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression))); var expectedSelectProjection = new NamedExpression("test", Expression.MakeMemberAccess(selectProjection, memberInfo)); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedSelectProjection, ((SqlSubStatementExpression)result).SqlStatement.SelectProjection); Assert.That(((SqlSubStatementExpression)result).SqlStatement.DataInfo, Is.TypeOf(typeof(StreamedSequenceInfo))); Assert.That(((SqlSubStatementExpression)result).SqlStatement.DataInfo.DataType, Is.EqualTo(typeof(IQueryable <>).MakeGenericType(typeof(string)))); }
public void VisitBinaryExpression() { var binaryExpression = Expression.And(Expression.Constant(1), Expression.Constant(1)); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.SameAs(binaryExpression)); }
public void VisitSubqueryExpressionTest_IsCountQuery() { var mainFromClause = ExpressionHelper.CreateMainFromClause <Cook>(); var querModel = ExpressionHelper.CreateQueryModel(mainFromClause); var expression = new SubQueryExpression(querModel); var sqlStatement = SqlStatementModelObjectMother.CreateSqlStatement(); var fakeSqlStatementBuilder = new SqlStatementBuilder(sqlStatement) { SelectProjection = new AggregationExpression(typeof(int), sqlStatement.SelectProjection, AggregationModifier.Count) }; var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement(); _stageMock .Expect(mock => mock.PrepareSqlStatement(querModel, _context)) .Return(fakeSqlStatement); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression))); Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(fakeSqlStatement)); Assert.That( ((AggregationExpression)((SqlSubStatementExpression)result).SqlStatement.SelectProjection).AggregationModifier, Is.EqualTo(AggregationModifier.Count)); }
public void VisitMemberExpression_WithInnerSqlCaseExpression() { var testPredicate1 = Expression.Constant(true); var testPredicate2 = Expression.Constant(false); var value1 = Expression.Constant(new TypeWithMember <int> (1)); var value2 = Expression.Constant(new TypeWithMember <int> (2)); var elseValue = Expression.Constant(new TypeWithMember <int> (3)); var caseExpression = new SqlCaseExpression( typeof(TypeWithMember <int>), new[] { new SqlCaseExpression.CaseWhenPair(testPredicate1, value1), new SqlCaseExpression.CaseWhenPair(testPredicate2, value2) }, elseValue); var memberExpression = Expression.Property(caseExpression, "Property"); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedResult = new SqlCaseExpression( typeof(int), new[] { new SqlCaseExpression.CaseWhenPair(testPredicate1, Expression.Property(value1, "Property")), new SqlCaseExpression.CaseWhenPair(testPredicate2, Expression.Property(value2, "Property")) }, Expression.Property(elseValue, "Property")); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void VisitMemberExpression_WithInnerSqlCaseExpression_NoElse() { var predicate = Expression.Constant(true); var valueTypeValue = Expression.Constant(new TypeWithMember <int> (1)); var nullableValueTypeValue = Expression.Constant(new TypeWithMember <int?> (1)); var referenceTypeValue = Expression.Constant(new TypeWithMember <string> ("hoy")); var valueTypeMemberExpression = CreateMemberExpressionWithInnerSqlCaseExpression <int> (predicate, valueTypeValue, null); var nullableValueTypeMemberExpression = CreateMemberExpressionWithInnerSqlCaseExpression <int?> (predicate, nullableValueTypeValue, null); var referenceTypeMemberExpression = CreateMemberExpressionWithInnerSqlCaseExpression <string> (predicate, referenceTypeValue, null); var valueTypeResult = SqlPreparationExpressionVisitor.TranslateExpression( valueTypeMemberExpression, _context, _stageMock, _methodCallTransformerProvider); var nullableValueTypeResult = SqlPreparationExpressionVisitor.TranslateExpression( nullableValueTypeMemberExpression, _context, _stageMock, _methodCallTransformerProvider); var referenceTypeResult = SqlPreparationExpressionVisitor.TranslateExpression( referenceTypeMemberExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedValueTypeResult = CreateSqlCaseExpressionWithInnerMemberExpressionNoElse <int?> (predicate, valueTypeValue); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedValueTypeResult, valueTypeResult); var expectedNullableValueTypeResult = CreateSqlCaseExpressionWithInnerMemberExpressionNoElse <int?> (predicate, nullableValueTypeValue); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedNullableValueTypeResult, nullableValueTypeResult); var expectedReferenceTypeResult = CreateSqlCaseExpressionWithInnerMemberExpressionNoElse <string> (predicate, referenceTypeValue); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedReferenceTypeResult, referenceTypeResult); }
public void VisitSubqueryExpressionTest_RevisitsResult() { var mainFromClause = ExpressionHelper.CreateMainFromClause <Cook>(); var querModel = ExpressionHelper.CreateQueryModel(mainFromClause); var expression = new SubQueryExpression(querModel); var fakeSqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement()) { TopExpression = null }; fakeSqlStatementBuilder.Orderings.Add(new Ordering(Expression.Constant("order"), OrderingDirection.Asc)); fakeSqlStatementBuilder.SqlTables.Add(SqlStatementModelObjectMother.CreateSqlTable(typeof(Cook))); var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement(); fakeSqlStatementBuilder.Orderings.Clear(); var expectedStatement = fakeSqlStatementBuilder.GetSqlStatement(); _stageMock .Expect(mock => mock.PrepareSqlStatement(querModel, _context)) .Return(fakeSqlStatement); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression))); Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.EqualTo(expectedStatement)); }
public void VisitNotSupportedExpression_ThrowsNotImplentedException() { var expression = new CustomExpression(typeof(int)); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.EqualTo(expression)); }
public void VisitMemberExpression_WithProperty_NotRegistered() { var memberExpression = Expression.Property(Expression.Constant(new TypeWithMember <int> (5)), "Property"); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.SameAs(memberExpression)); }
public void VisitConstantExpression_WithNoCollection_LeavesExpressionUnchanged() { var constantExpression = Expression.Constant("string"); var result = SqlPreparationExpressionVisitor.TranslateExpression(constantExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.SameAs(constantExpression)); }
public void VisitQuerySourceReferenceExpression_CreatesSqlTableReferenceExpression() { var result = SqlPreparationExpressionVisitor.TranslateExpression(_cookQuerySourceReferenceExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlTableReferenceExpression))); Assert.That(((SqlTableReferenceExpression)result).SqlTable, Is.SameAs(_sqlTable)); Assert.That(result.Type, Is.SameAs(typeof(Cook))); }
public void VisitBinaryExpression_QuerySourceReferenceExpressionsOnBothSide() { var binaryExpression = Expression.Equal(_cookQuerySourceReferenceExpression, _cookQuerySourceReferenceExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.AssignableTo(typeof(BinaryExpression))); Assert.That(((BinaryExpression)result).Left, Is.TypeOf(typeof(SqlTableReferenceExpression))); Assert.That(((BinaryExpression)result).Right, Is.TypeOf(typeof(SqlTableReferenceExpression))); }
public void VisitBinaryExpression_ReturnsSqlIsNullExpression_NullLeft() { var leftExpression = Expression.Constant(null); var binaryExpression = Expression.Equal(leftExpression, _cookQuerySourceReferenceExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlIsNullExpression))); Assert.That(((SqlIsNullExpression)result).Expression, Is.TypeOf(typeof(SqlTableReferenceExpression))); }
public void VisitMemberExpression_WithProperty_Registered() { var stringExpression = Expression.Constant("test"); var memberExpression = Expression.Property(stringExpression, "Length"); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedResult = new SqlLengthExpression(stringExpression); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void VisitBinaryExpression_ReturnsSqlIsNotNullExpression_NullRight() { var rightExpression = Expression.Constant(null); var leftExpression = Expression.Constant("1"); var binaryExpression = Expression.NotEqual(leftExpression, rightExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlIsNotNullExpression))); Assert.That(((SqlIsNotNullExpression)result).Expression, Is.SameAs(leftExpression)); }
public void VisitConstantExpression_WithCollection_ReturnsSqlCollectionExpression() { var constantExpression = Expression.Constant(new[] { 1, 2, 3 }); var result = SqlPreparationExpressionVisitor.TranslateExpression(constantExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedExpression = new SqlCollectionExpression( typeof(int[]), new[] { Expression.Constant(1), Expression.Constant(2), Expression.Constant(3) }); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, result); }
public void VisitBinaryExpression_NotEqual_WithNullOnLeftSide() { var leftExpression = Expression.Constant(null); var binaryExpression = Expression.Coalesce(leftExpression, _cookQuerySourceReferenceExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.AssignableTo(typeof(BinaryExpression))); Assert.That(((BinaryExpression)result).Left, Is.TypeOf(typeof(ConstantExpression))); Assert.That(((BinaryExpression)result).Right, Is.TypeOf(typeof(SqlTableReferenceExpression))); }
public void VisitPartialEvaluationExceptionExpression_StripsExceptionAndVisitsInnerExpression() { var evaluatedExpression = Expression.Property(Expression.Constant(""), "Length"); var exception = new InvalidOperationException("What?"); var expression = new PartialEvaluationExceptionExpression(exception, evaluatedExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); var expectedExpression = new SqlLengthExpression(evaluatedExpression.Expression); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, result); }
public void VisitSqlSubStatmentExpression_NoTopExpression_ReturnsSame() { var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook()); builder.Orderings.Clear(); var sqlStatement = builder.GetSqlStatement(); var subStatementExpression = new SqlSubStatementExpression(sqlStatement); var result = SqlPreparationExpressionVisitor.TranslateExpression(subStatementExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.SameAs(subStatementExpression)); }
public void VisitMethodCallExpression() { var method = typeof(string).GetMethod("ToUpper", new Type[] { }); var constantExpression = Expression.Constant("Test"); var methodCallExpression = Expression.Call(constantExpression, method); var result = SqlPreparationExpressionVisitor.TranslateExpression(methodCallExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlFunctionExpression))); Assert.That(((SqlFunctionExpression)result).SqlFunctioName, Is.EqualTo("UPPER")); Assert.That(((SqlFunctionExpression)result).Args[0], Is.SameAs(constantExpression)); Assert.That(((SqlFunctionExpression)result).Args.Count, Is.EqualTo(1)); }
public void VisitNewExpression_NoMembers() { var expression = Expression.New(TypeForNewExpression.GetConstructor(typeof(int)), new[] { Expression.Constant(0) }); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.Not.Null); Assert.That(result, Is.TypeOf(typeof(NewExpression))); Assert.That(result, Is.Not.SameAs(expression)); Assert.That(((NewExpression)result).Arguments.Count, Is.EqualTo(1)); Assert.That(((NewExpression)result).Arguments[0], Is.TypeOf(typeof(NamedExpression))); Assert.That(((NewExpression)result).Members, Is.Null); Assert.That(((NamedExpression)((NewExpression)result).Arguments[0]).Name, Is.EqualTo("m0")); }
public void VisitMethodCallExpression_TransformerNotRegistered_NoObject_WrapsArgumentsIntoNamedExpressions() { var methodCallExpression = Expression.Call( MemberInfoFromExpressionUtility.GetMethod(() => int.Parse("")), Expression.Constant("")); var result = SqlPreparationExpressionVisitor.TranslateExpression(methodCallExpression, _context, _stageMock, _methodCallTransformerProvider); var expected = Expression.Call( MemberInfoFromExpressionUtility.GetMethod(() => int.Parse("")), new[] { new NamedExpression("Arg0", Expression.Constant("")) }); SqlExpressionTreeComparer.CheckAreEqualTrees(expected, result); }
public void VisitBinaryExpression_WithConditionalExpressionInBinaryExpression() { var leftExpression = Expression.Constant("Name"); var testPredicate = Expression.Constant(true); var ifTrueExpression = Expression.Constant("true"); var ifFalseExpression = Expression.Constant("false"); var rightExpression = Expression.Condition(testPredicate, ifTrueExpression, ifFalseExpression); var binaryExpression = Expression.Equal(leftExpression, rightExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(binaryExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedResult = Expression.Equal( leftExpression, SqlCaseExpression.CreateIfThenElse(typeof(string), testPredicate, ifTrueExpression, ifFalseExpression)); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void VisitMethodCallExpression_TransformerNotRegistered_WrapsArgumentsIntoNamedExpressions() { var methodCallExpression = Expression.Call( Expression.Constant(0), MemberInfoFromExpressionUtility.GetMethod(() => 0.ToString ("", null)), Expression.Constant(""), Expression.Constant(null, typeof(IFormatProvider))); var result = SqlPreparationExpressionVisitor.TranslateExpression(methodCallExpression, _context, _stageMock, _methodCallTransformerProvider); var expected = Expression.Call( new NamedExpression("Object", Expression.Constant(0)), MemberInfoFromExpressionUtility.GetMethod(() => 0.ToString ("", null)), new[] { new NamedExpression("Arg0", Expression.Constant("")), new NamedExpression("Arg1", Expression.Constant(null, typeof(IFormatProvider))) }); SqlExpressionTreeComparer.CheckAreEqualTrees(expected, result); }
public void VisitConditionalExpression_VisitsSubExpressions() { var testPredicate = Expression.Equal(Expression.Constant("test"), Expression.Constant(null, typeof(string))); var ifTrueExpression = Expression.Equal(Expression.Constant("test"), Expression.Constant(null, typeof(string))); var ifFalseExpression = Expression.Equal(Expression.Constant("test"), Expression.Constant(null, typeof(string))); var conditionalExpression = Expression.Condition(testPredicate, ifTrueExpression, ifFalseExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(conditionalExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlCaseExpression))); var sqlCaseExpression = (SqlCaseExpression)result; Assert.That(sqlCaseExpression.Cases, Has.Count.EqualTo(1)); Assert.That(sqlCaseExpression.Cases[0].When, Is.TypeOf <SqlIsNullExpression>()); Assert.That(sqlCaseExpression.Cases[0].Then, Is.TypeOf <SqlIsNullExpression> ()); Assert.That(sqlCaseExpression.ElseCase, Is.TypeOf <SqlIsNullExpression> ()); }
public void VisitMemberExpression_WithInnerCoalesceExpression_RevisitsResult() { var left = Expression.Constant("left"); var right = Expression.Constant("right"); var coalesceExpression = Expression.Coalesce(left, right); var memberExpression = Expression.Property(coalesceExpression, "Length"); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedResult = SqlCaseExpression.CreateIfThenElse( typeof(int), new SqlIsNotNullExpression(coalesceExpression.Left), new SqlLengthExpression(coalesceExpression.Left), new SqlLengthExpression(coalesceExpression.Right)); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void VisitSqlSubStatmentExpression_HasTopExpression_ReturnsNew() { var builder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatementWithCook()); builder.Orderings.Add(new Ordering(Expression.Constant("order"), OrderingDirection.Asc)); var sqlStatement = builder.GetSqlStatement(); builder.Orderings.Clear(); var expectedStatement = builder.GetSqlStatement(); var subStatementExpression = new SqlSubStatementExpression(sqlStatement); var result = SqlPreparationExpressionVisitor.TranslateExpression(subStatementExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.Not.SameAs(subStatementExpression)); Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression))); Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.EqualTo(expectedStatement)); }
public void VisitConditionalExpression() { var testPredicate = Expression.Constant(true); var ifTrueExpression = Expression.Constant("true"); var ifFalseExpression = Expression.Constant("false"); var conditionalExpression = Expression.Condition(testPredicate, ifTrueExpression, ifFalseExpression); var result = SqlPreparationExpressionVisitor.TranslateExpression(conditionalExpression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlCaseExpression))); var sqlCaseExpression = (SqlCaseExpression)result; Assert.That(sqlCaseExpression.Type, Is.SameAs(typeof(string))); Assert.That(sqlCaseExpression.Cases, Has.Count.EqualTo(1)); Assert.That(sqlCaseExpression.Cases[0].When, Is.EqualTo(testPredicate)); Assert.That(sqlCaseExpression.Cases[0].Then, Is.EqualTo(ifTrueExpression)); Assert.That(sqlCaseExpression.ElseCase, Is.EqualTo(ifFalseExpression)); }
public void VisitMemberExpression_WithInnerSqlCaseExpression_RevisitsResult() { var predicate = Expression.Constant(true); var value = Expression.Constant("value1"); var elseValue = Expression.Constant("value2"); var caseExpression = new SqlCaseExpression(typeof(string), new[] { new SqlCaseExpression.CaseWhenPair(predicate, value) }, elseValue); var memberExpression = Expression.Property(caseExpression, "Length"); var result = SqlPreparationExpressionVisitor.TranslateExpression(memberExpression, _context, _stageMock, _methodCallTransformerProvider); var expectedResult = new SqlCaseExpression( typeof(int), new[] { new SqlCaseExpression.CaseWhenPair(predicate, new SqlLengthExpression(value)) }, new SqlLengthExpression(elseValue)); SqlExpressionTreeComparer.CheckAreEqualTrees(expectedResult, result); }
public void VisitSubqueryExpressionTest_WithSqlTables() { var mainFromClause = ExpressionHelper.CreateMainFromClause <Cook>(); var querModel = ExpressionHelper.CreateQueryModel(mainFromClause); var expression = new SubQueryExpression(querModel); var fakeSqlStatementBuilder = new SqlStatementBuilder(SqlStatementModelObjectMother.CreateSqlStatement()); fakeSqlStatementBuilder.SqlTables.Add(_sqlTable); var fakeSqlStatement = fakeSqlStatementBuilder.GetSqlStatement(); _stageMock .Expect(mock => mock.PrepareSqlStatement(querModel, _context)) .Return(fakeSqlStatement); var result = SqlPreparationExpressionVisitor.TranslateExpression(expression, _context, _stageMock, _methodCallTransformerProvider); Assert.That(result, Is.TypeOf(typeof(SqlSubStatementExpression))); Assert.That(((SqlSubStatementExpression)result).SqlStatement, Is.SameAs(fakeSqlStatement)); }
public void VisitMethodCallExpression_ExpressionPropertiesVisitedBeforeTransformation() { var method = MethodCallTransformerUtility.GetInstanceMethod(typeof(object), "ToString"); var methodCallExpression = Expression.Call(_cookQuerySourceReferenceExpression, method); var transformerMock = MockRepository.GenerateMock <IMethodCallTransformer>(); transformerMock .Expect(mock => mock.Transform(Arg.Is(methodCallExpression))) .Return(methodCallExpression); transformerMock.Replay(); var registry = new MethodInfoBasedMethodCallTransformerRegistry(); registry.Register(method, transformerMock); SqlPreparationExpressionVisitor.TranslateExpression(methodCallExpression, _context, _stageMock, registry); transformerMock.VerifyAllExpectations(); }