public void ThrowExceptionWhenDroppingWithNullSqlConnection()
        {
            Table      table = new Table("test");
            TableIndex index = new TableIndex("testIndex", table, column);

            Assert.Throws <ArgumentNullException>(() => index.Drop(null));
        }
Example #2
0
        public void CreateIndexProperly(IndexType indexType, bool isUnique, ColumnSort columnSort)
        {
            string indexName = "IX_Test";
            Table  table     = fixture.table;
            List <Tuple <Column, ColumnSort> > indexColumns = new List <Tuple <Column, ColumnSort> >();

            table.Columns.ForEach(c =>
            {
                indexColumns.Add(Tuple.Create(c, columnSort));
                TableIndex index = new TableIndex(indexName, table, indexColumns.ToArray())
                {
                    IsUnique  = isUnique,
                    IndexType = indexType
                };

                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();
                    index.Create(sqlConnection);

                    using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
                    {
                        string sql = $@"
                            SELECT COUNT(*)
                            FROM sys.indexes
                            JOIN sys.index_columns 
                                ON sys.indexes.object_id = sys.index_columns.object_id
                            where name = '{indexName}'";

                        sqlCommand.CommandText = sql;
                        int columnCount        = (int)sqlCommand.ExecuteScalar();

                        Assert.Equal(indexColumns.Count, columnCount);

                        sql = $@"
                            SELECT OBJECT_NAME(sys.indexes.object_id), name, type_desc, is_unique, is_descending_key
                            FROM sys.indexes
                            JOIN sys.index_columns 
                                ON sys.indexes.object_id = sys.index_columns.object_id
                            where name = '{indexName}'";

                        sqlCommand.CommandText = sql;
                        using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                        {
                            while (sqlDataReader.Read())
                            {
                                Assert.Equal(table.Name, sqlDataReader.GetString(0));
                                Assert.Equal(index.Name, sqlDataReader.GetString(1));
                                Assert.Equal(indexType.ToString(), sqlDataReader.GetString(2));
                                Assert.Equal(isUnique, sqlDataReader.GetBoolean(3));
                                Assert.Equal(columnSort == ColumnSort.DESC, sqlDataReader.GetBoolean(4));
                            }
                        }
                    }

                    index.Drop(sqlConnection);
                }
            });
        }