Example #1
0
        public void Constructor_WithNullConfiguration_Throws()
        {
            // Arrange
            TestDatabaseConfiguration dbConfig = null;

            // Act
            _ = new DbContextSeedBuilder(dbConfig);

            // Assert
            Assert.Fail("Expected exception to be thrown.");
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }