cartus.test

A cartus.core/Logger implementation and utilities for use in tests.

create-outcome

(create-outcome logger modifiers log-specs)

events

(events test-logger)

Retrieves events logged to the provided test-logger.

logged?

(logged? logger & log-specs)(logged? logger modifiers & log-specs)

Asserts that the logger received log events matching the provided log specs.

Takes either a logger and a variable number of log specs or a logger, a set of modifiers and a variable number of log specs:

  • logger must be a cartus.test/logger
  • modifiers must be a set, optionally containing one of each of:
    • #{:in-order :in-any-order} to specify ordering constraints, defaults to :in-order
    • #{:only :at-least} to specify whether the provided logs must exactly match the log events logged to the logger or whether they represent a subset, defaults to :at-least
    • #{:fuzzy-contents :strict-contents} to specify whether log events should be matched fuzzily, i.e., surplus keys can be present in the log event map, or strictly, i.e., the keys and values must match exactly, defaults to :fuzzy-contents
  • each log spec is a partial or full map of the log event as returned by events.

Internally, logged? uses the matcher-combinators library meaning more complex log specs can be provided, including using predicates to match parts of the log events. See the Getting Started guide for more details.

Examples:

(is (logged? logger
  {:level   :debug
   :type    :service.database/connection-pool.started
   :context {:max-connections 10}}))
(is (logged? logger #{:in-any-order :strict-contents}
  {:level   :info
   :type    :service.database/connection-pool.online.starting
   :context {:max-connections 10}
   :meta    {:ns     (find-ns 'service.database)
             :line   1
             :column 1}}
   {:level   :info
   :type    :service.database/connection-pool.batch.starting
   :context {:max-connections 3}
   :meta    {:ns     (find-ns 'service.database)
             :line   2
             :column 1}}))

logger

(logger)

Constructs a test logger storing all logged events in an atom.

Events are stored as maps including all provided attributes, in the order in which they occur, accounting for concurrency.

Events can be retrieved using events.

was-logged?

(was-logged? logger modifiers & log-specs)

Returns true if the given log-specs were logged.

Takes a logger, a set of modifiers and a variable number of log specs:

  • logger must be a cartus.test/logger
  • modifiers must be a set, optionally containing one of each of:
  • #{:in-order :in-any-order} to specify ordering constraints, defaults to :in-order
  • #{:only :at-least} to specify whether the provided logs must exactly match the log events logged to the logger or whether they represent a subset, defaults to :at-least
  • #{:fuzzy-contents :strict-contents} to specify whether log events should be matched fuzzily, i.e., surplus keys can be present in the log event map, or strictly, i.e., the keys and values must match exactly, defaults to :fuzzy-contents
  • each log spec is a partial or full map of the log event as returned by events.