mercredi 27 janvier 2016

UnitTest FluentNhibernate using PostgreSQLConfiguration

When setting up our new architecture I followed a guide which used NHibernate with MsSql2008 configuration.

We are not using MsSql2008, instead using Postgresql. The configuration for this all works great and it saves to the database etc.

I am trying to write a unit test to test the UoW but I can't get the InMemory configuration to work.

The guide that I followed used this following Provider:

public class InMemoryNHibernateConfigurationProvider : NHibernateConfigurationProvider
{
    public override Configuration GetDatabaseConfiguration()
    {
        var databaseDriver = SQLiteConfiguration.Standard.InMemory().ShowSql();
        return CreateCoreDatabaseConfiguration(databaseDriver);
    }

    public static void InitialiseDatabase(Configuration configuration, ISession session)
    {
        new SchemaExport(configuration).Execute(true, true, false, session.Connection, Console.Out);
    }
}

My standard (Non UnitTest) configuration looks like this:

public abstract class NHibernateConfigurationProvider : INHibernateConfigurationProvider
{
    public abstract Configuration GetDatabaseConfiguration();

    public Configuration CreateCoreDatabaseConfiguration(
        IPersistenceConfigurer databaseDriver,
        Action<Configuration> databaseBuilder = null)
    {
        var fluentConfiguration =
            Fluently.Configure()
            .Database(databaseDriver)                
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Organisation>(new DefaultMappingConfiguration())
                //.Conventions.AddFromAssemblyOf<IdGenerationConvention>()
                 .UseOverridesFromAssemblyOf<OrganisationMappingOverride>()));

        if (databaseBuilder != null)
        {
            fluentConfiguration.ExposeConfiguration(databaseBuilder);
        }

        return fluentConfiguration.BuildConfiguration();
    }
}


public class PostgreSQLServerNHibernateConfigurationProvider : NHibernateConfigurationProvider
{
    private static readonly string NpgsqlConnectionString = ConfigurationManager.ConnectionStrings["ProdDBConnection"].ConnectionString;

    public override Configuration GetDatabaseConfiguration()
    {
        return CreateCoreDatabaseConfiguration(
            PostgreSQLConfiguration.Standard.ConnectionString(NpgsqlConnectionString).
                Dialect("NHibernate.Dialect.PostgreSQL82Dialect").ShowSql(),
            BuildDatabase);
    }

    ....... // Other Methods etc
}

How do I write a InMemoryConfigurationProvider that tests using PostgresqlConfiguration instead of SqlLiteCOnfiguration. PostgresqlConfiguration does not have an InMemory option.

Do I implement a configuration that creates another database and just drop it on teardown? Or is there perhaps another way of doing it?

Aucun commentaire:

Enregistrer un commentaire