BitcrowdEcto.Assertions (bitcrowd_ecto v0.12.0) View Source

Useful little test assertions related to Ecto.Changeset.t/0.

Example

Import this in your ExUnit.CaseTemplate:

defmodule MyApp.TestCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import BitcrowdEcto.Assertions
    end
  end
end

Link to this section Summary

Functions

Asserts that a changeset contains a failed "acceptance" validation on a given field.

Allows to compare a DateTime field to another, testing whether they are roughly equal (d=5s). Delta defaults to 5 seconds and can be passed in optionally.

Allows to compare a DateTime field to the present time.

Asserts that the value of a datetime field changed to the present time.

Asserts that a changeset contains a change of a given field.

Asserts that a changeset contains a change of a given field to a given value.

Assert that a given function changes the count of a given database table.

Assert multiple database table count changes.

Asserts that a given function changes the integer fetched by another function by a delta.

Asserts that a changeset contains a given error on a given field.

Asserts that a changeset contains a failed "foreign_key" constraint validation on a given field.

Asserts that a changeset contains a failed "foreign_key" constraint validation on a given field.

Asserts that a changeset contains a constraint on a given field.

Asserts that a changeset contains a failed "format" validation on a given field.

Asserts that a changeset contains a failed "inclusion" validation on a given field.

Asserts that a changeset contains a failed "no_assoc" constraint validation on a given field.

Asserts that a changeset contains a constraint on a given field.

Asserts that a changeset contains a failed "number" validation on a given field.

Asserts that an Ecto struct has a preloaded nested struct at a given path.

Asserts that a changeset contains a failed "required" validation on a given field.

Assert that two lists are equal when sorted (Enum.sort).

Asserts that a changeset contains a failed "unique" constraint validation on a given field.

Asserts that a changeset contains a constraint on a given field.

A better error helper that transforms the errors on a given field into a list of [<message>, <value of the :validation metadata field>].

Refutes that a changeset accepts changes to a given field.

Assert that a given function doesn't change the value fetched by another function.

Asserts that a changeset does not contain an error on a given field.

Refutes that an Ecto struct has a preloaded nested struct at a given path.

Link to this section Functions

Link to this function

assert_acceptance_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_acceptance_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "acceptance" validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_almost_coincide(a, b, delta \\ 5)

View Source (since 0.3.0)

Allows to compare a DateTime field to another, testing whether they are roughly equal (d=5s). Delta defaults to 5 seconds and can be passed in optionally.

Link to this function

assert_almost_now(timestamp)

View Source (since 0.3.0)

Allows to compare a DateTime field to the present time.

Link to this function

assert_change_to_almost_now(changeset, field)

View Source (since 0.9.0)

Specs

