public void Deletes_data_from_simple_table(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange database.Insert(new Dog { Name = "Some Name 1", Age = 10 }); database.Insert(new Dog { Name = "Some Name 2", Age = 10 }); database.Insert(new Dog { Name = "Some Name 3", Age = 10 }); database.Insert(new Dog { Name = "Some Name 4", Age = 11 }); // Act DataWiper.ClearAllData(database); // Assert database.Count <Dog>().Should().Be(0); } }
public void Errors_when_an_ignored_table_references_an_unignored_one() { // Arrange var userId = this.database.Insert <int>(new User { Name = "Some Name 1", Age = 10 }); this.database.Insert(new SimpleForeignKey { Name = "Some Name 1", UserId = userId }); var sut = new DataWiper { IgnoredTables = new[] { "dbo.SimpleForeignKeys" } }; // Act Action act = () => sut.ClearAllData(this.database); // Assert act.ShouldThrow <Exception>(); // Cleanup this.database.DeleteAll <SimpleForeignKey>(); }
public void Ignores_tables_in_other_schemas(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange database.Insert(new SchemaOther { Name = "Other" }); string name; switch (dialect) { case SqlServer2012Dialect _: name = "Other.SchemaOther"; break; case PostgreSqlDialect _: name = "other.schemaother"; break; default: throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name); } // Act DataWiper.ClearAllData(database, new HashSet <string> { name }); // Assert database.Count <SchemaOther>().Should().Be(1); } }
public void RevertToSnapshot() { Snapshot now = GetSnapshot(); List <string> changedTables = _latestSnapshot .Where(x => x.Value != now[x.Key]) .Select(x => x.Key) .ToList(); if (!changedTables.Any()) { return; } DataWiper dataWiper = new DataWiper(_connectionString); using (SqlConnection connection = new SqlConnection(_connectionString)) { connection.Open(); SqlConstraints.DisableAllConstraints(connection); foreach (string tableName in changedTables) { dataWiper.Execute(connection, tableName); } DataToSql toSql = new DataToSql(_connectionString, _directory, changedTables); toSql.Execute(connection); SqlConstraints.EnableAllConstraints(connection); } TakeSnapshot(); }
public void Deletes_data_from_cyclic_foreign_keyed_tables(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var a = new CyclicForeignKeyA(); a.Id = database.Insert <int>(a); var b = new CyclicForeignKeyB { ForeignId = a.Id }; b.Id = database.Insert <int>(b); var c = new CyclicForeignKeyC { ForeignId = b.Id }; c.Id = database.Insert <int>(c); a.ForeignId = c.Id; database.Update(a); // Act DataWiper.ClearAllData(database); // Assert database.Count <CyclicForeignKeyA>().Should().Be(0); database.Count <CyclicForeignKeyB>().Should().Be(0); database.Count <CyclicForeignKeyC>().Should().Be(0); } }
public void Ignores_specified_tables(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange database.Insert(new Dog { Name = "Some Name 1", Age = 10 }); string tableName; switch (dialect) { case SqlServer2012Dialect _: tableName = "dbo.Dogs"; break; case PostgreSqlDialect _: tableName = "public.dog"; break; default: throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name); } // Act DataWiper.ClearAllData(database, new HashSet <string> { tableName }); // Assert database.Count <Dog>().Should().Be(1); } }
public void Deletes_data_from_cyclic_foreign_keyed_tables() { // Arrange var a = new CyclicForeignKeyA(); a.Id = this.database.Insert <int>(a); var b = new CyclicForeignKeyB { ForeignId = a.Id }; b.Id = this.database.Insert <int>(b); var c = new CyclicForeignKeyC { ForeignId = b.Id }; c.Id = this.database.Insert <int>(c); a.ForeignId = c.Id; this.database.Update(a); var sut = new DataWiper(); // Act sut.ClearAllData(this.database); // Assert this.database.Count <CyclicForeignKeyA>().Should().Be(0); this.database.Count <CyclicForeignKeyB>().Should().Be(0); this.database.Count <CyclicForeignKeyC>().Should().Be(0); }
private static bool ExecuteInput(ConsoleKeyInfo input, string connectionString, string directory) { System.Console.ForegroundColor = ConsoleColor.Cyan; System.Console.WriteLine(); switch (input.KeyChar) { case '1': System.Console.WriteLine("Storing database \n {0} \nTo directory \n {1}", connectionString, directory); DataToFile dataToFile = new DataToFile(connectionString, directory); dataToFile.Execute(); break; case '2': System.Console.WriteLine("Wiping database \n {0}", connectionString); System.Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("Are you sure you want to continue? Y/N"); ConsoleKeyInfo confirmInfo = System.Console.ReadKey(); System.Console.WriteLine(); System.Console.ForegroundColor = ConsoleColor.Cyan; if (confirmInfo.KeyChar == 'Y' || confirmInfo.KeyChar == 'y') { DataWiper dataWiper = new DataWiper(connectionString); dataWiper.Execute(); } else { System.Console.ForegroundColor = ConsoleColor.Red; System.Console.WriteLine("Aborted"); } break; case '3': System.Console.WriteLine("Deploying data to database \n {0} From \n {1}", connectionString, directory); DataToSql dataToSql = new DataToSql(connectionString, directory); dataToSql.Execute(); break; case '4': return(false); default: System.Console.ForegroundColor = ConsoleColor.Red; System.Console.WriteLine("Incorrect input"); break; } return(true); }
public static IDatabase MakeDatabase(IDialect dialect) { CleanUp(); switch (dialect) { case PostgreSqlDialect _: return(OpenBlankDatabase(PostgresPool, cs => new NpgsqlConnection(cs), PeregrineConfig.Postgres)); case SqlServer2012Dialect _: return(OpenBlankDatabase(SqlServer2012Pool, cs => new SqlConnection(cs), PeregrineConfig.SqlServer2012)); default: throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name); } IDatabase OpenBlankDatabase( ObjectPool <string> pool, Func <string, IDbConnection> makeConnection, PeregrineConfig config) { var pooledConnectionString = pool.Acquire(); try { IDbConnection dbConnection = null; try { dbConnection = makeConnection(pooledConnectionString.Item); dbConnection.Open(); IDbConnection pooledConnection = new PooledConnection <IDbConnection>(pooledConnectionString, dbConnection); var database = new DefaultDatabase(pooledConnection, config); DataWiper.ClearAllData(database); return(database); } catch { dbConnection?.Dispose(); throw; } } catch { pooledConnectionString?.Dispose(); throw; } } }
public void Deletes_data_from_self_referenced_foreign_keyed_tables(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var id = database.Insert <int>(new SelfReferenceForeignKey()); database.Insert(new SelfReferenceForeignKey { ForeignId = id }); // Act DataWiper.ClearAllData(database); // Assert database.Count <SelfReferenceForeignKey>().Should().Be(0); } }
public void Deletes_data_from_self_referenced_foreign_keyed_tables() { // Arrange var id = this.database.Insert <int>(new SelfReferenceForeignKey()); this.database.Insert(new SelfReferenceForeignKey { ForeignId = id }); var sut = new DataWiper(); // Act sut.ClearAllData(this.database); // Assert this.database.Count <SelfReferenceForeignKey>().Should().Be(0); }
public void Can_delete_tables_which_reference_another_twice(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var id = database.Insert <int>(new WipeMultipleForeignKeyTarget { Name = "Other" }); database.Insert(new WipeMultipleForeignKeySource { NameId = id, OptionalNameId = id }); // Act DataWiper.ClearAllData(database); // Assert database.Count <WipeMultipleForeignKeyTarget>().Should().Be(0); } }
public void Deletes_data_from_tables_in_other_schemas(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var otherId = database.Insert <int>(new SchemaOther { Name = "Other" }); database.Insert(new SchemaSimpleForeignKeys { SchemaOtherId = otherId }); // Act DataWiper.ClearAllData(database); // Assert database.Count <SchemaOther>().Should().Be(0); database.Count <SchemaSimpleForeignKeys>().Should().Be(0); } }
public void Deletes_data_from_foreign_keyed_tables() { // Arrange var userId = this.database.Insert <int>(new User { Name = "Some Name 1", Age = 10 }); this.database.Insert(new SimpleForeignKey { Name = "Some Name 1", UserId = userId }); var sut = new DataWiper(); // Act sut.ClearAllData(this.database); // Assert this.database.Count <User>().Should().Be(0); this.database.Count <SimpleForeignKey>().Should().Be(0); }
public void Ignores_tables_in_other_schemas() { // Arrange this.database.Insert(new SchemaOther { Name = "Other" }); var sut = new DataWiper { IgnoredTables = new[] { "Other.SchemaOther" } }; // Act sut.ClearAllData(this.database); // Assert this.database.Count <SchemaOther>().Should().Be(1); }
public void Deletes_data_from_tables_in_other_schemas() { // Arrange var otherId = this.database.Insert <int>(new SchemaOther { Name = "Other" }); this.database.Insert(new SchemaSimpleForeignKeys { SchemaOtherId = otherId }); var sut = new DataWiper(); // Act sut.ClearAllData(this.database); // Assert this.database.Count <SchemaOther>().Should().Be(0); this.database.Count <SchemaSimpleForeignKeys>().Should().Be(0); }
public void Ignores_specified_tables() { // Arrange this.database.Insert(new User { Name = "Some Name 1", Age = 10 }); var sut = new DataWiper { IgnoredTables = new[] { "dbo.Users" } }; // Act sut.ClearAllData(this.database); // Assert this.database.Count <User>().Should().Be(1); }
public void Deletes_data_from_foreign_keyed_tables(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var dogId = database.Insert <int>(new Dog { Name = "Some Name 1", Age = 10 }); database.Insert(new SimpleForeignKey { Name = "Some Name 1", DogId = dogId }); // Act DataWiper.ClearAllData(database); // Assert database.Count <Dog>().Should().Be(0); database.Count <SimpleForeignKey>().Should().Be(0); } }
public void Errors_when_an_ignored_table_references_an_unignored_one(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var dogId = database.Insert <int>(new Dog { Name = "Some Name 1", Age = 10 }); database.Insert(new SimpleForeignKey { Name = "Some Name 1", DogId = dogId }); string tableName; switch (dialect) { case SqlServer2012Dialect _: tableName = "dbo.SimpleForeignKeys"; break; case PostgreSqlDialect _: tableName = "public.simple_foreign_key"; break; default: throw new NotSupportedException("Unknown dialect: " + dialect.GetType().Name); } // Act Action act = () => DataWiper.ClearAllData(database, new HashSet <string> { tableName }); // Assert act.Should().Throw <Exception>(); // Cleanup database.DeleteAll <SimpleForeignKey>(); } }
public void Deletes_data_from_simple_table() { // Arrange this.database.Insert(new User { Name = "Some Name 1", Age = 10 }); this.database.Insert(new User { Name = "Some Name 2", Age = 10 }); this.database.Insert(new User { Name = "Some Name 3", Age = 10 }); this.database.Insert(new User { Name = "Some Name 4", Age = 11 }); var sut = new DataWiper(); // Act sut.ClearAllData(this.database); // Assert this.database.Count <User>().Should().Be(0); }