I am trying to make ergonomic test helpers like so:
def violated_unique_constraint?(
field,
{:error,
%Changeset{
errors: [
{field,
{_,
[
{:constraint, :unique},
{:constraint_name, _}
]}}
]
}}
),
do: true
def violated_unique_constraint?(_, _), do: false
Which in my tests I am invoking like so:
assert violated_unique_constraint?(:country_code) =
insert(%{country_code: "us", ...})
Which fails with:
** (CompileError) test/schema/whatever_test.exs:15 cannot find or invoke local violated_unique_constraint?/1 inside match. Only macros can be invoked in a match and they must be defined before their invocation. Called as: violated_unique_constraint?(:country_code)
You’ll notice I am matching the first parameter inside the second which AFAIK is allowed in Elixir and worked really well for me in other scenarios.
What am I doing wrong?