---
title: "15.5 Testing it"
url: https://una-lang.org/en/learn/concurrency/testing-it/
release: v0.4.0
edition: "0.2"
---

# 15.5 Testing it

## 15.5.1 Fakes for each warehouse

The examples install each warehouse with a `given` line, as [chapter 14](https://una-lang.org/en/learn/testing/fakes-given-expect-performed-and-expect-not-performed/#s14-3) shows: `shelves` with a stock list for a warehouse that answers, and `offline` for one that does not. The handlers are part of the program, so the examples need no network and give the same answer on every run. Every example of a function that asks the warehouses gives all three bindings a `given` line.

## 15.5.2 The counts, and the order of the results

```una include="day-to-day/src/availability.una#L118-L139" title="day-to-day/src/availability.una" caption="the happy paths"
example "each warehouse's count goes into its own field"
  given @north = shelves(north_stock())
  given @south = shelves(south_stock())
  given @east = shelves(east_stock())
  do availability(OrderLine(sku: "LAMP-01", quantity: 5))
  expect result == Availability(sku: "LAMP-01", wanted: 5, north: 4, south: 2, east: 0)

example "the lines come back in the order of the order"
  given @north = shelves(north_stock())
  given @south = shelves(south_stock())
  given @east = shelves(east_stock())
  do check_order([OrderLine(sku: "RUG-07", quantity: 1), OrderLine(sku: "DESK-02", quantity: 1)])
  expect result.len == 2
  expect result.first() == Some(Availability(sku: "RUG-07", wanted: 1, north: 0, south: 3, east: 5))

example "an empty order asks no warehouse"
  given @north = shelves(north_stock())
  given @south = shelves(south_stock())
  given @east = shelves(east_stock())
  do check_order([])
  expect result == []
  expect not performed @north.on_hand
```

The first example checks that each warehouse's count lands in its own field. The second checks the guarantee of `together for`: the lines come back in the order of the order, whatever order their tasks finish in. The third checks the edge: an empty order starts no task, so no warehouse is asked, which `expect not performed` states.

## 15.5.3 The failure paths

```una include="day-to-day/src/availability.una#L141-L161" title="day-to-day/src/availability.una" caption="the failure paths"
example "a warehouse that does not answer fails the line"
  given @north = shelves(north_stock())
  given @south = shelves(south_stock())
  given @east = offline("east")
  do availability(OrderLine(sku: "LAMP-01", quantity: 5))
  expect fails Offline

example "the warehouses before the one that fails are still asked"
  given @north = shelves(north_stock())
  given @south = offline("south")
  given @east = shelves(east_stock())
  do availability(OrderLine(sku: "LAMP-01", quantity: 5))
  expect fails Offline
  expect performed @north.on_hand

example "one line that cannot be checked fails the whole order"
  given @north = shelves(north_stock())
  given @south = offline("south")
  given @east = shelves(east_stock())
  do check_order([OrderLine(sku: "LAMP-01", quantity: 5), OrderLine(sku: "DESK-02", quantity: 2)])
  expect fails Offline
```

One warehouse offline fails the line, and one line that fails fails the whole order. The second example states the rule of the first failure from the other side: the south warehouse fails, and the north one, the branch before it, was still asked.

## 15.5.4 What not to test: what timing decides

No example states whether the east warehouse was asked when the south one failed. Under `una test`, the east branch never starts, since the tasks run one after another. But the language does not promise that: in an executable, the east branch, which comes after the failing one, may have asked its warehouse before it was canceled. An example that states it would test the interpreter's schedule, not the guarantee. Test what the guarantee fixes: the result, the failure, and the effects of the tasks before a failing one.

## 15.5.5 The pure functions

The last four examples test `report_line`, `ships_whole` and `reason` with plain values, without fakes. An empty order ships whole, since none of its lines is short.

## 15.5.6 Running the examples

```text include="day-to-day/expected/commands/test.txt#L2-L13"
example availability "each warehouse's count goes into its own field" ... ok
example availability "the lines come back in the order of the order" ... ok
example availability "an empty order asks no warehouse" ... ok
example availability "a warehouse that does not answer fails the line" ... ok
example availability "the warehouses before the one that fails are still asked" ... ok
example availability "one line that cannot be checked fails the whole order" ... ok
example availability "a line short of stock says by how much" ... ok
example availability "an order ships whole only when every line can ship" ... ok
example availability "an empty order ships whole" ... ok
example availability "the reason names the warehouse" ... ok

10 examples: 10 passed, 0 failed
```
