public void NonConstructor_WithNullTestName_Throws() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = null; // Act _ = new TestDatabaseConfiguration(configKey, contextType, testName); }
public void NonConstructor_WithNullContextType_Throws() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = null; string testName = nameof(NonGenericConstructor_AssignsValuesProvided); // Act _ = new TestDatabaseConfiguration(configKey, contextType, testName); }
public void Constructor_WithNullConfiguration_Throws() { // Arrange TestDatabaseConfiguration dbConfig = null; // Act _ = new DbContextSeedBuilder(dbConfig); // Assert Assert.Fail("Expected exception to be thrown."); }
protected virtual void SeedDatabase(TestServer server, TestDatabaseConfiguration databaseConfiguration, DbContext dbContext) { // Resolve a context seeder and seed the database with the individual seeders IDataContextSeeder contextSeeder = server.Host.Services.GetRequiredService <IDataContextSeeder>(); IEntitySeeder[] entitySeeders = databaseConfiguration.SeedBuilder.GetEntitySeeders(); contextSeeder.SeedDataContext(dbContext, entitySeeders); // Always run the callback seeder last so that individual tests have the ability to replace data // inserted via seeders. databaseConfiguration.DatabaseSeeder?.DynamicInvoke(dbContext); }
public void WithSeedData_ReturnsBuilder() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(Constructor_AssignsTestConfiguration); var dbConfig = new TestDatabaseConfiguration(configKey, contextType, testName); // Act var seedBuilder = new DbContextSeedBuilder(dbConfig).WithSeedData <MockSeeder>(); // Assert Assert.IsNotNull(seedBuilder); }
public void Constructor_AssignsTestConfiguration() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(Constructor_AssignsTestConfiguration); var dbConfig = new TestDatabaseConfiguration(configKey, contextType, testName); // Act var seedBuilder = new DbContextSeedBuilder(dbConfig); // Assert Assert.AreEqual(dbConfig, seedBuilder.TestConfiguration); }
public void RetainDatabase_UpdatesTestConfiguration() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(Constructor_AssignsTestConfiguration); var dbConfig = new TestDatabaseConfiguration(configKey, contextType, testName); // Act var seedBuilder = new DbContextSeedBuilder <DbContext>(dbConfig) .RetainDatabase(); // Assert Assert.IsTrue(seedBuilder.TestConfiguration.RetainDatabase); }
public void GenericConstructor_AssignsValuesProvided() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(NonGenericConstructor_AssignsValuesProvided); // Act var dbConfig = new TestDatabaseConfiguration <DbContext>(configKey, testName); // Assert Assert.AreEqual(configKey, dbConfig.ConfigurationConnectionStringKey); Assert.AreEqual(contextType, dbConfig.DbContextType); Assert.AreEqual(testName, dbConfig.ExecutingTest); }
public void WithSeedData_AssignsSeederCallback() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(Constructor_AssignsTestConfiguration); var dbConfig = new TestDatabaseConfiguration(configKey, contextType, testName); Action <DbContext> callback = context => { }; // Act var seedBuilder = new DbContextSeedBuilder <DbContext>(dbConfig) .WithSeedData(callback); // Assert Assert.AreEqual(callback, seedBuilder.TestConfiguration.DatabaseSeeder); }
public void GetEntitySeeders_ReturnsRegisteredSeeders() { // Arrange string configKey = "ConnectionStrings:Default"; Type contextType = typeof(DbContext); string testName = nameof(Constructor_AssignsTestConfiguration); var dbConfig = new TestDatabaseConfiguration(configKey, contextType, testName); // Act var seedBuilder = new DbContextSeedBuilder(dbConfig).WithSeedData <MockSeeder>(); IEntitySeeder[] seeders = seedBuilder.GetEntitySeeders(); // Assert Assert.AreEqual(1, seeders.Length); Assert.AreEqual(typeof(MockSeeder), seeders.First().GetType()); }
public DbContextSeedBuilder <TContext> WithDataContext <TContext>(string configurationConnectionStringKey, string connectionStringDatabaseKey, [CallerMemberName] string executingTest = "") where TContext : DbContext { if (string.IsNullOrEmpty(configurationConnectionStringKey)) { throw new ArgumentNullException(nameof(configurationConnectionStringKey), "You must provide the fully qualified IConfiguration Key used to discover the connection string value in the configuration system."); } var dbConfig = new TestDatabaseConfiguration <TContext>(configurationConnectionStringKey, executingTest) { // If this specific DBContext can't use the Factory database key, then we use the DBContext specific key. ConnectionStringDatabaseKey = string.IsNullOrEmpty(connectionStringDatabaseKey) ? this.connectionStringDatabaseKey : connectionStringDatabaseKey }; var seedBuilder = new DbContextSeedBuilder <TContext>(dbConfig); this.databaseConfigurations.Add(dbConfig); return(seedBuilder); }
public IEntitySeeder GetDataContextSeedData <TContext, TEntitySeeder>() where TContext : DbContext where TEntitySeeder : IEntitySeeder { TestDatabaseConfiguration dbConfig = this.databaseConfigurations .First(config => config.DbContextType == typeof(TContext)); if (dbConfig == null) { throw new InvalidOperationException($"The {typeof(TContext).FullName} DbContext is not registered with this factory instance. You can register it with {nameof(WithDataContext)}()"); } IEntitySeeder entitySeeder = dbConfig.SeedBuilder .GetEntitySeeders() .FirstOrDefault(seeder => seeder.GetType() == typeof(TEntitySeeder)); if (entitySeeder == null) { throw new InvalidOperationException($"The {typeof(TEntitySeeder).FullName} has not been registered for the DbContext {typeof(TContext).FullName}. You need to add the seeder to the context registration for this Factory instance."); } return(entitySeeder); }
protected virtual void RenameConnectionStringDatabase(IWebHostBuilder hostBuilder, TestDatabaseConfiguration databaseConfiguration) { hostBuilder.ConfigureAppConfiguration((hostContext, configBuilder) => { IConfiguration configuration = configBuilder.Build(); string connectionString = configuration.GetConnectionString(databaseConfiguration.ConfigurationConnectionStringKey); if (string.IsNullOrEmpty(connectionString)) { connectionString = configuration[databaseConfiguration.ConfigurationConnectionStringKey]; } string dbName = this.GetDatabaseNameFromConnectionString(connectionString, databaseConfiguration.ConnectionStringDatabaseKey); string newDbName = this.CreateDatabaseNameForTest(dbName); connectionString = this.ReplaceDatabaseNameOnConnectionString(newDbName, connectionString, databaseConfiguration.ConnectionStringDatabaseKey); string configKey = $"ConnectionStrings:{databaseConfiguration.ConfigurationConnectionStringKey}"; if (string.IsNullOrEmpty(configKey)) { configKey = databaseConfiguration.ConfigurationConnectionStringKey; } var connectionStringConfig = new Dictionary <string, string>() { { configKey, connectionString } }; configBuilder.AddInMemoryCollection(connectionStringConfig); }); }
protected virtual void CreateDatabase(TestDatabaseConfiguration configuration, DbContext context) { context.Database.EnsureCreated(); }
internal DbContextSeedBuilder(TestDatabaseConfiguration databaseConfiguration) : base(databaseConfiguration) { }
internal DbContextSeedBuilder(TestDatabaseConfiguration databaseConfiguration) { this.TestConfiguration = databaseConfiguration ?? throw new ArgumentNullException(nameof(databaseConfiguration), $"You must pass in an instance of the {typeof(TestDatabaseConfiguration).Name}"); databaseConfiguration.SeedBuilder = this; }