legacy Session and SessionFactory

Topics: Core, General, Troubleshooting
Nov 5, 2013 at 10:58 AM
In writing a unit test for an Orchard module, I would like to use the previously stored data present in Orchard's database.

I have a module in orchard that is storing non content data in two tables (UserRecord, AccessRecord). This two tables are populated with data by using a migration class. I can read these data from Microsoft SQL Server Management Studio.

The problem now is that I want to access to this data also from my unit test.

The key part in configuring my unit test is to open a session (Issession).
I do this by creating a new session factory with:

ISessionFactory _sessionFactory = DataUtility.CreateSessionFactory(
and then opening a session with

ISession _session = _sessionFactory.OpenSession();

As I understood, the problem is that creating a sesssion factory means creating a temporary empty database -> no previous data.

Of course, I could popolate the temporary database before any test call but this seems a little messy.
Is there a way to open the same session as in Orchard?

Nov 6, 2013 at 5:26 AM
Mmmh, a unit test doing database access? You should not do that. Instead, mock what you need to mock so that what you are unit testing gets test data without database access.
Nov 6, 2013 at 7:23 AM
A straightforward way to test DB-accessing code is to derive your test from DatabaseEnabledTestBase. It contains the necessary plumbing and configuration and will use a temp DB.

As Bertrand says using a real DB for testing is a bad idea (data there is not reliably constant and you don't want to modify the real DB either) and the way to go is to build up the whole DB when you begin your test.
Nov 7, 2013 at 6:22 AM
I would not call that "the way to go", no ;)