View Source DeltaCheck (DeltaCheck v0.1.1)

Main API for DeltaCheck, a testing toolkit for making assertions on database writes.

Link to this section Summary

Functions

Tracks database writes made in the given block, and asserts that the resulting delta matches the given pattern.

Tracks database writes made in the given block, and asserts that no writes were made.

Compares two snapshots and returns a delta.

Returns the name of the primary key of the schema.

Returns all Ecto schemas for the given application.

Returns a snapshot of the database.

Tracks database writes made in the given function, and returns a tuple with the containing the return value of the function and a database delta.

Link to this section Types

@type delta() :: [
  insert: Ecto.Schema.t(),
  update: {Ecto.Schema.t(), [{atom(), term()}]},
  delete: Ecto.Schema.t()
]
@type snapshot() :: %{optional(module()) => %{optional(term()) => Ecto.Schema.t()}}

Link to this section Functions

Link to this macro

assert_changes(pattern, opts \\ [], list)

View Source (macro)

Tracks database writes made in the given block, and asserts that the resulting delta matches the given pattern.

options

Options

  • :schemas - A list of schemas to track writes for, instead of the default schemas provided in the application configuration.

examples

Examples

iex> assert_changes(insert: %User{name: "John Doe"}) do
iex>   Accounts.create_user(%{name: "John Doe"})
iex> end
Link to this macro

assert_no_changes(opts \\ [], list)

View Source (macro)

Tracks database writes made in the given block, and asserts that no writes were made.

options

Options

  • :schemas - A list of schemas to track writes for, instead of the default schemas provided in the application configuration.

examples

Examples

iex> assert_no_changes do
iex>   Accounts.create_user(%{name: nil})
iex> end
Link to this function

compare(snapshot1, snapshot2)

View Source
@spec compare(map(), map()) :: delta()

Compares two snapshots and returns a delta.

examples

Examples

iex> compare(
iex>   %{User => %{1 => %User{id: 1, name: "John Doe"}}},
iex>   %{User => %{1 => %User{id: 1, name: "Jane Doe"}}}
iex> )
[update: {%User{id: 1, name: "Jane Doe"}, name: {"John Doe", "Jane Doe"}}]
Link to this function

get_primary_key!(schema)

View Source
@spec get_primary_key!(schema :: module()) :: atom()

Returns the name of the primary key of the schema.

examples

Examples

iex> get_primary_key!(User)
:id
Link to this function

get_schemas(application)

View Source
@spec get_schemas(application :: atom()) :: [module()]

Returns all Ecto schemas for the given application.

examples

Examples

iex> get_schemas(:my_app)
[Foo, Bar, Baz]
@spec snapshot(opts :: Keyword.t()) :: snapshot()

Returns a snapshot of the database.

options

Options

  • :schemas - A list of schemas to include in the snapshot, instead of the default schemas provided in the application configuration.

examples

Examples

iex> {:ok, %{id: id} = user} = Accounts.create_user(%{name: "John Doe"})
iex> snapshot(schemas: [User])
%{User => %{id => user}}
Link to this function

track_changes(fun, opts \\ [])

View Source
@spec track_changes(fun :: (() -> term()), opts :: Keyword.t()) :: term()

Tracks database writes made in the given function, and returns a tuple with the containing the return value of the function and a database delta.

options

Options

  • :schemas - A list of schemas to track writes for, instead of the default schemas provided in the application configuration.

examples

Examples

iex> {{_, user}, _} = track_changes(fn ->
iex>   Accounts.create_user(%{name: "John Doe"})
iex> end)
{{:ok, user}, [insert: user]}