public void Equals_EqualStatementsWithAllMembers ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var isDistinctQuery = BooleanObjectMother.GetRandomBoolean();
      var selectProjection = Expression.Constant (1);
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var ordering = new Ordering (Expression.Constant ("ordering"), OrderingDirection.Asc);
      var whereCondition = Expression.Constant (true);
      var topExpression = Expression.Constant ("top");
      var rowNumberSelector = Expression.Constant ("selector");
      var currentRowNumberOffset = Expression.Constant (1);
      var groupByExpression = Expression.Constant ("group");

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable },
          whereCondition,
          groupByExpression,
          new[] { ordering },
          topExpression,
          isDistinctQuery,
          rowNumberSelector,
          currentRowNumberOffset);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable },
          whereCondition,
          groupByExpression,
          new[] { ordering },
          topExpression,
          isDistinctQuery,
          rowNumberSelector,
          currentRowNumberOffset);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.True);
    }
    public void RecalculateDataInfo_WithOtherDataInfo_ReturnsSameDataInfo ()
    {
      var statementBuilder = new SqlStatementBuilder ();

      var previousSelectProjection = Expression.Constant ("test");
      var originalDataInfo = new TestStreamedValueInfo(typeof(string));
      statementBuilder.DataInfo = originalDataInfo;
      statementBuilder.SelectProjection = new SqlColumnDefinitionExpression (typeof (int), "c", "Length", false);

      statementBuilder.RecalculateDataInfo (previousSelectProjection);

      Assert.That (statementBuilder.DataInfo, Is.SameAs (originalDataInfo));
    }
    public void GetSqlStatement_CheckProperties ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (Cook));
      var topExpression = ExpressionHelper.CreateExpression();
      var isDistinctQuery = BooleanObjectMother.GetRandomBoolean();
      var selectProjection = new AggregationExpression (typeof (int), Expression.Constant (1), AggregationModifier.Min);
      var whereCondition = Expression.Constant (true);
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var ordering = new Ordering (Expression.Constant ("order"), OrderingDirection.Desc);
      var rowNumberSelector = Expression.Constant ("selector");
      var currentRowNumberOffset = Expression.Constant (1);
      var groupExpression = Expression.Constant ("group");

      var statementBuilder = new SqlStatementBuilder
                             {
                                 DataInfo = dataInfo,
                                 TopExpression = topExpression,
                                 IsDistinctQuery = isDistinctQuery,
                                 SelectProjection = selectProjection,
                                 SqlTables = { sqlTable },
                                 WhereCondition = whereCondition,
                                 RowNumberSelector = rowNumberSelector,
                                 CurrentRowNumberOffset = currentRowNumberOffset,
                                 GroupByExpression = groupExpression,
                                 Orderings = { ordering }
                             };

      var sqlStatement = statementBuilder.GetSqlStatement();

      Assert.That (sqlStatement.DataInfo, Is.SameAs (dataInfo));
      Assert.That (sqlStatement.TopExpression, Is.SameAs (topExpression));
      Assert.That (sqlStatement.IsDistinctQuery, Is.EqualTo (isDistinctQuery));
      Assert.That (sqlStatement.SelectProjection, Is.SameAs (selectProjection));
      Assert.That (sqlStatement.SqlTables[0], Is.SameAs (sqlTable));
      Assert.That (sqlStatement.Orderings[0], Is.SameAs (ordering));
      Assert.That (sqlStatement.WhereCondition, Is.SameAs (whereCondition));
      Assert.That (sqlStatement.RowNumberSelector, Is.SameAs (rowNumberSelector));
      Assert.That (sqlStatement.CurrentRowNumberOffset, Is.SameAs (currentRowNumberOffset));
      Assert.That (sqlStatement.GroupByExpression, Is.SameAs (groupExpression));
    }
    public void ToString_MandatoryProperties ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);

      var sqlStatement = new SqlStatement (dataInfo, selectProjection, new SqlTable[0], null, null, new Ordering[0], null, false, null, null);

      var result = sqlStatement.ToString();

      Assert.That (result, Is.EqualTo ("SELECT 1"));
    }
    public void ToString_SingleTable ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);

      var sqlStatement = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable },
          null,
          null,
          new Ordering[0],
          null,
          false,
          null,
          null);

      var result = sqlStatement.ToString ();

      Assert.That (result, Is.EqualTo ("SELECT 1 FROM [CookTable] [c]"));
    }
    public void Equals_DifferentDataInfo ()
    {
      var dataInfo1 = new TestStreamedValueInfo (typeof (int));
      var dataInfo2 = new TestStreamedValueInfo (typeof (char));
      var selectProjection = Expression.Constant (1);

      var sqlStatement1 = new SqlStatement (
          dataInfo1,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo2,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void ToString_AllProperties ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlTable1 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var sqlTable2 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Kitchen), "KitchenTable", "k"), JoinSemantics.Left);
      var ordering = new Ordering (Expression.Constant ("ordering"), OrderingDirection.Asc);
      var whereCondition = Expression.Constant (true);
      var topExpression = Expression.Constant (10);
      var groupExpression = Expression.Constant ("group");

      var sqlStatement = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable1, sqlTable2 },
          whereCondition,
          groupExpression,
          new[] { ordering },
          topExpression,
          true,
          null,
          null);

      var result = sqlStatement.ToString();

      Assert.That (
          result,
          Is.EqualTo (
              "SELECT DISTINCT TOP (10) 1 FROM [CookTable] [c], [KitchenTable] [k] WHERE True GROUP BY \"group\" ORDER BY \"ordering\" ASC"));
    }
    public void Equals_DifferentCurrentRowNumberOffset ()
    {
      var currentRowNumberOffset1 = Expression.Constant (1);
      var currentRowNumberOffset2 = Expression.Constant (2);
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          currentRowNumberOffset1);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          currentRowNumberOffset2);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void Equals_DifferentGroupByExpression ()
    {
      var groupByExpression1 = Expression.Constant ("group1");
      var groupByExpression2 = Expression.Constant ("group2");
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          groupByExpression1,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          groupByExpression2,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void Equals_DifferentDistinctCondition ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var isDistinctQuery = BooleanObjectMother.GetRandomBoolean();

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          isDistinctQuery,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          !isDistinctQuery,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void Equals_DifferentRowNumberSelector ()
    {
      var rowNumberSelector1 = Expression.Constant ("selector1");
      var rowNumberSelector2 = Expression.Constant ("selector2");
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          rowNumberSelector1,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          rowNumberSelector2,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void DifferentAggregationModifier ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var selectProjectionWithCountAggregation = new AggregationExpression (typeof (int), selectProjection, AggregationModifier.Count);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjectionWithCountAggregation,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void Equals_DifferentOrderings ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var ordering1 = new Ordering (Expression.Constant ("ordering1"), OrderingDirection.Asc);
      var ordering2 = new Ordering (Expression.Constant ("ordering2"), OrderingDirection.Desc);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new[] { ordering1 },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new[] { ordering2 },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void Equals_DifferentSqlTables ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlTable1 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var sqlTable2 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Kitchen), "KitchenTable", "k"), JoinSemantics.Inner);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable1 },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new[] { sqlTable2 },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.False);
    }
    public void ToString_AllProperties ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlTable1 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);
      var sqlTable2 = new SqlTable (new ResolvedSimpleTableInfo (typeof (Kitchen), "KitchenTable", "k"), JoinSemantics.Left);
      var ordering = new Ordering (Expression.Constant ("ordering"), OrderingDirection.Asc);
      var whereCondition = Expression.Constant (true);
      var topExpression = Expression.Constant (10);
      var groupExpression = Expression.Constant ("group");

      var builder = new SqlStatementBuilder
                    {
                        DataInfo = dataInfo,
                        SelectProjection = selectProjection,
                        SqlTables = { sqlTable1, sqlTable2 },
                        Orderings = { ordering },
                        WhereCondition = whereCondition,
                        TopExpression = topExpression,
                        IsDistinctQuery = true,
                        GroupByExpression = groupExpression
                    };

      var result = builder.ToString();

      Assert.That (
          result,
          Is.EqualTo (
              "SELECT DISTINCT TOP (10) 1 FROM [CookTable] [c], [KitchenTable] [k] WHERE True GROUP BY \"group\" ORDER BY \"ordering\" ASC"));
    }
    public void Equals_ObjectIsNotASqlStatement ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlStatement = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement.Equals (new object()), Is.False);
    }
    public void ToString_SingleTable ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);
      var sqlTable = new SqlTable (new ResolvedSimpleTableInfo (typeof (Cook), "CookTable", "c"), JoinSemantics.Inner);

      var builder = new SqlStatementBuilder
      {
        DataInfo = dataInfo,
        SelectProjection = selectProjection,
        SqlTables = { sqlTable },
      };

      var result = builder.ToString ();

      Assert.That (result, Is.EqualTo ("SELECT 1 FROM [CookTable] [c]"));
    }
    public void Equals_EqualStatementsWithMandatoryMembers ()
    {
      var dataInfo = new TestStreamedValueInfo (typeof (int));
      var selectProjection = Expression.Constant (1);

      var sqlStatement1 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      var sqlStatement2 = new SqlStatement (
          dataInfo,
          selectProjection,
          new SqlTable[] { },
          null,
          null,
          new Ordering[] { },
          null,
          false,
          null,
          null);

      Assert.That (sqlStatement1.Equals (sqlStatement2), Is.True);
    }