{"action":"created","comment":{"url":"https://api.github.com/repos/codurance/site/pulls/comments/82502890","pull_request_review_id":3408779,"id":82502890,"diff_hunk":"@@ -0,0 +1,87 @@\n+---\r\n+layout: post\r\n+name: database-testing-with-tsql \r\n+title: Database Testing With tSQLt\r\n+date: 2016-10-06 10:20:00 +00:00\r\n+author: Halima Koundi\r\n+image:\r\n+   src: /assets/img/custom/blog/db-testing/db-testing.jpg\r\n+tags:\r\n+- ms sql server \r\n+- database\r\n+- practices\r\n+---\r\n+\r\n+\r\n+Evolutionary Database Development, as coined in Martin Fowler’s book Refactoring Databases, is a concept whereby the design of the database is not done upfront, but instead is done in a way that evolves as the software that is relying on the data store does.\r\n+In order to have flexibility and to allow changes in the database to be made safely; you need to have a test suite to run and check for regressions. In this post we will have a look at a testing framework for Microsoft SQL Server databases.\r\n+\r\n+\r\n+##Environment\r\n+\r\n+###Installing MS SQL Server\r\n+\r\n+Get started installing [SQL Server Express](https://www.microsoft.com/en-us/download/details.aspx?id=52679)\r\n+.\r\n+SQL Server Express is a free lightweight version of Microsoft’s SQL Server database.\r\n+\r\n+###Installing MSSQL Server Manager\r\n+\r\n+SQL Server Management studio provides us with a powerful user interface to access, manage and configure our MS SQL database server.\r\n+If you don’t already have it, [install it](https://msdn.microsoft.com/en-us/library/mt238290.aspx). Please note this will take some time.\r\n+\r\n+###Downloading tSQLt\r\n+\r\n+We are going to use the [tSQLt](http://tsqlt.org/) unit testing framework to test our Microsoft SQL Server database.\r\n+We first need to get the [download](http://tsqlt.org/downloads/) from the official website. \r\n+\r\n+The tSQLt [open source](https://github.com/tSQLt-org/tSQLt) framework allows us to run our tests within transactions which makes our tests independant and takes care of cleaning up after it runs. The framework also gives us the ability to isolate our tests through fake tables and stored procedure (SP) spies.\r\n+\r\n+##Running the test sample\r\n+\r\n+Once you have SQL Server Express up and running, and MSSQL Management Studio installed, open the latter and connect to your server.\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-1.png \"\" %}\r\n+\r\n+The tSQLt team provides a quick start example database and a set of tests to help developers get started quickly on how the framework works.\r\n+Let’s try the example together.\r\n+First unzip the tSQLt folder you downloaded, and make sure that CLRs are enabled on your development server. Enabling [CLR integration](https://msdn.microsoft.com/en-us/library/ms254498v=vs.110.aspx) allows us to run managed code, such as C#, on our SQL Server database.\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-2.png \"\" %}\r\n+\r\n+Open the Example.sql file that you will find within the unzipped folder, and execute it in your database server. This will create a test database named `tSQLt_Example`.\r\n+\r\n+Once your database is created, open a new query window on that DB and run the following command:\r\n+`EXEC tSQLt.RunAll`\r\n+This is a command to run all the tests. You should see a failing test in the result screen:\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-3.png \"\" %}\r\n+\r\n+Test case number 11, named test ready for experimentation if 2 particles, is failing. Let’s open the file and see what’s wrong with it; we will need to navigate to the stored procedures folder to find the test.\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-4.png \"\" %}\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-5.png \"\" %}\r\n+\r\n+The failing test is calling `IsExperimentReady` and checks that it returns 1 when the particle table has two particles.\r\n+Looking at the function, it seems that it is not counting the number of rows in the Particle table, and it is not doing the right checks.\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/db-testing-6.png \"\" %}\r\n+\r\n+Once we’ve solved the issue, we can run the tests again and see that they are all passing now.\r\n+\r\n+###What is this test doing and how is the fake table working?\r\n+\r\n+Tests in the tSQLt framework are run within transactions. The function we are testing relies on the Particle table to determine whether the system is ready for experimentation. Looking at our test here, we can see that we are creating a fake table of the `Accelerator.Particle` in which we will insert two rows.\r\n+\r\n+If we have closer at look at the `tSQLt.FakeTable` SP, it is renaming the original table we are faking, and copying its structure into a new table of the original name without applying any of the constraints. \r\n+Then we run the test by calling the function under test, knowing that it should use the table we just faked.\r\n+After the test has run wrapped inside the transaction, this transaction is rolled back. This will revert all the changes made to the mocked table.\r\n+The test results that were stored in temporary fields are then saved in a test result table.\r\n+\r\n+{% img /assets/img/custom/blog/db-testing/TestingWithFakeTables.png \"\" %}\r\n+\r\n+In this first blog post we learned about database testing, and how the tSQLt testing framework works. In the next post, we will go beyond the quick start example and see how to approach legacy transact-SQL, implementing tests into an existing database.\r\n+\r","path":"_posts/2016-10-06-database-testing-with-tsqlt.md","position":85,"original_position":85,"commit_id":"575d3bc93c9fd176af70b0962327638658b90240","original_commit_id":"575d3bc93c9fd176af70b0962327638658b90240","user":{"login":"halimakoundi","id":5450688,"avatar_url":"https://avatars.githubusercontent.com/u/5450688?v=3","gravatar_id":"","url":"https://api.github.com/users/halimakoundi","html_url":"https://github.com/halimakoundi","followers_url":"https://api.github.com/users/halimakoundi/followers","following_url":"https://api.github.com/users/halimakoundi/following{/other_user}","gists_url":"https://api.github.com/users/halimakoundi/gists{/gist_id}","starred_url":"https://api.github.com/users/halimakoundi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halimakoundi/subscriptions","organizations_url":"https://api.github.com/users/halimakoundi/orgs","repos_url":"https://api.github.com/users/halimakoundi/repos","events_url":"https://api.github.com/users/halimakoundi/events{/privacy}","received_events_url":"https://api.github.com/users/halimakoundi/received_events","type":"User","site_admin":false},"body":"they are not indeed.","created_at":"2016-10-08T13:57:28Z","updated_at":"2016-10-08T13:57:28Z","html_url":"https://github.com/codurance/site/pull/410#discussion_r82502890","pull_request_url":"https://api.github.com/repos/codurance/site/pulls/410","_links":{"self":{"href":"https://api.github.com/repos/codurance/site/pulls/comments/82502890"},"html":{"href":"https://github.com/codurance/site/pull/410#discussion_r82502890"},"pull_request":{"href":"https://api.github.com/repos/codurance/site/pulls/410"}}},"pull_request":{"url":"https://api.github.com/repos/codurance/site/pulls/410","id":88406761,"html_url":"https://github.com/codurance/site/pull/410","diff_url":"https://github.com/codurance/site/pull/410.diff","patch_url":"https://github.com/codurance/site/pull/410.patch","issue_url":"https://api.github.com/repos/codurance/site/issues/410","number":410,"state":"closed","locked":false,"title":"adding db testing blog post","user":{"login":"halimakoundi","id":5450688,"avatar_url":"https://avatars.githubusercontent.com/u/5450688?v=3","gravatar_id":"","url":"https://api.github.com/users/halimakoundi","html_url":"https://github.com/halimakoundi","followers_url":"https://api.github.com/users/halimakoundi/followers","following_url":"https://api.github.com/users/halimakoundi/following{/other_user}","gists_url":"https://api.github.com/users/halimakoundi/gists{/gist_id}","starred_url":"https://api.github.com/users/halimakoundi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/halimakoundi/subscriptions","organizations_url":"https://api.github.com/users/halimakoundi/orgs","repos_url":"https://api.github.com/users/halimakoundi/repos","events_url":"https://api.github.com/users/halimakoundi/events{/privacy}","received_events_url":"https://api.github.com/users/halimakoundi/received_events","type":"User","site_admin":false},"body":"![db-testing](https://cloud.githubusercontent.com/assets/5450688/19185268/bd471166-8c78-11e6-9a08-77db4043bcfb.png)\r\n","created_at":"2016-10-07T09:30:05Z","updated_at":"2016-10-08T13:57:28Z","closed_at":"2016-10-07T15:46:49Z","merged_at":"2016-10-07T15:46:49Z","merge_commit_sha":"5b2bbca30cc4a78af9951d30d7b65d7f28014e7f","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/codurance/site/pulls/410/commits","review_comments_url":"https://api.github.com/repos/codurance/site/pulls/410/comments","review_comment_url":"https://api.github.com/repos/codurance/site/pulls/comments{/number}","comments_url":"https://api.github.com/repos/codurance/site/issues/410/comments","statuses_url":"https://api.github.com/repos/codurance/site/statuses/575d3bc93c9fd176af70b0962327638658b90240","head":{"label":"codurance:db-testing-blog-post","ref":"db-testing-blog-post","sha":"575d3bc93c9fd176af70b0962327638658b90240","user":{"login":"codurance","id":5751452,"avatar_url":"https://avatars.githubusercontent.com/u/5751452?v=3","gravatar_id":"","url":"https://api.github.com/users/codurance","html_url":"https://github.com/codurance","followers_url":"https://api.github.com/users/codurance/followers","following_url":"https://api.github.com/users/codurance/following{/other_user}","gists_url":"https://api.github.com/users/codurance/gists{/gist_id}","starred_url":"https://api.github.com/users/codurance/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codurance/subscriptions","organizations_url":"https://api.github.com/users/codurance/orgs","repos_url":"https://api.github.com/users/codurance/repos","events_url":"https://api.github.com/users/codurance/events{/privacy}","received_events_url":"https://api.github.com/users/codurance/received_events","type":"Organization","site_admin":false},"repo":{"id":38424930,"name":"site","full_name":"codurance/site","owner":{"login":"codurance","id":5751452,"avatar_url":"https://avatars.githubusercontent.com/u/5751452?v=3","gravatar_id":"","url":"https://api.github.com/users/codurance","html_url":"https://github.com/codurance","followers_url":"https://api.github.com/users/codurance/followers","following_url":"https://api.github.com/users/codurance/following{/other_user}","gists_url":"https://api.github.com/users/codurance/gists{/gist_id}","starred_url":"https://api.github.com/users/codurance/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codurance/subscriptions","organizations_url":"https://api.github.com/users/codurance/orgs","repos_url":"https://api.github.com/users/codurance/repos","events_url":"https://api.github.com/users/codurance/events{/privacy}","received_events_url":"https://api.github.com/users/codurance/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/codurance/site","description":"","fork":false,"url":"https://api.github.com/repos/codurance/site","forks_url":"https://api.github.com/repos/codurance/site/forks","keys_url":"https://api.github.com/repos/codurance/site/keys{/key_id}","collaborators_url":"https://api.github.com/repos/codurance/site/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/codurance/site/teams","hooks_url":"https://api.github.com/repos/codurance/site/hooks","issue_events_url":"https://api.github.com/repos/codurance/site/issues/events{/number}","events_url":"https://api.github.com/repos/codurance/site/events","assignees_url":"https://api.github.com/repos/codurance/site/assignees{/user}","branches_url":"https://api.github.com/repos/codurance/site/branches{/branch}","tags_url":"https://api.github.com/repos/codurance/site/tags","blobs_url":"https://api.github.com/repos/codurance/site/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/codurance/site/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/codurance/site/git/refs{/sha}","trees_url":"https://api.github.com/repos/codurance/site/git/trees{/sha}","statuses_url":"https://api.github.com/repos/codurance/site/statuses/{sha}","languages_url":"https://api.github.com/repos/codurance/site/languages","stargazers_url":"https://api.github.com/repos/codurance/site/stargazers","contributors_url":"https://api.github.com/repos/codurance/site/contributors","subscribers_url":"https://api.github.com/repos/codurance/site/subscribers","subscription_url":"https://api.github.com/repos/codurance/site/subscription","commits_url":"https://api.github.com/repos/codurance/site/commits{/sha}","git_commits_url":"https://api.github.com/repos/codurance/site/git/commits{/sha}","comments_url":"https://api.github.com/repos/codurance/site/comments{/number}","issue_comment_url":"https://api.github.com/repos/codurance/site/issues/comments{/number}","contents_url":"https://api.github.com/repos/codurance/site/contents/{+path}","compare_url":"https://api.github.com/repos/codurance/site/compare/{base}...{head}","merges_url":"https://api.github.com/repos/codurance/site/merges","archive_url":"https://api.github.com/repos/codurance/site/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/codurance/site/downloads","issues_url":"https://api.github.com/repos/codurance/site/issues{/number}","pulls_url":"https://api.github.com/repos/codurance/site/pulls{/number}","milestones_url":"https://api.github.com/repos/codurance/site/milestones{/number}","notifications_url":"https://api.github.com/repos/codurance/site/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/codurance/site/labels{/name}","releases_url":"https://api.github.com/repos/codurance/site/releases{/id}","deployments_url":"https://api.github.com/repos/codurance/site/deployments","created_at":"2015-07-02T09:54:02Z","updated_at":"2016-09-27T15:40:24Z","pushed_at":"2016-10-08T13:49:38Z","git_url":"git://github.com/codurance/site.git","ssh_url":"git@github.com:codurance/site.git","clone_url":"https://github.com/codurance/site.git","svn_url":"https://github.com/codurance/site","homepage":null,"size":171607,"stargazers_count":2,"watchers_count":2,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":8,"mirror_url":null,"open_issues_count":2,"forks":8,"open_issues":2,"watchers":2,"default_branch":"master"}},"base":{"label":"codurance:master","ref":"master","sha":"6191bbaa111d0b1910418ae3f3f08cf55230873d","user":{"login":"codurance","id":5751452,"avatar_url":"https://avatars.githubusercontent.com/u/5751452?v=3","gravatar_id":"","url":"https://api.github.com/users/codurance","html_url":"https://github.com/codurance","followers_url":"https://api.github.com/users/codurance/followers","following_url":"https://api.github.com/users/codurance/following{/other_user}","gists_url":"https://api.github.com/users/codurance/gists{/gist_id}","starred_url":"https://api.github.com/users/codurance/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codurance/subscriptions","organizations_url":"https://api.github.com/users/codurance/orgs","repos_url":"https://api.github.com/users/codurance/repos","events_url":"https://api.github.com/users/codurance/events{/privacy}","received_events_url":"https://api.github.com/users/codurance/received_events","type":"Organization","site_admin":false},"repo":{"id":38424930,"name":"site","full_name":"codurance/site","owner":{"login":"codurance","id":5751452,"avatar_url":"https://avatars.githubusercontent.com/u/5751452?v=3","gravatar_id":"","url":"https://api.github.com/users/codurance","html_url":"https://github.com/codurance","followers_url":"https://api.github.com/users/codurance/followers","following_url":"https://api.github.com/users/codurance/following{/other_user}","gists_url":"https://api.github.com/users/codurance/gists{/gist_id}","starred_url":"https://api.github.com/users/codurance/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codurance/subscriptions","organizations_url":"https://api.github.com/users/codurance/orgs","repos_url":"https://api.github.com/users/codurance/repos","events_url":"https://api.github.com/users/codurance/events{/privacy}","received_events_url":"https://api.github.com/users/codurance/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/codurance/site","description":"","fork":false,"url":"https://api.github.com/repos/codurance/site","forks_url":"https://api.github.com/repos/codurance/site/forks","keys_url":"https://api.github.com/repos/codurance/site/keys{/key_id}","collaborators_url":"https://api.github.com/repos/codurance/site/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/codurance/site/teams","hooks_url":"https://api.github.com/repos/codurance/site/hooks","issue_events_url":"https://api.github.com/repos/codurance/site/issues/events{/number}","events_url":"https://api.github.com/repos/codurance/site/events","assignees_url":"https://api.github.com/repos/codurance/site/assignees{/user}","branches_url":"https://api.github.com/repos/codurance/site/branches{/branch}","tags_url":"https://api.github.com/repos/codurance/site/tags","blobs_url":"https://api.github.com/repos/codurance/site/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/codurance/site/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/codurance/site/git/refs{/sha}","trees_url":"https://api.github.com/repos/codurance/site/git/trees{/sha}","statuses_url":"https://api.github.com/repos/codurance/site/statuses/{sha}","languages_url":"https://api.github.com/repos/codurance/site/languages","stargazers_url":"https://api.github.com/repos/codurance/site/stargazers","contributors_url":"https://api.github.com/repos/codurance/site/contributors","subscribers_url":"https://api.github.com/repos/codurance/site/subscribers","subscription_url":"https://api.github.com/repos/codurance/site/subscription","commits_url":"https://api.github.com/repos/codurance/site/commits{/sha}","git_commits_url":"https://api.github.com/repos/codurance/site/git/commits{/sha}","comments_url":"https://api.github.com/repos/codurance/site/comments{/number}","issue_comment_url":"https://api.github.com/repos/codurance/site/issues/comments{/number}","contents_url":"https://api.github.com/repos/codurance/site/contents/{+path}","compare_url":"https://api.github.com/repos/codurance/site/compare/{base}...{head}","merges_url":"https://api.github.com/repos/codurance/site/merges","archive_url":"https://api.github.com/repos/codurance/site/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/codurance/site/downloads","issues_url":"https://api.github.com/repos/codurance/site/issues{/number}","pulls_url":"https://api.github.com/repos/codurance/site/pulls{/number}","milestones_url":"https://api.github.com/repos/codurance/site/milestones{/number}","notifications_url":"https://api.github.com/repos/codurance/site/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/codurance/site/labels{/name}","releases_url":"https://api.github.com/repos/codurance/site/releases{/id}","deployments_url":"https://api.github.com/repos/codurance/site/deployments","created_at":"2015-07-02T09:54:02Z","updated_at":"2016-09-27T15:40:24Z","pushed_at":"2016-10-08T13:49:38Z","git_url":"git://github.com/codurance/site.git","ssh_url":"git@github.com:codurance/site.git","clone_url":"https://github.com/codurance/site.git","svn_url":"https://github.com/codurance/site","homepage":null,"size":171607,"stargazers_count":2,"watchers_count":2,"language":"CSS","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":8,"mirror_url":null,"open_issues_count":2,"forks":8,"open_issues":2,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/codurance/site/pulls/410"},"html":{"href":"https://github.com/codurance/site/pull/410"},"issue":{"href":"https://api.github.com/repos/codurance/site/issues/410"},"comments":{"href":"https://api.github.com/repos/codurance/site/issues/410/comments"},"review_comments":{"href":"https://api.github.com/repos/codurance/site/pulls/410/comments"},"review_comment":{"href":"https://api.github.com/repos/codurance/site/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/codurance/site/pulls/410/commits"},"statuses":{"href":"https://api.github.com/repos/codurance/site/statuses/575d3bc93c9fd176af70b0962327638658b90240"}}}}
{"actor":{"display_login":"halimakoundi"}}