assert_identity v0.1.0 AssertIdentity View Source

Link to this section Summary

Types

Value that can be compared by identity.

Functions

Asserts that a and b have the same identity.

Link to this section Types

Link to this type

comparable()

View Source
comparable() :: list() | {list(), any()} | %{id: any()} | {map(), any()}

Value that can be compared by identity.

Link to this section Functions

Link to this function

assert_ids_match(a, b, opts \\ [])

View Source
assert_ids_match(comparable(), comparable(), list()) :: boolean()

Asserts that a and b have the same identity.

Checks that the id keys of all provided structs are equal. Also compares any lists.

This is useful to assert that Ecto structs are equal without doing a comparison on the direct structs, which may not be strictly equivalent due to e.g. association preloading.

Raises ExUnit.AssertionError if identities can't be compared.

Options

  • sorted - If true, indicates that the given lists are already sorted and should not be sorted by the function

Examples

iex> AssertIdentity.assert_ids_match([%{id: 1}], [%{id: 1}])
true

iex> AssertIdentity.assert_ids_match({[%{"id" => 1}], "id"}, {[%{"id" => 1}], "id"})
true

iex> AssertIdentity.assert_ids_match(%{id: 1}, %{id: 1})
true

iex> AssertIdentity.assert_ids_match({%{"id" => 1}, "id"}, {%{"id" => 1}, "id"})
true

iex> AssertIdentity.assert_ids_match([%{id: 2}, %{id: 1}], [%{id: 1}, %{id: 2}])
true