Saturday, September 19, 2015

Unit Testing with NDBUnit and Identity Column

NDBUnit offers a great way to test your database related functionalities. While performing automated unit testing with databases, libraries like NDBUnit are very handy and they do an amazing job.

If you would like to learn more about them and get started, do visit their GitHub page here - https://github.com/NDbUnit/NDbUnit


In this article, I am specifically talking about a common use case where we need to test the database functionality for a table having identity set to true. In that case, you will need to turn it on in your setup method like this

INDbUnitTest dbUnitTest = new NDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString);
_mySqlDatabase.ReadXmlSchema(TestUtil.ESMSDBXsd.GetCompletePath());
dbUnitTest.PerformDbOperation(DbOperationFlag.InsertIdentity);
In my case, even after doing this, it wasn't inserting identity and was always inserting -1 in the primary key column. After doing a lot of research, I realized that I had to explicitly set it up once again in the XSD as well. If you open your XSD in visual studio, locate the particular table and hit F4 (i.e. go to properties window), you should see all of the properties of that field. We are mainly interested in AutoIncrementSeed And AutoIncrementStep properties which should be set to same value as that of your database (typically 1). Ideally this value should get set automatically when you generate XSD, but it appears that in one of my table that wasn't happening which caused me to spend some time to get around this and finally found the solution as mentioned above.


No comments:

Post a Comment

Thank you for your feedback. I will be glad to know, if this post helped you.