View Source BitcrowdEcto.Assertions (bitcrowd_ecto v0.16.0)
Useful little test assertions related to Ecto.Changeset.t/0
.
example
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
@spec 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.
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.
@spec 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
Example
%TestSchema{datetime: nil}
|> Ecto.Changeset.change(%{datetime: DateTime.utc_now()})
|> assert_change_to_almost_now(:datetime)
@spec 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.
@spec 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.
@spec assert_changeset_valid(Ecto.Changeset.t()) :: Ecto.Changeset.t() | no_return()
assert_count_difference(repo, schema, by, how, opts \\ [])
View Source (since 0.1.0)@spec assert_count_difference(Ecto.Repo.t(), module(), integer(), (() -> any()), [ BitcrowdEcto.Repo.ecto_option() ]) :: Ecto.Changeset.t() | no_return()
Assert that a given function changes the count of a given database table.
example
Example
assert_count_difference Repo, Foo, 1, fn ->
Repo.insert(%Foo{})
end
assert_count_differences(repo, table_counts, how, opts \\ [])
View Source (since 0.1.0)@spec assert_count_differences( Ecto.Repo.t(), [{module(), integer()}], (() -> any()), keyword() ) :: Ecto.Changeset.t() | no_return()
Assert multiple database table count changes.
See assert_count_difference/5
for details.
example
Example
assert_count_differences([{MyApp.Foo, 1}, {MyApp.Bar, -1}], fn ->
%MyApp.Foo{} |> MyApp.Repo.insert()
%MyApp.Bar{id: 1} |> MyApp.Repo.delete()
end
@spec 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
Example
assert_difference fn -> Repo.count(Foo) end, 1 fn ->
%Foo{} |> Repo.insert()
end
assert_error_on(changeset, field, error, opts \\ [])
View Source (since 0.1.0)@spec 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.
assert_foreign_constraint_error_on(changeset, field)
View Source (since 0.1.0)@spec 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.
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.
assert_foreign_key_constraint_on(changeset, field, opts \\ [])
View Source (since 0.10.0)@spec 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
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
.
@spec 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.
@spec 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.
assert_no_assoc_constraint_error_on(changeset, field)
View Source (since 0.1.0)@spec 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.
assert_no_assoc_constraint_on(changeset, field, opts \\ [])
View Source (since 0.10.0)@spec 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
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
.
@spec 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.
@spec 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.
@spec 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.
Assert that two lists are equal when sorted (Enum.sort).
example
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
)
@spec 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.
assert_unique_constraint_on(changeset, field, opts \\ [])
View Source (since 0.10.0)@spec 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
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
.
@spec 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
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.
@spec 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.
@spec refute_changeset_valid(Ecto.Changeset.t()) :: Ecto.Changeset.t() | no_return()
@spec 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
Example
refute_difference fn -> Repo.count(Foo) end, fn ->
Repo.insert(%Foo{})
end
@spec 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.
@spec 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.