{"id":789,"date":"2012-01-10T22:41:00","date_gmt":"2012-01-10T22:41:00","guid":{"rendered":"http:\/\/mooredynasty.net\/?p=789"},"modified":"2014-12-18T22:42:07","modified_gmt":"2014-12-18T22:42:07","slug":"simple-database-level-unit-test-process","status":"publish","type":"post","link":"https:\/\/mooredynasty.net\/index.php\/2012\/01\/simple-database-level-unit-test-process\/","title":{"rendered":"Simple Database-level Unit Test Process"},"content":{"rendered":"<p>Unit testing of database-related code has long been problematic for the ESI team.&#160; Using Entity Framework Code First, EF Migrations, and SQL Server Compact Edition we may be able to do a much better job in upcoming projects.<\/p>\n<p>Essentially the demonstrated in this article scheme is this: When a set of tests is executed, a SQL CE database is created and populated with the data needed to drive those tests.&#160; Then the tests are executed and evaluated and the database is discarded.&#160; <\/p>\n<p><strong>Nuget<\/strong><\/p>\n<p>This post assumes you have Visual Studio 2010 installed on your machine and have the NuGet extension installed.&#160; If the latter is not true, <a href=\"http:\/\/docs.nuget.org\/docs\/start-here\/installing-nuget\">see the instructions and get that done<\/a>.<\/p>\n<p>Also, you must have <a href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?id=17876\">SQL Server Compact Edition 4.0<\/a> installed.<\/p>\n<p>Given that, let\u2019s proceed.<\/p>\n<p><strong>Data Layer and NuGet<\/strong><\/p>\n<p>The first step is to create a data layer project in your solution.&#160; This is just a simple class library like the ones found in other ESI projects.<\/p>\n<p>Let\u2019s use NuGet to bring Microsoft Entity Framework (EF) 4.2 into the solution.&#160; Right-click on your data project\u2019s References node and pull up the NuGet Package Manager:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-2-BC3945F37FE6.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb6C769D89A26A.png\" width=\"244\" height=\"120\" \/><\/a><\/p>\n<p>Find the Entity Framework project and install it.<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle8df439-4-.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTMLe8df439\" border=\"0\" alt=\"SNAGHTMLe8df439\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle8df439_thumb-1-.png\" width=\"613\" height=\"410\" \/><\/a><\/p>\n<p>Next, find and install the Beta version of Entity Framework Migrations:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle8fc406-6-.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTMLe8fc406\" border=\"0\" alt=\"SNAGHTMLe8fc406\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle8fc406_thumb-3-.png\" width=\"610\" height=\"408\" \/><\/a><\/p>\n<p>The last NuGet package we want to install is SQL Server Compact Edition:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle9314a5-5-.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SNAGHTMLe9314a5\" border=\"0\" alt=\"SNAGHTMLe9314a5\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/snaghtmle9314a5_thumb-2-.png\" width=\"612\" height=\"409\" \/><\/a><\/p>\n<p><strong>Core Shared Modules<\/strong><\/p>\n<p>We\u2019re going to create a simple Employee table in the EF \u201cCode First\u201d style, so we will have neither a database project with a SQL table object or an EF object model with generated class definitions.<\/p>\n<p>Instead, we\u2019re going to have a plain-old-CLR-object (POCO) class of our own to act as the model for the table.&#160; Since this class will be used to move data around layers of the solution, let\u2019s create another new class library to contain it.&#160; Let\u2019s call this the \u201ccore\u201d library.<\/p>\n<p>Then add a class called EmployeeDTO to the core library:<\/p>\n<pre>&#160;<\/pre>\n<pre><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-6-877CBC68C81F.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb-2-B464D733686E.png\" width=\"332\" height=\"141\" \/><\/a><\/pre>\n<pre>&#160;<\/pre>\n<p>(Yes, putting this class in a separate library is overkill for this example, but in a production-grade application, you\u2019ll want to have a core library for these and other classes. Think of extension methods, etc., as well.)<\/p>\n<pre> <\/pre>\n<p><strong>Data Layer Functionality<\/strong><\/p>\n<p>With our POCO in place we can create our application\u2019s data context object:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-17-7B821FF7028D.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb-9-05B370A5F03C.png\" width=\"455\" height=\"220\" \/><\/a><\/p>\n<p>(In the DataContext class, the OnModelCreating method is used to map the context\u2019s Employees property to the Employee table in our database.)<\/p>\n<p>Next, we\u2019ll create an \u201cemployee migration\u201d class in our data project\u2019s Migrations folder (which was created by NuGet when we installed the Migrations package):<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-32-.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb-18-4B0171353431.png\" width=\"414\" height=\"271\" \/><\/a><\/p>\n<p>This class is used by EF Migrations to create the actual database table in the SQL CE database.&#160; In this simple example, pay particular attention to the Id column, which is declared as a SQL Identity.&#160; This will be used later in the testing process.<\/p>\n<p><strong>A Basic Test<\/strong><\/p>\n<p>Let\u2019s create a 3rd project in our solution for testing purposes, then add a basic unit test class like this one:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-33-2649B6E579A2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb-19-7161190B78FB.png\" width=\"360\" height=\"358\" \/><\/a><\/p>\n<p>The \u201cCan_insert\u201d test is perfectly clear, but where does the DataContext object come from?&#160; To instantiate our one-off SQL CE database instance, we need to create a custom database factory.<\/p>\n<p><strong>Creating a Database Instance<\/strong><\/p>\n<p>In an EF application, the database context acts as a wrapper around a connection to the database.&#160; Before we can get a connection in our test library, we need to migrate our database schema to SQL CE.&#160; <\/p>\n<p>Our test data context factory does that, then returns a data context to the test:<\/p>\n<p><a href=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image-34-.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/bcs.system.tamus.edu\/images\/MarcBlogs\/image_thumb-20-20984296161D.png\" width=\"515\" height=\"285\" \/><\/a><\/p>\n<p><strong>Dissecting the Test<\/strong><\/p>\n<p>The unit test we wrote a moment ago is no different than any other.&#160; It arranges, acts, and asserts as usual.&#160; The only difference is that the test class\u2019 initialization method has created a scratch database for us to work with.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>When the test(s) are run, the database is created and made available to the test via the context object, then the data operation is invoked.<\/p>\n<p>It\u2019s easy to imagine a more complex test doing additional arrangement to set up a particular testing scenario, then executing the required business rule code and evaluating the results.<\/p>\n<p>It\u2019s also easy to imagine using the database generation to create development and test databases, particularly during the early phases of a project.<\/p>\n<p>This project is in TFS under the $\/DeveloperTools\/ProofOfConcept\/DbUnitTesting folder.<\/p>\n<p><strong>Primary Reference<\/strong><\/p>\n<p><a href=\"http:\/\/iamnotmyself.com\/2011\/12\/18\/test-driven-evolutionary-design-with-entity-framework\/\">http:\/\/iamnotmyself.com\/2011\/12\/18\/test-driven-evolutionary-design-with-entity-framework\/<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/NotMyself\/SignMeUp\">https:\/\/github.com\/NotMyself\/SignMeUp<\/a><\/p>\n<p><strong>Other References<\/strong><\/p>\n<p><a href=\"http:\/\/docs.nuget.org\/docs\/start-here\/managing-nuget-packages-using-the-dialog\">http:\/\/docs.nuget.org\/docs\/start-here\/managing-nuget-packages-using-the-dialog<\/a><\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/adonet\/archive\/2011\/11\/29\/code-first-migrations-beta-1-released.aspx\">http:\/\/blogs.msdn.com\/b\/adonet\/archive\/2011\/11\/29\/code-first-migrations-beta-1-released.aspx<\/a><\/p>\n<p><a href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?id=17876\">http:\/\/www.microsoft.com\/download\/en\/details.aspx?id=17876<\/a><\/p>\n<p><a href=\"http:\/\/books.google.com\/books?id=ENaambUXoSAC&amp;pg=PA172&amp;lpg=PA172&amp;dq=ef+migrations+example&amp;source=bl&amp;ots=n2eT6qdVjb&amp;sig=e1bGAKYQAH7uaEt34qBRiqHpjMw&amp;hl=en&amp;sa=X&amp;ei=JkUMT-OVFsfKsQKviZnqBQ&amp;ved=0CCQQ6AEwAQ#v=onepage&amp;q=ef%20migrations%20example&amp;f=false\">http:\/\/books.google.com\/books?id=ENaambUXoSAC&amp;pg=PA172&amp;lpg=PA172&amp;dq=ef+migrations+example&amp;source=bl&amp;ots=n2eT6qdVjb&amp;sig=e1bGAKYQAH7uaEt34qBRiqHpjMw&amp;hl=en&amp;sa=X&amp;ei=JkUMT-OVFsfKsQKviZnqBQ&amp;ved=0CCQQ6AEwAQ#v=onepage&amp;q=ef%20migrations%20example&amp;f=false<\/a><\/p>\n<p><a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2010\/12\/08\/announcing-entity-framework-code-first-ctp5-release.aspx\">http:\/\/weblogs.asp.net\/scottgu\/archive\/2010\/12\/08\/announcing-entity-framework-code-first-ctp5-release.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing of database-related code has long been problematic for the ESI team.&#160; Using Entity Framework Code First, EF Migrations, and SQL Server Compact Edition we may be able to do a much better job in upcoming projects. Essentially the demonstrated in this article scheme is this: When a set &hellip; <\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":1,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":790,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/posts\/789\/revisions\/790"}],"wp:attachment":[{"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mooredynasty.net\/index.php\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}