{"id":79,"date":"2010-06-08T12:31:16","date_gmt":"2010-06-08T17:31:16","guid":{"rendered":"http:\/\/sqlity.net\/en\/?p=79"},"modified":"2014-11-13T14:04:24","modified_gmt":"2014-11-13T19:04:24","slug":"identifying-test-cases-1","status":"publish","type":"post","link":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/","title":{"rendered":"Identifying Test Cases #1"},"content":{"rendered":"<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">It takes a lot of practice to write test cases for SQL code, and much of that practice happens on the job. One of the most important aspects in writing test cases is simply identifying what test cases to write. Normally I start by writing my first test case before I touch the code at all, but today we're going to look at code first and then talk about what test cases are needed. The examples here will be using the tables in the AdventureWorks database for SQL Server.<\/p>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \"><strong>Example 1 - Expressions<\/strong><\/p>\n<pre style=\"background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(243, 242, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(69, 99, 192); border-right-color: rgb(69, 99, 192); border-bottom-color: rgb(69, 99, 192); border-left-color: rgb(69, 99, 192); display: block; overflow-x: auto; overflow-y: auto; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; white-space: pre; background-position: initial initial; background-repeat: initial initial; \">\r\nSELECT AVG(DATALENGTH(Comments)) AS AvgLen FROM Production.ProductReview\r\n<\/pre>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">This query is simply returning the average length of the Comments column. Here are some of the tests I think are needed:<\/p>\n<ol>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">There are no rows in the ProductReview table<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">There is one row in the ProductReview table<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">The Comments column contains a null value (Comments is a nullable column, we want to make sure that the value returned is appropriate)<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">The Comments column contains an empty string<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">There are multiple rows in the ProductReview table<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">There are multiple rows in the ProductReview table and for some of them the Comments column is a null value or empty string (testing that null values and empty strings are being handled the way necessary for my application)<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">There is a very large text in the Comments column (testing that we do not overflow the value returned by AVG or DATALENGTH functions)<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \"><strong>Example 2 - Filter Criteria<\/strong><\/p>\n<pre style=\"background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(243, 242, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(69, 99, 192); border-right-color: rgb(69, 99, 192); border-bottom-color: rgb(69, 99, 192); border-left-color: rgb(69, 99, 192); display: block; overflow-x: auto; overflow-y: auto; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; white-space: pre; background-position: initial initial; background-repeat: initial initial; \">\r\nSELECT ProductNumber, Name FROM Production.Product WHERE Color != 'Silver'\r\n<\/pre>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">This query gets us the product numbers and names of products which are not Silver. Here are some test cases:<\/p>\n<ol>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A single row with a Color of 'Silver'<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A single row with a Color other than 'Silver', e.g. 'Red'<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A single row with a null Color (the way the query is written now, this row would not be returned)<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A single row with a null ProductNumber<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A single row with a null Name. This case and the previous one are especially important if this is part of code that will consume the values of this query. If these columns are not-nullable, then there should be a test case elsewhere to assert that design.<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">Multiple rows which match the criteria and do not match the criteria<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">Depending on the needs for case-sensitivity, I would also have test cases that varied the case of the word 'Silver'<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \"><strong>Example 3 - A Simple Join<\/strong><\/p>\n<pre style=\"background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(243, 242, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(69, 99, 192); border-right-color: rgb(69, 99, 192); border-bottom-color: rgb(69, 99, 192); border-left-color: rgb(69, 99, 192); display: block; overflow-x: auto; overflow-y: auto; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; white-space: pre; background-position: initial initial; background-repeat: initial initial; \">\r\nSELECT Store.Name, Customer.AccountNumber\r\nFROM Sales.Store\r\nJOIN Sales.Customer ON Store.CustomerID = Customer.CustomerID\r\n<\/pre>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">We're joining on CustomerID which is the primary key in both of these tables, and it is a foreign key from the Sales table to the Customer table. Assuming these constraints are unit tested somewhere else, we might have the following test cases for this SELECT statement:<\/p>\n<ol>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">No rows in either table<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A row in Customer which does not have a corresponding row in Store<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">A row in Customer which does have a corresponding row in Store<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \"><strong>Homework<\/strong><\/p>\n<ol>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">Take another look at Example 3. What additional test cases would we have if Store.CustomerID was not the primary key of the Store table and was instead a nullable foreign key column?<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">Why do we have to pay such close attention to null?<\/p>\n<\/li>\n<li>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">Are there any test cases missing in the examples above?<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \"><strong>Looking ahead<\/strong><br \/>Next time, we'll have some more advanced query examples and work with parameters. Then later, we'll discuss some simple practices and tools that can help you identify test cases while you're programming.<\/p>\n<p style=\"margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; \">\n","protected":false},"excerpt":{"rendered":"<p>It takes a lot of practice to write test cases for SQL code, and much of that practice happens on the job. One of the most important aspects in writing test cases is simply identifying what test cases to write. <a href=\"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/\">[more&#8230;]<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-general"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v29.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Identifying Test Cases #1<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Identifying Test Cases #1\" \/>\n<meta property=\"og:description\" content=\"It takes a lot of practice to write test cases for SQL code, and much of that practice happens on the job. One of the most important aspects in writing test cases is simply identifying what test cases to write. [more...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/\" \/>\n<meta property=\"og:site_name\" content=\"sqlity.net\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/sqlity.net\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-08T17:31:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-13T19:04:24+00:00\" \/>\n<meta name=\"author\" content=\"dennis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sqlity\" \/>\n<meta name=\"twitter:site\" content=\"@sqlity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dennis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/\"},\"author\":{\"name\":\"dennis\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/caeaf2eec046b6d097266d21374d10b6\"},\"headline\":\"Identifying Test Cases #1\",\"datePublished\":\"2010-06-08T17:31:16+00:00\",\"dateModified\":\"2014-11-13T19:04:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/\"},\"wordCount\":554,\"commentCount\":0,\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/\",\"name\":\"Identifying Test Cases #1\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\"},\"datePublished\":\"2010-06-08T17:31:16+00:00\",\"dateModified\":\"2014-11-13T19:04:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/caeaf2eec046b6d097266d21374d10b6\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/79\\\/identifying-test-cases-1\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Identifying Test Cases #1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\",\"name\":\"sqlity.net\",\"description\":\"Quality for SQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/sqlity.net\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/caeaf2eec046b6d097266d21374d10b6\",\"name\":\"dennis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g\",\"caption\":\"dennis\"},\"sameAs\":[\"http:\\\/\\\/www.curiouslycorrect.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Identifying Test Cases #1","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/","og_locale":"en_US","og_type":"article","og_title":"Identifying Test Cases #1","og_description":"It takes a lot of practice to write test cases for SQL code, and much of that practice happens on the job. One of the most important aspects in writing test cases is simply identifying what test cases to write. [more...]","og_url":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/","og_site_name":"sqlity.net","article_publisher":"https:\/\/www.facebook.com\/sqlity.net","article_published_time":"2010-06-08T17:31:16+00:00","article_modified_time":"2014-11-13T19:04:24+00:00","author":"dennis","twitter_card":"summary_large_image","twitter_creator":"@sqlity","twitter_site":"@sqlity","twitter_misc":{"Written by":"dennis","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/#article","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/"},"author":{"name":"dennis","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/caeaf2eec046b6d097266d21374d10b6"},"headline":"Identifying Test Cases #1","datePublished":"2010-06-08T17:31:16+00:00","dateModified":"2014-11-13T19:04:24+00:00","mainEntityOfPage":{"@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/"},"wordCount":554,"commentCount":0,"articleSection":["General"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/","url":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/","name":"Identifying Test Cases #1","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/#website"},"datePublished":"2010-06-08T17:31:16+00:00","dateModified":"2014-11-13T19:04:24+00:00","author":{"@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/caeaf2eec046b6d097266d21374d10b6"},"breadcrumb":{"@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sqlity.net\/en\/79\/identifying-test-cases-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sqlity.net\/en\/"},{"@type":"ListItem","position":2,"name":"Identifying Test Cases #1"}]},{"@type":"WebSite","@id":"https:\/\/sqlity.net\/en\/#website","url":"https:\/\/sqlity.net\/en\/","name":"sqlity.net","description":"Quality for SQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sqlity.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/caeaf2eec046b6d097266d21374d10b6","name":"dennis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/166ceda64e242f6ea01d467d0b6121b7264f9127efd3531ece1126385c50a385?s=96&d=mm&r=g","caption":"dennis"},"sameAs":["http:\/\/www.curiouslycorrect.com"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2wXuw-1h","jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":0,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}