/// <summary>
 /// Registers a provider to use in PostgreSQL databases in unit tests. Use the [PostgreSqlTest]-attribute on class- or method-level to use that provider.
 /// </summary>
 /// <param name="configuration">The config where the provider should be registered on.</param>
 /// <param name="connectionString">The connection string points to the postgres database. You don't know the name of the test DB in this context. The provider takes care of that.</param>
 /// <param name="dbNamePrefix">All test databases are created with the prefix (ie. `YourProjectTest`). Use the branch name or sth. like that when running in a CI pipeline.</param>
 /// <param name="templateDb">When a template is defined, the test database will initially be copied from it (ie. `YourProjectTest_Template`). When a template is set, migrations won't be executed.
 /// The template is expected to be up to date. Use <see cref="PostgreSqlUtil" /> for template creation support.</param>
 /// <param name="seed">The seed that should be executed when this provider is used.</param>
 /// <param name="npgsqlOptions">
 ///     The options builder for Npgsql if the context requires one. Gets used in DbContextOptionsBuilder{TDbContext}().UseNpgsql().
 ///     Example: o => o.UseNodaTime().
 ///     Note: UseNpgSql() has already been called on this builder.</param>
 /// <param name="dbContextOptions">The <see cref="DbContextOptionsBuilder{TDbContext}"/> which can be used for further configuration. Example: o => o.AddInterceptors(...).</param>
 public static DatabaseFixtureConfiguration <TDbContext> UsePostgreSqlDatabase <TDbContext>(
     this DatabaseFixtureConfiguration <TDbContext> configuration,
     string connectionString,
     string dbNamePrefix,
     string?templateDb            = null,
     Func <TDbContext, Task>?seed = null,
     Action <NpgsqlDbContextOptionsBuilder>?npgsqlOptions            = null,
     Action <DbContextOptionsBuilder <TDbContext> >?dbContextOptions = null)
     where TDbContext : DbContext
 {
     configuration.RegisterProvider(typeof(PostgreSqlTestAttribute),
                                    attr =>
     {
         var attribute = (PostgreSqlTestAttribute)attr;
         return(new PostgreSqlDatabaseProvider <TDbContext>(connectionString, dbNamePrefix, templateDb, attribute.EnableLogging, seed, npgsqlOptions, dbContextOptions));
     });
     return(configuration);
 }
Exemplo n.º 2
0
    protected override void RegisterCoreDependencies(Container container)
    {
        base.RegisterCoreDependencies(container);

        //Configuration for the database providers. Usually global, but may be overridden by specific test fixtures.
        container.Register(() =>
        {
            var configuration = new DatabaseFixtureConfiguration <TDbContext>();
            ConfigureDatabaseProviders(configuration);
            return(configuration);
        },
                           Lifestyle.Singleton);

        //The database provider attribute used on the test
        container.RegisterTestScoped(() =>
        {
            if (container.IsVerifying)
            {
                return(new NoDatabaseAttribute());
            }

            var configuration     = container.GetInstance <DatabaseFixtureConfiguration <TDbContext> >();
            var providerAttribute = DatabaseTestContext.CurrentProviderAttribute.Value ?? configuration.DefaultProviderAttribute;
            if (providerAttribute == null)
            {
                throw new InvalidOperationException("Could not determine database provider. No DatabaseProviderAttribute is set on the class or the method. No default attribute is configured.");
            }

            if (configuration.ReplaceProvider != null)
            {
                providerAttribute = configuration.ReplaceProvider(providerAttribute);
            }

            return(providerAttribute);
        });

        //The database provider instance itself that manages db creation, seeding, deletion and the options injected into the dbContext.
        container.RegisterTestScoped(() =>
        {
            var configuration = container.GetInstance <DatabaseFixtureConfiguration <TDbContext> >();
            var attribute     = container.GetInstance <DatabaseProviderAttribute>();

            var attributeType = attribute.GetType();
            if (!configuration !.ProviderFactories.ContainsKey(attributeType))
            {
                throw new InvalidOperationException($"Cannot resolve provider for attribute {attributeType.FullName}. No provider is registered for it.");
            }

            var providerFactory = configuration.ProviderFactories[attributeType];
            var provider        = providerFactory(attribute);
            return(provider);
        });

        //The db context options given by the database provider
        container.RegisterTestScoped(() =>
        {
            var provider = container.GetInstance <ITestDatabaseProvider <TDbContext> >();
            return(provider.GetContextOptions());
        });

        //The db context
        container.Register <TDbContext>(Lifestyle.Scoped);
    }
Exemplo n.º 3
0
 protected abstract void ConfigureDatabaseProviders(DatabaseFixtureConfiguration <TDbContext> configuration);
Exemplo n.º 4
0
 /// <summary>
 /// Registers a provider to use in memory databases in unit tests. Use the [InMemoryTest]-attribute on class- or method-level to use that provider.
 /// </summary>
 public static DatabaseFixtureConfiguration <TDbContext> UseInMemoryDatabase <TDbContext>(this DatabaseFixtureConfiguration <TDbContext> configuration, Func <TDbContext, Task>?seed = null)
     where TDbContext : DbContext
 {
     configuration.RegisterProvider(typeof(InMemoryTestAttribute), _ => new InMemoryDatabaseProvider <TDbContext>(seed));
     return(configuration);
 }