<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jadjare</title>
    <description>The latest articles on DEV Community by jadjare (@jadjare).</description>
    <link>https://dev.to/jadjare</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F53850%2Fb51fed27-fe60-4bbb-869f-91fb9adfd4a8.jpeg</url>
      <title>DEV Community: jadjare</title>
      <link>https://dev.to/jadjare</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jadjare"/>
    <language>en</language>
    <item>
      <title>Unit Testing. Logic in Domain vs Logic in SQL</title>
      <dc:creator>jadjare</dc:creator>
      <pubDate>Mon, 12 Mar 2018 11:04:27 +0000</pubDate>
      <link>https://dev.to/jadjare/unit-testing-logic-in-domain-vs-logic-in-sql--ph8</link>
      <guid>https://dev.to/jadjare/unit-testing-logic-in-domain-vs-logic-in-sql--ph8</guid>
      <description>&lt;h1&gt;
  
  
  Unit Testing. Logic in Domain vs Logic in SQL
&lt;/h1&gt;

&lt;p&gt;We have a table of 10 million records that include details of a location, a product, order date and units ordered.&lt;/p&gt;

&lt;p&gt;We need to query this data to find the sum of units ordered by product, for a given set of locations.&lt;br&gt;
We have to apply a business rule that says that if the order date is N days in the past then the ordered units are classifed as &lt;code&gt;late units&lt;/code&gt; else they are classified as &lt;code&gt;pending units&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This information will be displayed in a read on table (i.e. an onscreen report)&lt;br&gt;
| Product | Late Units | Pending Units |&lt;/p&gt;

&lt;p&gt;Our current design is to have a SQL Query that is passed the date on which ordered units should be classified as &lt;code&gt;late units&lt;/code&gt;.  The logic that then aggregates the data and places the units either in "late" or "Pending" is then applied in the SQL Stored Procedure.&lt;br&gt;
The query results are then placed into a Data Transfer Object (e.g. &lt;code&gt;{"product": "a", "lateUnits": 0, "pendingUnits": 100}&lt;/code&gt; ) and supplied to the client application via a Web Api.&lt;/p&gt;

&lt;p&gt;The challenge with this design is it becomes much harder to write a suite of unit tests to demonstrate the business rules are correctly applied.  At present we have created canned integration test data, then we spin up an a special integration test instance of the database and run tests against the sample data.&lt;/p&gt;

&lt;p&gt;This allows us to take advantage of the SQL query engine for performance, whilst having a reasonable degree of testing coverage.&lt;/p&gt;

&lt;p&gt;I'm still not 100% convinced of the approach we're taking and wonder what approach other developer's might take to solve this problem, e.g. would you load all the data into the domain and apply the logic there, apply the logic in SQL, or another approach?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>testing</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