assert_change_to_almost_now(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that the value of a datetime field changed to the present time.

Example

%TestSchema{datetime: nil}
|> Ecto.Changeset.change(%{datetime: DateTime.utc_now()})
|> assert_change_to_almost_now(:datetime)
Link to this function

assert_changes(changeset, field)

View Source (since 0.1.0)

Specs

assert_changes(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a change of a given field.

Returns the changeset for chainability.

Link to this function

assert_changes(changeset, field, value)

View Source (since 0.1.0)

Specs

assert_changes(Ecto.Changeset.t(), atom(), any()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a change of a given field to a given value.

Returns the changeset for chainability.

Link to this function

assert_changeset_valid(cs)

View Source (since 0.11.0)

Specs

assert_changeset_valid(Ecto.Changeset.t()) :: Ecto.Changeset.t() | no_return()
Link to this function

assert_count_difference(repo, schema, by, how)

View Source (since 0.1.0)

Specs

assert_count_difference(Ecto.Repo.t(), module(), integer(), (() -> any())) ::
  Ecto.Changeset.t() | no_return()

Assert that a given function changes the count of a given database table.

Example

assert_count_difference Repo, Foo, 1, fn ->
  Repo.insert(%Foo{})
end
Link to this function

assert_count_differences(repo, list, how)

View Source (since 0.1.0)

Specs

assert_count_differences(Ecto.Repo.t(), [{module(), integer()}], (() -> any())) ::
  Ecto.Changeset.t() | no_return()

Assert multiple database table count changes.

See assert_count_difference/4 for details.

Example

assert_count_differences([{MyApp.Foo, 1}, {MyApp.Bar, -1}], fn ->
  %MyApp.Foo{} |> MyApp.Repo.insert()
  %MyApp.Bar{id: 1} |> MyApp.Repo.delete()
end
Link to this function

assert_difference(what, by, how, opts \\ [])

View Source (since 0.1.0)

Specs

assert_difference(
  (() -> float() | integer()),
  float() | integer(),
  (() -> any()),
  [{:message, String.t()}]
) :: Ecto.Changeset.t() | no_return()

Asserts that a given function changes the integer fetched by another function by a delta.

Example

assert_difference fn -> Repo.count(Foo) end, 1 fn ->
  %Foo{} |> Repo.insert()
end
Link to this function

assert_error_on(changeset, field, error, opts \\ [])

View Source (since 0.1.0)

Specs

assert_error_on(Ecto.Changeset.t(), atom(), atom() | [atom()], [
  {:metadata, atom()}
]) :: Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a given error on a given field.

Returns the changeset for chainability.

Link to this function

assert_foreign_constraint_error_on(changeset, field)

View Source (since 0.1.0)
This function is deprecated. Use assert_foreign_key_constraint_error_on/2 instead.

Specs

assert_foreign_constraint_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "foreign_key" constraint validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_foreign_key_constraint_error_on(changeset, field)

View Source (since 0.10.0)

Asserts that a changeset contains a failed "foreign_key" constraint validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_foreign_key_constraint_on(changeset, field, opts \\ [])

View Source (since 0.10.0)

Specs

assert_foreign_key_constraint_on(Ecto.Changeset.t(), atom(), keyword()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a constraint on a given field.

This function looks into the changeset's (internal) constraints field to see if a *_constraint function has been called on it. Tests using this do not need to actually perform the database operation. However, given that the constraints only work in combination with a corresponding database constraint, it is advisable to perform the operation and use assert_foreign_key_constraint_error_on/2 instead.

Returns the changeset for chainability.

Options

The given options are used as match values against the constraint map. They loosely correspond to the options of Ecto.Changeset.foreign_key_constraint/2, only :name becomes :constraint.

Link to this function

assert_format_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_format_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "format" validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_inclusion_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_inclusion_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "inclusion" validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_no_assoc_constraint_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_no_assoc_constraint_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "no_assoc" constraint validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_no_assoc_constraint_on(changeset, field, opts \\ [])

View Source (since 0.10.0)

Specs

assert_no_assoc_constraint_on(Ecto.Changeset.t(), atom(), keyword()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a constraint on a given field.

This function looks into the changeset's (internal) constraints field to see if a *_constraint function has been called on it. Tests using this do not need to actually perform the database operation. However, given that the constraints only work in combination with a corresponding database constraint, it is advisable to perform the operation and use assert_no_assoc_constraint_error_on/2 instead.

Returns the changeset for chainability.

Options

The given options are used as match values against the constraint map. They loosely correspond to the options of Ecto.Changeset.no_assoc_constraint/2, only :name becomes :constraint.

Link to this function

assert_number_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_number_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "number" validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_preloaded(record, x)

View Source (since 0.1.0)

Specs

assert_preloaded(schema :: Ecto.Schema.t(), fields :: atom() | [atom()]) ::
  boolean() | no_return()

Asserts that an Ecto struct has a preloaded nested struct at a given path.

Link to this function

assert_required_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_required_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "required" validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_sorted_equal(a, b)

View Source (since 0.3.0)

Assert that two lists are equal when sorted (Enum.sort).

Example

assert_sorted_equal [:"1", :"2"], [:"2", :"1"]
assert_sorted_equal(
  [%{id: 2}, %{id: 1}],
  [%{id: 1, preload_nested_resource: %{id: 5}}, %{id: 2}],
  & &1.id
)
Link to this function

assert_sorted_equal(a, b, accessor)

View Source
Link to this function

assert_unique_constraint_error_on(changeset, field)

View Source (since 0.1.0)

Specs

assert_unique_constraint_error_on(Ecto.Changeset.t(), atom()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a failed "unique" constraint validation on a given field.

Returns the changeset for chainability.

Link to this function

assert_unique_constraint_on(changeset, field, opts \\ [])

View Source (since 0.10.0)

Specs

assert_unique_constraint_on(Ecto.Changeset.t(), atom(), keyword()) ::
  Ecto.Changeset.t() | no_return()

Asserts that a changeset contains a constraint on a given field.

This function looks into the changeset's (internal) constraints field to see if a *_constraint function has been called on it. Tests using this do not need to actually perform the database operation. However, given that the constraints only work in combination with a corresponding database constraint, it is advisable to perform the operation and use assert_unique_constraint_error_on/2 instead.

Returns the changeset for chainability.

Options

The given options are used as match values against the constraint map. They loosely correspond to the options of Ecto.Changeset.unique_constraint/2, only :name becomes :constraint.

Link to this function

flat_errors_on(changeset, field, opts \\ [])

View Source (since 0.1.0)

Specs

flat_errors_on(Ecto.Changeset.t(), atom(), [{:metadata, atom()}]) :: [
  String.t() | atom()
]

A better error helper that transforms the errors on a given field into a list of [<message>, <value of the :validation metadata field>].

If multiple validations failed, the list will contain more elements! That simple.

Metadata

By default, flat_errors_on/2 extracts metadata from the :validation and :constraint keys, as those are were Ecto stores its metadata. Custom metadata at different keys can be extracted using the :metadata option.

Link to this function

refute_changes(changeset, field)

View Source (since 0.1.0)

Specs

refute_changes(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t() | no_return()

Refutes that a changeset accepts changes to a given field.

Returns the changeset for chainability.

Link to this function

refute_changeset_valid(cs)

View Source (since 0.11.0)

Specs

refute_changeset_valid(Ecto.Changeset.t()) :: Ecto.Changeset.t() | no_return()
Link to this function

refute_difference(what, how, opts \\ [])

View Source (since 0.1.0)

Specs

refute_difference((() -> any()), (() -> any()), [{:message, String.t()}]) ::
  Ecto.Changeset.t() | no_return()

Assert that a given function doesn't change the value fetched by another function.

Example

refute_difference fn -> Repo.count(Foo) end, fn ->
  Repo.insert(%Foo{})
end
Link to this function

refute_errors_on(changeset, field)

View Source (since 0.1.0)

Specs

refute_errors_on(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t() | no_return()

Asserts that a changeset does not contain an error on a given field.

Returns the changeset for chainability.

Link to this function

refute_preloaded(record, x)

View Source (since 0.1.0)

Specs

refute_preloaded(schema :: Ecto.Schema.t(), fields :: atom() | [atom()]) ::
  boolean() | no_return()

Refutes that an Ecto struct has a preloaded nested struct at a given path.