Unit Test of Nhibernate with SQLite Memory Database

Source: Internet
Author: User

The unit test for data access code is in an awkward position. If the operation is not performed for a real database, it cannot capture database-specific errors, such as whether the SQL statement syntax is correct, whether the operation violates the database constraints and whether the transaction is correctly committed. In addition, tests should be isolated. One test cannot affect the data of another test. That is to say, the table structure should be rebuilt before each test is run and the test data should be re-installed. Executing these operations on a real database will make the test a good fit.

Fortunately, SQLite provides a memory database to avoid disk Io and improve performance. A memory database has a very important feature: the database only exists when the connection is opened. Once the connection is closed, the database disappears. This is exactly what we want. The steps for running the test are as follows:

1. open the session in the [testinitialize] method, create the table structure, and install the test data. By default, SQLite Unicode strings are case-sensitive. Therefore, you must specify collate nocase for the text column when creating the table structure.

2. Run the test

3. Close the session in the [testcleanup] method, which will cause the underlying connection to close and the memory database to disappear.

The Code is as follows:

[Testclass] public class mytest {[assemblyinitialize] public static void initassembly (testcontext) {nhibernatehelper. init ();} [assemblycleanup] public static void cleanupassembly () {nhibernatehelper. closesessionfactory ();} protected isession session {Get; private set;} [testinitialize ()] public void mytestinitialize () {try {// 1 Open Session session = nhibernatehelper. opensession (); // 2 create the table nhib.pdf. tool. hbm2ddl. schemaexport export = new nhib.pdf. tool. hbm2ddl. schemaexport (nhibernatehelper. configuration); export. setdelimiter (";"); stringwriter Sw = new stringwriter (); export. execute (false, session. connection, SW); Using (idbcommand cmd = session. connection. createcommand () {// Replace the field Definition Statement cmd. commandtext = RegEx. replace (SW. tostring (), @ "\ s + TEXT \ s +", "Text Collate nocase ", regexoptions. ignorecase | regexoptions. compiled); cmd. executenonquery ();} // 3 create test data using (itransaction Tx = session. begintransaction () {role = new role (); role. name = "Admins"; Session. save (role); Tx. commit ();} // 4 clear the session cache session. clear ();} catch (exception ex) {// if an exception occurs, testcleanup will not be executed, so the resource if (session! = NULL) {session. close ();} Throw;} [testcleanup ()] public void mytestcleanup () {session. close (); Session = NULL;} [testmethod] public void mytestmethod () {using (itransaction Tx = session. begintransaction () {role = session. query <role> (). firstordefault (x => X. name = "Admins"); assert. isnotnull (role); Tx. commit ();}}}

Nhibernate configuration. Note that the connection. release_mode attribute must be set to on_close.

Reference link:

Http://ayende.com/blog/3983/nhibernate-unit-testing

Http://www.sqlite.org/faq.html#q18

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.