Datumo.Scenario (datumo v0.1.0)

Copy Markdown View Source

The board's scenarios as tests: given, then decide or read, then expect.

import Datumo.Scenario

# a write slice
given([%LotOpened{lot_id: "L-1"}])
|> decide(Decider, %CloseLot{lot_id: "L-1"})
|> expect({:ok, [%LotClosed{lot_id: "L-1"}]})

# a read slice
given([%PlotRegistered{plot_id: "P-1", name: "La Vega"}])
|> read(ReadModel, "P-1")
|> expect(%{name: "La Vega"})

Why not fold the given by hand

In production a decision only sees what its query/1 reads. Folding the given straight into apply_event/2 skips that read, so a query that forgets a type, asks for the wrong tag or misses a follow-up passes every test and silently never fires. Here the given is the store:

The given

An event struct — the normal case. An event no code writes any more (it's still in the store) or that comes from outside has no struct: give it as the shape FACT appends, %{type: "…", data: %{…}, tags: ["…"]}, plus id: "…" when the fold keys on the event id.

Summary

Functions

Runs a write slice's decision against the given.

Asserts the result matches pattern (a struct or map pattern matches the keys it names). Returns the result.

Stores the scenario's given events, in order.

Computes a read slice's view from the given.

Functions

decide(scenario, decider, cmd)

Runs a write slice's decision against the given.

expect(scenario, pattern)

(macro)

Asserts the result matches pattern (a struct or map pattern matches the keys it names). Returns the result.

given(events)

Stores the scenario's given events, in order.

read(scenario, read_model, args)

Computes a read slice's view from the given.