chapter six

6 Unit tests

 

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.1 Programming at a Coffee shop

In a Data-Oriented system, our code deals mainly with data manipulation: most of our functions receive data and return data. As a consequence, it’s quite easy to write unit tests to check whether our code behaves as expected. 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 previous chapters.

6.2 The simplicity of Data-Oriented test cases

Theo and Joe are seated around a large wooden table in a corner of "La vie est belle", a nice little French coffee shop, located near the Golden Gate Bridge. Theo orders a café au lait with a croissant and Joe orders a tight espresso with a pain au chocolat.

Instead of the usual general discussions about programming and life when they’re out of the office, Joe leads the discussion towards a very concrete topic: Unit tests. Theo asks Joe for an explanation.

THEO: 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.

THEO: Why does that make a difference?

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 Moving forward

6.7 Summary