6 Unit tests

 

Programming at a coffee shop

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

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 data input and compare the data output of the function with the expected data output. In this chapter, we write unit tests for the queries and mutations that we wrote in the previous chapters.

6.1 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.2 Unit tests for data manipulation code

6.2.1 The tree of function calls

6.2.2 Unit tests for functions down the tree

6.2.3 Unit tests for nodes in the tree

6.3 Unit tests for queries

6.4 Unit tests for mutations

Moving forward

Summary