FlowAssertions.Ecto.ChangesetA (Ecto Flow Assertions v0.1.0) View Source
Assertions for Ecto.Changeset
structures.
Link to this section Summary
Functions
Like assert_changes/2
for cases where you care only about a single field.
Applies FlowAssertions.MapA.assert_fields/2
to the changes in the changeset.
Assert that a field in the data part of a changeset matches a binding form.
Like assert_errors
but reads better when there's only a single error
to be checked
Assert that a field or fields have no associated errors.
Assert that a changeset contains specific errors. In the simplest case, it requires that each named field have at least one error, but doesn't require any specific message
A pipeline-ready version of refute changeset.valid?
The changeset must contain no changes.
Require a changeset to have no changes in particular fields. Unmentioned fields may have changes. When there's only a single field, it needn't be enclosed in a list.
A pipeline-ready version of assert changeset.valid?
Operate on the single element of a list in a changeset field.
Link to this section Functions
Like assert_changes/2
for cases where you care only about a single field.
This is just a convenience function for the grammatically obsessive.
changeset |> assert_change(:name)
changeset |> assert_change(name: "Bossie")
Applies FlowAssertions.MapA.assert_fields/2
to the changes in the changeset.
To check that fields have been changed:
changeset |> assert_changes([:name, :tags])
To check specific changed values:
changeset |> assert_changes(name: "Bossie", tags: [])
Assert that a field in the data part of a changeset matches a binding form.
changeset |> assert_data_shape(:field, %User{})
changeset |> assert_data_shape(:field, [_ | _])
Like assert_errors
but reads better when there's only a single error
to be checked:
assert_error(changeset, name: "is invalid")
If the message isn't to be checked, you can use a single atom:
assert_error(changeset, :name)
Assert that a field or fields have no associated errors.
changeset |> assert_error_free([:in_service_datestring, :name])
You needn't use a list if there's only one field to check.
changeset |> assert_error_free(:in_service_datestring)
Assert that a changeset contains specific errors. In the simplest case, it requires that each named field have at least one error, but doesn't require any specific message:
changeset |> assert_errors([:name, :tags])
A message may also be required:
changeset
|> assert_errors(name: "may not be blank", tags: "is invalid")
The given string must be an exact match for one of the field's error messages.
If you want to check more than one error message for a given field, enclose them in a list:
changeset
|> assert_errors(name: "may not be blank",
tags: ["is invalid", "has something else wrong"])
The list need not be a complete list of errors.
A pipeline-ready version of refute changeset.valid?
The changeset must contain no changes.
Require a changeset to have no changes in particular fields. Unmentioned fields may have changes. When there's only a single field, it needn't be enclosed in a list.
changeset |> assert_no_changes([:name, :tags])
changeset |> assert_no_changes(:name)
A pipeline-ready version of assert changeset.valid?
Operate on the single element of a list in a changeset field.
This is typically used with fields that take list values. Often,
you only want to test the empty list and a singleton list.
(When testing functions that produce their values with Enum.map/2
or for
,
creating a second list element gains you nothing.)
Using with_singleton_content
, it's
convenient to apply assertions to the single element:
changeset
|> assert_valid
|> with_singleton_content(:changes, :service_gaps)
|> assert_shape(%VM.ServiceGap{})
|> Ex.Datespan.assert_datestrings(:first)
The second value can be :data
, :changes
, or :newest
. The first use
their respective fields in the changeset. The last uses Ecto.Changeset.fetch_field!/2
, meaning:
- If the field is present in
Changeset.changes
, that value is used. - Otherwise, the value in
Changeset.data
is used.
If field
does not exist or isn't an Enum
, with_singleton_content
will fail in
the same way FlowAssertions.EnumA.singleton_content/1
does.