Decree.Decision.Assertions (decree v0.1.0)

Copy Markdown View Source

Test helpers for a Decree.Decision, for an application to import.

use ExUnit.Case

import Decree.Decision.Assertions

test "a claimed email refuses a second claim" do
  assert_refuses(ClaimEmail, [%EmailClaimed{digest: "d"}], %ClaimEmailCommand{digest: "d"},
    :email_taken
  )
end

test "a free email is claimed" do
  assert_decides(ClaimEmail, [], command, %EmailClaimed{digest: "d"})
end

They run Decree.Decision.evolve/2, Decree.Decision.admit/2 and Decree.Decision.decide/2 with no store: the given events stand for what the reads would answer, decrypted where the decision lists them, so they never read time or a database. What only a store can answer, such as a retry or a command match, needs a dispatch. So do a batch's scope and fact checks, which run on prepared tags, and what a dispatch reduces: on a decision that lists its fold, a reason here is the callback's whole. They check no tag index and no activation.

A failure is an ExUnit.AssertionError, which is why ExUnit has to be running, as it is under mix test.

Summary

Functions

Asserts that the decision decides expected: an event, a list of events, or {:streams, members}, as Decree.Decision.decide/2 would answer it. Answers the state the fold reached with the new events applied.

Asserts that the decision refuses with reason.

Folds given_events, then admits and decides command, answering as a dispatch would.

Functions

assert_decides(decision, given_events, command, expected, opts \\ [])

@spec assert_decides(module(), [struct()], struct(), term(), keyword()) :: term()

Asserts that the decision decides expected: an event, a list of events, or {:streams, members}, as Decree.Decision.decide/2 would answer it. Answers the state the fold reached with the new events applied.

Takes the options of decision_result/4.

assert_query_items(decision, command, expected, opts \\ [])

@spec assert_query_items(module(), struct(), term(), keyword()) :: :ok

Asserts that Decree.Decision.query/2 answers expected for the command.

Options

assert_refuses(decision, given_events, command, reason, opts \\ [])

@spec assert_refuses(module(), [struct()], struct(), term(), keyword()) :: :ok

Asserts that the decision refuses with reason.

Takes the options of decision_result/4.

decision_result(decision, given_events, command, opts \\ [])

@spec decision_result(module(), [struct()], struct(), keyword()) ::
  {:ok, [struct()]} | {:ok, term()} | {:error, term()}

Folds given_events, then admits and decides command, answering as a dispatch would.

  • {:ok, events}, every new event in append order, as a dispatch with returning: :events answers, or {:ok, state}, the fold with the new events applied, with returning: :aggregate_state.
  • {:error, reason}, the refusal a dispatch answers. :duplicate from Decree.Decision.decide/2 is {:error, :duplicate_command}.

Options