chapter six

6 The synergy between unit tests and Data-Oriented programming

 

6.1 Introduction

In a Data-Oriented system, our code deals mainly with data manipulation: most of the functions we wrote in Part 1 receive data and return data. Therefore, it’s quite easy to write unit tests to check our code behave as we expect. A unit test is made of test cases that generate a data input and compare the data output of the function with the expected data output.

In this chapter, we write unit tests for queries and mutations that we wrote in Part 1. This chapter covers:

  • Generation of the minimal data input for a test case
  • Comparison of the output of a function with the expected output
  • Guidance about the quality and the quantity of the test cases

6.2 The simplicity of Data-Oriented test cases

You seat with Joe at a Starbucks Coffee near the office and instead of the usual general discussions about programming and life, when you are out of the office, this time Joe leads the discussion towards a very concrete topic: Unit tests. You ask Joe for an explanation.

YOU: Are unit tests such a simple topic that we can tackle it here in a coffee shop?

JOE: Unit tests in general, no. But unit tests for Data-Oriented code, yes.

YOU: Why does it make a difference?

JOE: The vast majority of the code base of a Data-Oriented system deals with data manipulation.

YOU: Yeah. I noticed that almost all the functions we wrote in Part 1, receive data and return data.

6.3 Unit tests for data manipulation code

6.3.1 The tree of function calls

6.3.2 Unit test for functions down the tree

6.3.3 Unit test for nodes in the tree

6.4 Unit tests for queries

6.5 Unit tests for mutations

6.6 Wrapping up