Acaz Souza
Acaz Souza

Reputation: 8631

NHibernate/FluentNHibernate and SQLite in memory for mapping test

I'm trying to use SQLite and NHibernate for first time to test my mappings but I'm getting this error:

Test method BMGChip.Tests.clsCorrespondenteMapTest.Can_correctly_map_Correspondente threw exception: 
NHibernate.Exceptions.GenericADOException: could not insert: [BMGChip.NHibernate.Entities.clsCorrespondente][SQL: INSERT INTO CPHSITE12_COR (COR_NOM, COR_EMA, COR_TEL, COR_RUA, COR_NUM, COR_COM, COR_CID, COR_EST, COR_CEP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); select last_insert_rowid()] ---> System.Data.SQLite.SQLiteException: SQLite error
no such table: CPHSITE12_COR

I'm trying to create and drop database for each test method.

My NHibernate configuration:

Public Class clsSessionFactoryBuilder
    Private Shared _sessionFactory As ISessionFactory

    Private Shared Function GetSessionFactory() As ISessionFactory
        If _sessionFactory Is Nothing Then
            _sessionFactory = Fluently.Configure() _
                .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of clsFaleConoscoMap)()) _
                .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()) _
                .ExposeConfiguration(Function(cfg) ExportarSchema(cfg)) _
                .ExposeConfiguration(Function(cfg) cfg.SetProperty("current_session_context_class", "thread_static")) _
                .BuildSessionFactory()
        End If

        Return _sessionFactory
    End Function

    Public Shared Sub OpenSession()
        Dim session As ISession = GetSessionFactory.OpenSession
        CurrentSessionContext.Bind(session)
    End Sub

    Public Shared Function GetCurrentSession() As ISession
        Return GetSessionFactory.GetCurrentSession
    End Function

    Public Shared Sub CloseSession()
        Dim session As ISession = CurrentSessionContext.Unbind(_sessionFactory)

        If session Is Nothing Then Return

        Try
            'session.Transaction.Commit()
        Catch ex As Exception
            'session.Transaction.Rollback()
        Finally
            session.Close()
            session.Dispose()
        End Try
    End Sub

    Private Shared Function ExportarSchema(ByVal configuration As Cfg.Configuration)
        Dim export As New SchemaExport(configuration)

        export.Create(False, True)
        Return Nothing
    End Function
End Class

My test:

<TestMethod()>
Public Sub Can_correctly_map_Correspondente()
    clsSessionFactoryBuilder.OpenSession()

    Dim session As ISession = clsSessionFactoryBuilder.GetCurrentSession()

    With New PersistenceSpecification(Of clsCorrespondente)(session)
        .CheckProperty(Function(c) c.Nome, "Fernanda Moreira")
        .CheckProperty(Function(c) c.Email, "[email protected]")
        .CheckProperty(Function(c) c.Telefone, "(31) 3131-3131")
        .CheckProperty(Function(c) c.Rua, "R. Andaluzita")
        .CheckProperty(Function(c) c.Numero, "775")
        .CheckProperty(Function(c) c.Complemento, "Do lado do Pátio Savassi")
        .CheckProperty(Function(c) c.Cidade, "Belo Horizonte")
        .CheckProperty(Function(c) c.Estado, "MG")
        .CheckProperty(Function(c) c.Cep, "44444-444")
        .VerifyTheMappings()
    End With

    clsSessionFactoryBuilder.CloseSession()
End Sub

What could be?

Upvotes: 1

Views: 2196

Answers (2)

Acaz Souza
Acaz Souza

Reputation: 8631

I managed to make it work. What I realized was that I need to build my schema after calling BuildSessionFactory()

Public Class clsSessionFactoryBuilder
    Private Shared sessionFactory As ISessionFactory
    Private Shared configuration As Cfg.Configuration

    Public Shared Function GetSessionFactory() As ISessionFactory
        If sessionFactory Is Nothing Then
            sessionFactory = Fluently.Configure() _
                .Database(SQLiteConfiguration.Standard.InMemory) _
                .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of clsFaleConoscoMap)()) _
                .ExposeConfiguration(Function(c) c.SetProperty("current_session_context_class", "call")) _
                .ExposeConfiguration(Function(c) c.SetProperty("connection.release_mode", "on_close")) _
                .ExposeConfiguration(Function(c) PersistConfig(c)) _
                .BuildSessionFactory()
        End If

        Return sessionFactory
    End Function

    Public Shared Function OpenSession() As ISession
        Dim session As ISession = GetSessionFactory.OpenSession
        CurrentSessionContext.Bind(session)

        SchemaExport(configuration)

        Return session
    End Function

    Public Shared Function GetCurrentSession() As ISession
        Return GetSessionFactory.GetCurrentSession
    End Function

    Public Shared Sub CloseSession()
        Dim session As ISession = CurrentSessionContext.Unbind(sessionFactory)

        If session Is Nothing Then Return

        session.Close()
        session.Dispose()
    End Sub

    Private Shared Function SchemaExport(ByVal configuration As Cfg.Configuration)
        Dim export As New SchemaExport(configuration)

        export.Execute(False, True, False, sessionFactory.GetCurrentSession.Connection, Nothing)
        Return Nothing
    End Function

    Private Shared Function PersistConfig(ByVal c As Cfg.Configuration)
        configuration = c
    End Function
End Class

Upvotes: 1

Rich Tebb
Rich Tebb

Reputation: 7126

Try calling SchemaExport.Execute after you have created a session, for it to create the tables. Here's an excerpt from my C# unit test code:

new SchemaExport(configuration).Execute(
            false, // Change to true to write DDL script to console
            true,
            false,
            this.Session.Connection,
            null);

Also remember that the SQLIte in-memory configuration is not persistent between sessions, so you will need to execute the schema export for each test (there may be a configuration option to override this, not sure).

Upvotes: 2

Related Questions