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.

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

Link to this function

assert_change(cs, field_description)

View Source

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")
Link to this function

assert_changes(changeset, keyword_list)

View Source

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: [])
Link to this function

assert_data(changeset, expected)

View Source
Link to this macro

assert_data_shape(changeset, key, shape)

View Source (macro)

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, [_ | _])
Link to this function

assert_error(cs, error_description)

View Source

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)
Link to this function

assert_error_free(changeset, field)

View Source

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)
Link to this function

assert_errors(changeset, error_descriptions)

View Source

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.

Link to this function

assert_invalid(changeset)

View Source

A pipeline-ready version of refute changeset.valid?

Link to this function

assert_no_changes(changeset)

View Source

The changeset must contain no changes.

Link to this function

assert_no_changes(changeset, field)

View Source

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?

Link to this function

with_singleton_content(changeset, version, field)

View Source

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:

  1. If the field is present in Changeset.changes, that value is used.
  2. 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.