Exemplo n.º 1
0
        public void TestSqlStatementBuilderCreateQueryWithWhereAndWithOrderByAndWithTopAndWithHints()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");

            var where = new QueryGroup(new QueryField("Id", 1));
            var orderBy = OrderField.Parse(new
            {
                Field1 = Order.Ascending,
                Field2 = Order.Descending
            });
            var top   = 100;
            var hints = SqlTableHints.NoLock;

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields,
                                                      where : where,
                                                      orderBy: orderBy,
                                                      top: top,
                                                      hints: hints);
            var expected = $"" +
                           $"SELECT TOP (100) [Field1], [Field2], [Field3] " +
                           $"FROM [Table] WITH (NOLOCK) " +
                           $"WHERE ([Id] = @Id) " +
                           $"ORDER BY [Field1] ASC, [Field2] DESC ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 2
0
        public void TestSqlStatementBuilderCreateQueryWithOrderBy()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");
            var orderBy          = OrderField.Parse(new
            {
                Field1 = Order.Ascending,
                Field2 = Order.Descending
            });

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields,
                                                      orderBy: orderBy);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [Table] " +
                           $"ORDER BY [Field1] ASC, [Field2] DESC ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 3
0
        public void ThrowExceptionIfThereAreNoQueryableFields()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <ThrowExceptionIfThereAreNoQueryableFieldsClass>();
            var queryGroup       = (QueryGroup)null;

            // Act/Assert
            statementBuilder.CreateQuery(queryBuilder, queryGroup);
        }
Exemplo n.º 4
0
        public void ThrowExceptionIfAllFieldsWereIgnored()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <ThrowExceptionIfAllFieldsWereIgnoredClass>();
            var queryGroup       = (QueryGroup)null;

            // Act/Assert
            statementBuilder.CreateQuery(queryBuilder, queryGroup);
        }
Exemplo n.º 5
0
        public void ThrowExceptionOnSqlStatementBuilderCreateQueryIfThereAreNoFields()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";

            // Act
            statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                         tableName: tableName,
                                         fields: null);
        }
Exemplo n.º 6
0
        public void ThrowExceptionOnSqlStatementBuilderCreateQueryIfTheTableIsWhitespace()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = " ";
            var fields           = Field.From("Field1", "Field2", "Field3");

            // Act
            statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                         tableName: tableName,
                                         fields: fields);
        }
Exemplo n.º 7
0
        public void ThrowExceptionOnSqlStatementBuilderCreateQueryIfTheOrderFieldIsNotCovered()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");
            var orderBy          = OrderField.Parse(new { Id = Order.Ascending, Field1 = Order.Ascending });

            // Act
            statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                         tableName: tableName,
                                         fields: fields,
                                         orderBy: orderBy);
        }
        public void TestSqlDbProviderCreateQueryWithTop()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestSqlDbProviderCreateQueryWithTopClass>();
            var queryGroup       = (QueryGroup)null;

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup, top: 10);
            var expected = $"" +
                           $"SELECT TOP (10) [Field1], [Field2], [Field3] " +
                           $"FROM [TestSqlDbProviderCreateQueryWithTopClass] ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 9
0
        public void TestWithFieldMapping()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestWithFieldMappingClass>();
            var queryGroup       = (QueryGroup)null;

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field4] " +
                           $"FROM [TestWithFieldMappingClass] ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 10
0
        public void TestSqlDbProviderCreateQueryWithTableHintViaSqlTableHints()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestSqlDbProviderCreateQueryWithTableHintClass>();
            var queryGroup       = (QueryGroup)null;

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup, null, null, SqlTableHints.ReadUncommitted);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [TestSqlDbProviderCreateQueryWithTableHintClass] {SqlTableHints.ReadUncommitted} ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 11
0
        public void TestSqlDbProviderCreateQueryWithTableHint()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestSqlDbProviderCreateQueryWithTableHintClass>();
            var queryGroup       = (QueryGroup)null;

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup, null, null, "WITH (INDEX(ANYINDEX), NOLOCK)");
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [TestSqlDbProviderCreateQueryWithTableHintClass] WITH (INDEX(ANYINDEX), NOLOCK) ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 12
0
        public void TestSqlStatementBuilderCreateQueryWithUnquotedTableSchema()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "dbo.Table";
            var fields           = Field.From("Field1", "Field2", "Field3");

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields);
            var expected = "SELECT [Field1], [Field2], [Field3] FROM [dbo].[Table] ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 13
0
        public void TestWithAnyFieldsAtExpression()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestWithAnyFieldsAtExpressionClass>();
            var expression       = new { AnyField = 1 };
            var queryGroup       = QueryGroup.Parse(expression);

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [TestWithAnyFieldsAtExpressionClass] " +
                           $"WHERE ([AnyField] = @AnyField) ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 14
0
        public void TestWithDescendingOrderFields()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestWithDescendingOrderFieldsClass>();
            var queryGroup       = (QueryGroup)null;
            var orderBy          = OrderField.Parse(new { OrderField = Order.Descending });

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup, orderBy);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [TestWithDescendingOrderFieldsClass] " +
                           $"ORDER BY [OrderField] DESC ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 15
0
        public void TestSqlDbProviderCreateQueryWithUnknownFieldAsPartOfWhereForDynamic()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder <TestSqlDbProviderCreateQueryWithUnknownFieldAsPartOfWhereForDynamicClass>();
            var expression       = new { AnyField = 1 };
            var queryGroup       = QueryGroup.Parse(expression);

            // Act
            var actual   = statementBuilder.CreateQuery(queryBuilder, queryGroup);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [TestSqlDbProviderCreateQueryWithUnknownFieldAsPartOfWhereForDynamicClass] " +
                           $"WHERE ([AnyField] = @AnyField) ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 16
0
        public void TestSqlStatementBuilderCreateQueryWithTop()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");
            var top = 100;

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields,
                                                      top: top);
            var expected = "SELECT TOP (100) [Field1], [Field2], [Field3] FROM [Table] ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void TestSqlStatementBuilderCreateQueryWithHints()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");
            var hints            = SqlTableHints.NoLock;

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields,
                                                      hints: hints);
            var expected = "SELECT [Field1], [Field2], [Field3] FROM [Table] WITH (NOLOCK) ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 18
0
        public void TestSqlStatementBuilderCreateQueryWithWhereExpression()
        {
            // Setup
            var statementBuilder = new SqlStatementBuilder();
            var queryBuilder     = new QueryBuilder();
            var tableName        = "Table";
            var fields           = Field.From("Field1", "Field2", "Field3");

            var where = new QueryGroup(new QueryField("Id", 1));

            // Act
            var actual = statementBuilder.CreateQuery(queryBuilder: queryBuilder,
                                                      tableName: tableName,
                                                      fields: fields,
                                                      where : where);
            var expected = $"" +
                           $"SELECT [Field1], [Field2], [Field3] " +
                           $"FROM [Table] " +
                           $"WHERE ([Id] = @Id) ;";

            // Assert
            Assert.AreEqual(expected, actual);
        }