public void Update_CaseWhenPair_ChangeThen ()
    {
      var caseWhenPair = new SqlCaseExpression.CaseWhenPair (_predicate1, _value1);

      var result = caseWhenPair.Update (_predicate1, _value2);

      Assert.That (result, Is.Not.SameAs (caseWhenPair));
      Assert.That (result.When, Is.SameAs (_predicate1));
      Assert.That (result.Then, Is.SameAs (_value2));
    }
    public void VisitSqlCaseExpression_NoElse ()
    {
      var case1 = new SqlCaseExpression.CaseWhenPair (Expression.Constant (true), Expression.Constant (true));
      var case2 = new SqlCaseExpression.CaseWhenPair (Expression.Constant (false), Expression.Constant (false));
      var expression = new SqlCaseExpression (typeof (bool?), new[] { case1, case2 }, null);

      var result = _valueRequiredVisitor.VisitSqlCaseExpression (expression);

      var expectedCase1 = new SqlCaseExpression.CaseWhenPair (
          Expression.Equal (Expression.Constant (1), new SqlLiteralExpression (1)), new SqlConvertedBooleanExpression (Expression.Constant (1)));
      var expectedCase2 = new SqlCaseExpression.CaseWhenPair (
          Expression.Equal (Expression.Constant (0), new SqlLiteralExpression (1)), new SqlConvertedBooleanExpression (Expression.Constant (0)));
      var expectedExpression = new SqlCaseExpression (typeof (bool?), new[] { expectedCase1, expectedCase2 }, null);
      SqlExpressionTreeComparer.CheckAreEqualTrees (expectedExpression, result);
    }
    public void VisitSqlCaseExpression_WithElse ()
    {
      var case1 = new SqlCaseExpression.CaseWhenPair (new SqlCustomTextExpression ("test1", typeof (bool)), new SqlCustomTextExpression ("value1", typeof (int)));
      var case2 = new SqlCaseExpression.CaseWhenPair (new SqlCustomTextExpression ("test2", typeof (bool)), new SqlCustomTextExpression ("value2", typeof (int)));
      var elseCase = new SqlCustomTextExpression ("elseValue", typeof (int));
      var expression = new SqlCaseExpression (typeof (int), new[] { case1, case2 }, elseCase);

      SqlGeneratingExpressionVisitor.GenerateSql (expression, _commandBuilder, _stageMock);

      Assert.That (_commandBuilder.GetCommandText (), Is.EqualTo ("CASE WHEN test1 THEN value1 WHEN test2 THEN value2 ELSE elseValue END"));
    }
    public void VisitSqlCaseExpression_RequiresSingleValue_InElse ()
    {
      var entity = SqlStatementModelObjectMother.CreateSqlEntityDefinitionExpression (typeof (Cook));
      var nonEntity = Expression.Constant (null, typeof (Cook));

      var case1 = new SqlCaseExpression.CaseWhenPair (Expression.Constant (true), nonEntity);
      var elseCase = entity;
      var expression = new SqlCaseExpression (typeof (Cook), new[] { case1 }, elseCase);

      Assert.That (
          () => _valueRequiredVisitor.VisitSqlCaseExpression (expression),
          Throws.TypeOf<NotSupportedException> ().With.Message.EqualTo (
              "Cannot use an entity expression ('[t0]' of type 'Cook') in a place where SQL requires a single value."));
    }