Fluent NHibernate - PersistenceSpecification HiLo Schemes

Not sure I'm asking the right question, so please bear with me! NHibernate noob bit.

We use Fluent NH and have the following identifier generation scheme for all tables

public class IdGenerationConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        var where = string.Format("TableKey = '{0}'", instance.EntityType.Name);
        instance.GeneratedBy.HiLo("HiloPrimaryKeys", "NextHighValue", "1000", x => x.AddParam("where", where));
    }
}

We have an SQL script that generates a table HiloPrimaryKeysand parses it with data that runs during deployment. This works fine.

Now I'm trying to write unit tests to test our persistence level, ideally using SQLite in the memory configuration for speed. This is how I configure NH for tests:

[SetUp]
public void SetupContext()
{
    config = new SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .Raw("hibernate.generate_statistics", "true");

    var nhConfig = Fluently.Configure()
            .Database(PersistenceConfigurer)
            .Mappings(mappings =>
                 mappings.FluentMappings.AddFromAssemblyOf<DocumentMap>()
            .Conventions.AddFromAssemblyOf<IdGenerationConvention>());

    SessionSource = new SessionSource(nhConfig);
    Session = SessionSource.CreateSession();
    SessionSource.BuildSchema(Session);
}

The problem is that I don’t know how to tell NHibernate about our script deployment so that it generates the correct schema and seed data during the tests.

, , PersistenceSpecification:

[Test]
public void ShouldAddDocumentToDatabaseWithSimpleValues()
{
    new PersistenceSpecification<Document>(Session)
            .CheckProperty(x => x.CreatedBy, "anonymous")
            .CheckProperty(x => x.CreatedOn, new DateTime(1954, 12, 23))
            .CheckProperty(x => x.Reference, "anonymous")
            .CheckProperty(x => x.IsMigrated, true)
            .CheckReference(x => x.DocumentType, documentType)
            .VerifyTheMappings();
}

:

TestCase ... failed: 
Execute
NHibernate.Exceptions.GenericADOException: 
        could not get or update next value[SQL: ] 
        ---> System.Data.SQLite.SQLiteException: SQLite error
        no such column: TableKey

, , script .

? Google-, , .

+5
3

, script . :

var config = new SQLiteConfiguration()
        .InMemory()
        .ShowSql()
        .Raw("hibernate.generate_statistics", "true");

var nhConfig = Fluently.Configure()
        .Database(config)
        .Mappings(mappings =>
             mappings.FluentMappings.AddFromAssemblyOf<DocumentMap>()
        .Conventions.AddFromAssemblyOf<IdGenerationConvention>());

var SessionSource = new SessionSource(nhConfig);
var Session = SessionSource.CreateSession();
SessionSource.BuildSchema(Session);

// run the deployment script
var deploymentScriptQuery = Session.CreateSQLQuery("ALTER TABLE HiloPrimaryKeys ADD COLUMN TableKey VARCHAR(255); INSERT INTO HiloPrimaryKeys (TableKey, NextHighValue) values ('Document', 1);");
deploymentScriptQuery.ExecuteUpdate();

script ..

FNH . , , , , . . , .

EDIT: , , . ...

+4

: NHibernate...

... script ( ) ( execute/Process.Start) script . , , .

0

SQL script, HiloPrimaryKeys , . .

Can you create an object to be displayed that represents this HiloPrimaryKeys table and populate this table before starting the tests? You can put this in a base class that inherits all your other tests so you don't have to add this to every testing class.

This is similar to Brian’s decision, but instead this table will be created when you do the automation, like the rest of your tables.

0
source

All Articles