NHibernate problems with deleting records from In-Memory SQLite database

I'm trying to wrap my head around session management in NHibernate. I'm not 100% sure if I'm doing it the right way. So here's my problem. I'm unit testing NHibernate repositories for database operations (Add, update, delete, and load). I've been able to get my unit test working for Add, update, and load, but deleting records is giving me a lot of trouble.

Here are my gists for the source code:

NHibernate Helper for DB config and mapping

Doctors Class Mapping

Doctors Repository for DB operations

Unit Test for Doctors Repository DB operations

Now I think the problem lies in the code for CanDeleteDoctor() test:

[Test]
    public void CanDeleteDoctor()
    {
        using (DoctorsRepository repo = new DoctorsRepository())
        {
            try
            {
                repo.BeginTransaction();
                repo.Save(doctors[0]);
                Assert.IsNotNull(repo.GetByName(doctors[0].Name));
                repo.Delete(doctors[0]);
                Assert.IsNull(repo.GetByName(doctors[0].Name));
            }
            catch (Exception ex)
            {
                repo.RollbackTransaction();
                Assert.Fail(ex.Message + "\n" + ex.StackTrace);
            }
        }
    }

Whenever I run this test, I get an exception that says:

Test 'PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor' failed: 
  Expected: null
  But was:  <PrototypeSLM.Entities.Doctors>

   at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String   message, Object[] args)
   at NUnit.Framework.Assert.IsNull(Object anObject)
   at PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor() in c:\Users\Craig_2\Documents\Visual Studio 2012\Projects\PrototypeSLM\PrototypeSLM.Tests\Tests\DoctorsRepositoryTest.cs:line 100
Tests\DoctorsRepositoryTest.cs(105,0): at PrototypeSLM.Tests.DoctorsRepositoryTest.CanDeleteDoctor()

I have an feeling that this has something to do with how I do my session management on an In-Memory database? I tried adding a _session.Flush() in the Delete method in the DoctorsRepository, but it hasn't fixed the issue. I got a new exception that said "cannot access a disposed object" after flushing the session.

Any hints to help me figure out this problem?

Answers


I fixed it by changing the argument in the method repo.Delete(x) in CanDeleteDoctor() test from:

repo.Delete(doctors[0]);

to

repo.Delete(repo.GetByName(doctors[0].Name));

I think the issue is that the doctors[0]'s Id did not match the Id property of the Doctors record from the database. So the repository ended up deleting nothing. That was quite tricky to conclude.

My tests are 100% passed after this change.

EDIT: if anyone knows what actually happened here, I'd love to hear an explanation.


Need Your Help

How to fetch Column Names from Table?

sql oracle oracle11g

I am trying to fetch column names form Table in Oracle. But I am not getting Column Names.