Having to flush the session for unit tests

Topics: Customizing Orchard, Troubleshooting
Oct 25, 2013 at 5:09 PM
Edited Oct 25, 2013 at 5:12 PM
We have custom ContentParts. A few are being updated, created and selected during one method call. All works well if we use the application via browser (as intended): the method gets the right ContentParts.

Yet, when testing the same method via unit test we get old ContentParts. After we inserted a
after the update and creation method and before the get method the unit test works fine. Our unit test is not really a unit test, but rather a integration test. We create a SessionFactory for a clean database, and register all the necessary services so the test can persist, update and get object to and from the database (SQL-CE).

Of course we would like to avoid the flush call. Can somebody from experience tell us, what we may be doing wrong? What we should look into?

Here is where we create the session factory for the tests:
        public static ISessionFactory CreateSessionFactory(string fileName, params Type[] types) {
            //var persistenceModel = AutoMap.Source(new Types(types))
            //    .Alterations(alt => AddAlterations(alt, types))
            //    .Conventions.AddFromAssemblyOf<DataModule>();
            var persistenceModel = AbstractDataServicesProvider.CreatePersistenceModel(
                    types.Select(t => new RecordBlueprint { TableName = "Test_" + t.Name, Type = t }).ToList());
            var persistenceConfigurer = new SqlCeDataServicesProvider(fileName).GetPersistenceConfigurer(true /*createDatabase*/);
            ((MsSqlCeConfiguration) persistenceConfigurer).ShowSql();

            var mappingsPath = Path.Combine(Environment.CurrentDirectory, "_Mappings_");

            return Fluently.Configure()
                .Mappings(m => m.AutoMappings.Add(persistenceModel))
                    c => {
                        // This is to work around what looks to be an issue in the NHibernate driver:
                        // When inserting a row with IDENTITY column, the "SELECT @@IDENTITY" statement
                        // is issued as a separate command. By default, it is also issued in a separate
                        // connection, which is not supported (returns NULL).
                        c.SetProperty("connection.release_mode", "on_close");
                        new SchemaExport(c).Create(false /*script*/, true /*export*/);
Oct 26, 2013 at 7:49 AM
Did you look at existing tests, such as BodyPartTests in Orchard.Core.Tests?
That particular test derives from DatabaseEnabledTestsBase, so you don't have to setup and tear down the database yourself.
It also demonstrates a part being created and then queried from.
Marked as answer by latentlone on 11/1/2013 at 2:43 AM
Nov 1, 2013 at 9:43 AM
Thank you, Sipke.

What I found out, is that we did not use a Transaction (IsolationLevel.ReadCommitted). With the Transaction in place the problem was gone :).
I was not familiar with transaction, but now I read a little documentation on them, and think I understand roughly why it matters to use them, and why it solved our problem.

(Until code review we will probably further use our own DatabasEnabledTestsBase and DataUtility, because we made some changes. And we avoid changing Orchard code where easily possible.)