public void ServerDatabaseIsPresentAndCorrectButHasTablesInIt() { var server = new ExternalDatabaseServer(CatalogueRepository, "Fiction", null); server.Server = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.Name; //server.Database = "FictionalDatabase"; Ignored by the extractor! DiscoveredServerICanCreateRandomDatabasesAndTablesOn.CreateDatabase("FictionalDatabase"); var db = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase("FictionalDatabase"); using (var con = db.Server.GetConnection()) { con.Open(); db.Server.GetCommand("CREATE TABLE Bob(name varchar(10))", con).ExecuteNonQuery(); } try { var destination = new ExecuteFullExtractionToDatabaseMSSql(); destination.PreInitialize(_projectStub, new ThrowImmediatelyDataLoadEventListener()); destination.PreInitialize(_commandStub, new ThrowImmediatelyDataLoadEventListener()); destination.TargetDatabaseServer = server; destination.TableNamingPattern = "$d"; destination.DatabaseNamingPattern = "FictionalDatabase"; var tomemory = new ToMemoryCheckNotifier(new ThrowImmediatelyCheckNotifier()); destination.Check(tomemory); Assert.AreEqual(CheckResult.Warning, tomemory.GetWorst()); db.ExpectTable("Bob").Drop(); } finally { server.DeleteInDatabase(); } }
public void TestConnection(bool explicitClose) { //drop it if it existed if (DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testDbName).Exists()) { DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testDbName).Drop(); } DiscoveredServerICanCreateRandomDatabasesAndTablesOn.CreateDatabase(testDbName); Thread.Sleep(500); ThrowIfDatabaseLock(); var db = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testDbName); ThrowIfDatabaseLock(); using (var con = db.Server.GetConnection()) { con.Open(); //we are currently connected so this should throw Assert.Throws <Exception>(ThrowIfDatabaseLock); } Thread.Sleep(500); if (explicitClose) { SqlConnection.ClearAllPools(); Thread.Sleep(500); Assert.DoesNotThrow(ThrowIfDatabaseLock);//in this case we told .net to clear the pools which leaves the server free of locks/hanging connections } else { Assert.Throws <Exception>(ThrowIfDatabaseLock);//despite us closing the connection and using the 'using' block .net still keeps a connection in sleep state to the server >< } db.Drop(); }
public void ServerDatabaseIsPresentAndCorrect(bool alreadyExists) { var server = new ExternalDatabaseServer(CatalogueRepository, "Fiction", null); server.Server = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.Name; //server.Database = "FictionalDatabase"; Ignored by the extractor! DiscoveredServerICanCreateRandomDatabasesAndTablesOn.CreateDatabase("FictionalDatabase"); Assert.IsTrue(DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase("FictionalDatabase").Exists()); try { var destination = new ExecuteFullExtractionToDatabaseMSSql(); destination.PreInitialize(_projectStub, new ThrowImmediatelyDataLoadEventListener()); destination.PreInitialize(_commandStub, new ThrowImmediatelyDataLoadEventListener()); destination.TargetDatabaseServer = server; destination.TableNamingPattern = "$d"; if (alreadyExists) { destination.DatabaseNamingPattern = "FictionalDatabase"; //database that exists } else { destination.DatabaseNamingPattern = "Fictional$nDatabase"; //database does not exist (but server does) } var tomemory = new ToMemoryCheckNotifier(new ThrowImmediatelyCheckNotifier()); destination.Check(tomemory); Assert.AreEqual(alreadyExists? CheckResult.Warning: CheckResult.Success, tomemory.GetWorst()); } finally { server.DeleteInDatabase(); } }
// This no longer copies between servers, but the original test didn't guarantee that would happen anyway public void CloneDatabaseAndTable() { string testLiveDatabaseName = TestDatabaseNames.GetConsistentName("TEST"); var testDb = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testLiveDatabaseName); var raw = DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testLiveDatabaseName + "_RAW"); foreach (DiscoveredDatabase db in new[] { raw, testDb }) { if (db.Exists()) { foreach (DiscoveredTable table in db.DiscoverTables(true)) { table.Drop(); } db.Drop(); } } DiscoveredServerICanCreateRandomDatabasesAndTablesOn.CreateDatabase(testLiveDatabaseName); Assert.IsTrue(testDb.Exists()); testDb.CreateTable("Table_1", new[] { new DatabaseColumnRequest("Id", "int") }); //clone the builder var builder = new SqlConnectionStringBuilder(DiscoveredServerICanCreateRandomDatabasesAndTablesOn.Builder.ConnectionString) { InitialCatalog = testLiveDatabaseName }; var dbConfiguration = new HICDatabaseConfiguration(new DiscoveredServer(builder), null, new ServerDefaults(CatalogueRepository)); var cloner = new DatabaseCloner(dbConfiguration); try { var cloneDb = cloner.CreateDatabaseForStage(LoadBubble.Raw); //confirm database appeared Assert.IsTrue(DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(testLiveDatabaseName + "_RAW").Exists()); //now create a catalogue and wire it SetUp to the table TEST on the test database server Catalogue cata = SetupATestCatalogue(builder, testLiveDatabaseName, "Table_1"); //now clone the catalogue data structures to MachineName foreach (TableInfo tableInfo in cata.GetTableInfoList(false)) { cloner.CreateTablesInDatabaseFromCatalogueInfo(new ThrowImmediatelyDataLoadEventListener(), tableInfo, LoadBubble.Raw); } Assert.IsTrue(raw.Exists()); Assert.IsTrue(raw.ExpectTable("Table_1").Exists()); } finally { cloner.LoadCompletedSoDispose(ExitCodeType.Success, new ThrowImmediatelyDataLoadEventListener()); while (toCleanUp.Count > 0) { try { toCleanUp.Pop().DeleteInDatabase(); } catch (Exception e) { //always clean SetUp everything Console.WriteLine(e); } } } }