开发者

sqlite unit testing with a db file

开发者 https://www.devze.com 2023-03-22 17:17 出处:网络
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).

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;
     }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消