Fast clean up NHibernate database

I use NHibernate for ORM and everything works fine.

Now I started writing some unit tests (using the database, I don’t want to put a lot of effort into abstracting this, I know that it is not perfect, but it works ..).

I need to be sure that the database is completely empty for some tests. I can, of course, create the entire database. But this seems to be too much, and I think it will take more time.

Is there a DELETE_ALL command that clears all tables I can use in NHibernate?

Chris

EDIT: A short update, I decided to go for SQLite, no problem, to change this with NHibernate. There are some pitfalls, I use this configuration and it works. Otherwise, you may get "table not found" errors due to nHibernate closing the connection during the session, resulting in a "lost" database ...

For your convenience: copy and paste ...

.Database(SQLiteConfiguration.Standard.ConnectionString("Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;") .Raw("connection.release_mode", "on_close")) .Mappings(obj => obj.AutoMappings.Add(_config.APModel)); 
+4
source share
5 answers

Remove and recreate the database. You can use schemaexport:

 var export = new SchemaExport(config); export.Drop(false, true); export.Create(true, true); 

Sql lite in memory is faster for tests than a "normal" database, but the disadvantage is that the sql-lite dialect may be different from the sql dialect in production.

+9
source

I would recommend you check out ndbunit . This is not an NHibernate specification, but I have used it to test NHibernate projects in the past, and it works well. In principle, it provides functions for cleaning the database, pre-filling it with test data or restoring it to known states after each test. You just need to provide the XSD database schema and possibly some XML data for prefilling.

I believe that I first saw this on the series Summer of NHibernate , so check them out to see it in use.

+2
source

You do not write unit tests (i.e. tests that test a single block), you write integration tests in which units interact (i.e. with your database).

Part of your test infrastructure can run a sql script that does one of the following:

  • Remove db and recreate.
  • Trim all tables.

Ideally, you want to work a bit on abstracting db, especially since you have NH, which makes it a lot easier than some other frameworks.

+1
source

Use an in-memory database such as SQLite and configure the necessary data in it before each test. The initial setup takes a little time, but each test passes very quickly after that, and you can make sure that you start from scratch. Ayende has several blog posts on how to set it up.

0
source

Just in case if Drop / Create DB does not suit your needs (for example, if db contains an object that NHibernate does not know about, for example, SP, functions, etc.), you can always make a backup point with an empty database and after you "the testing done is simply restored to this point

0
source

Source: https://habr.com/ru/post/1313524/


All Articles