Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Jan 20, 2022
1 parent 69a5a99 commit 82a6da9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/changeset_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
field(:topics, {:array, :string})
field(:virtual, :string, virtual: true)
field(:published_at, :naive_datetime)
field(:metadata, {:array, :map})

belongs_to(:author, Author)
has_many(:tags, Tag)
Expand Down Expand Up @@ -181,6 +182,23 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert %ValidationMessage{code: :required, field: "tags.0.name", key: :name} = first
assert %ValidationMessage{code: :required, field: "tags.1.name", key: :name} = second
end

test "nested nil has many fields with errors" do
changeset =
%{"tags" => nil}
|> changeset()
|> cast_assoc(:tags,
with: fn tag, params ->
tag
|> cast(params, ~w(name)a)
|> validate_required(:name)
end
)

result = ChangesetParser.extract_messages(changeset)
assert [first] = result
assert %ValidationMessage{code: :association, field: :tags, key: :tags, message: "is invalid", options: [%{key: :type, value: "array-map"}]} = first
end
end

describe "construct_message/2" do
Expand Down

0 comments on commit 82a6da9

Please sign in to comment.