3

I am going through some code from an open source example of a DDD project DDD Sample.Net and came across an 'interesting' SQLite base test fixture (full code in the link).

Whereas I normally use a connection string for SQLite:

"Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1")

This author is using a DbFile with it (which he sets up / deletes each test):

"Data Source={0};Version=3;New=True;", DatabaseFile)

What is the advantage of doing it this way?

Cheers,
Berryl

EDIT

The reason I suspect the may be an advantage is because the balance of code in that class (below) suggests that the author is neither unsophisticated nor naive. The advantage wouldn't likely be performance. Am guessing it might make SQLite more reliable across test runs, but that's why I asked :--)

  protected IDisposable Scope(bool transactional)
  {
     return new ScopeImpl(SessionFactory, transactional);
  }

  protected IDisposable Scope(bool transactional, string description)
  {
     Console.WriteLine(description);
     return Scope(transactional);
  }

  private class ScopeImpl : IDisposable
  {
     private readonly ISessionFactory _sessionFactory;

     public ScopeImpl(ISessionFactory sessionFactory, bool transactional)
     {
        _sessionFactory = sessionFactory;
        ISession session = _sessionFactory.OpenSession();
        if (transactional)
        {
           session.BeginTransaction();
        }
        CurrentSessionContext.Bind(session);
     }

     public void Dispose()
     {
        ISession session = CurrentSessionContext.Unbind(_sessionFactory);
        if (!IsInExceptionContext())
        {               
           if (session.Transaction != null)
           {
              session.Transaction.Commit();
              session.Transaction.Dispose();
           }               
        }
        session.Close();            
     }

     /// <summary>
     /// Checks if current code is running in finally block ater throwing exception.
     /// </summary>         
     private static Boolean IsInExceptionContext()
     {
        return Marshal.GetExceptionPointers() != IntPtr.Zero || Marshal.GetExceptionCode() != 0;
     }
2
  • 2
    It may be that the author didn't realise you could run SQLite in memory. There's no real advantage, infact I would be willing to say it's a disadvantage having to write the setup/teardown. Commented Jul 26, 2011 at 3:23
  • I'd consider any test that actually accesses a database (especially creating a fresh database each time the test is run) to be an integration test rather than a unit test. Commented Jul 26, 2011 at 3:57

1 Answer 1

2

I think the advantage is that with file based SqLite you can open different sessions on the same file, so to get access to data inserted in a previous session in subsequent one (e.g. for asserting), whereas the in-memory db works in a single session, at least in the FluentNHibernate way of configuring things. Plus, if you delete the file only at test setup, you still have the file on disk after the last test and take a look at it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.