Skip to content

Commit

Permalink
Let’s try this
Browse files Browse the repository at this point in the history
  • Loading branch information
remi committed Sep 22, 2024
1 parent e10be8d commit 584bff3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/absinthe_error_payload/changeset_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ defmodule AbsintheErrorPayload.ChangesetParser do

defp interpolated_value_to_string(value) when is_list(value), do: Enum.join(value, ",")

defp interpolated_value_to_string({:parameterized, Ecto.Enum, %{on_load: mappings}}),
defp interpolated_value_to_string({:parameterized, {Ecto.Enum, %{on_load: mappings}}}),
do: mappings |> Map.values() |> Enum.join(",")

defp interpolated_value_to_string(value), do: to_string(value)
Expand Down
26 changes: 15 additions & 11 deletions test/changeset_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
)
end

defp sorted_options(options) do
Enum.sort_by(options, & &1.key)
end

describe "interpolate_message/1" do
test "interpolates correctly" do
result = ChangesetParser.interpolate_message({"Test %{one}", [one: "1"]})
Expand All @@ -96,7 +100,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
|> validate_length(:virtual, is: 4)

result = ChangesetParser.extract_messages(changeset)
assert [first, second, third] = result
assert [first, second, third] = Enum.sort_by(result, &to_string(&1.field))
assert %ValidationMessage{field: "author.name", key: :name} = first
assert %ValidationMessage{code: :format, field: :title, key: :title} = second
assert %ValidationMessage{code: :length, field: :virtual, key: :virtual} = third
Expand Down Expand Up @@ -303,7 +307,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.key == :title
assert message.field == :title

assert message.options == [
assert sorted_options(message.options) == [
%{key: :count, value: "2"},
%{key: :kind, value: "min"},
%{key: :type, value: "string"}
Expand All @@ -323,7 +327,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.code == :max
assert message.key == :title
assert message.field == :title
assert message.options == [%{key: :count, value: "3"}, %{key: :kind, value: "max"}, %{key: :type, value: "string"}]
assert sorted_options(message.options) == [%{key: :count, value: "3"}, %{key: :kind, value: "max"}, %{key: :type, value: "string"}]
assert message.message =~ ~r/3/
assert message.template =~ ~r/%{count}/
end
Expand All @@ -339,7 +343,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.key == :title
assert message.field == :title

assert message.options == [
assert sorted_options(message.options) == [
%{key: :count, value: "7"},
%{key: :kind, value: "is"},
%{key: :type, value: "string"}
Expand All @@ -359,7 +363,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.code == :greater_than
assert message.key == :upvotes
assert message.field == :upvotes
assert message.options == [%{key: :kind, value: "greater_than"}, %{key: :number, value: "10"}]
assert sorted_options(message.options) == [%{key: :kind, value: "greater_than"}, %{key: :number, value: "10"}]
assert message.message =~ ~r/10/
assert message.template =~ ~r/%{number}/
end
Expand All @@ -375,7 +379,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.key == :upvotes
assert message.field == :upvotes

assert message.options == [
assert sorted_options(message.options) == [
%{key: :kind, value: "greater_than_or_equal_to"},
%{key: :number, value: "10"}
]
Expand All @@ -395,7 +399,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.key == :upvotes
assert message.field == :upvotes

assert message.options ==
assert sorted_options(message.options) ==
[
%{key: :kind, value: "less_than"},
%{key: :number, value: "1"}
Expand All @@ -416,7 +420,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.key == :upvotes
assert message.field == :upvotes

assert message.options == [
assert sorted_options(message.options) == [
%{key: :kind, value: "less_than_or_equal_to"},
%{key: :number, value: "1"}
]
Expand All @@ -435,7 +439,7 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do
assert message.code == :equal_to
assert message.key == :upvotes
assert message.field == :upvotes
assert message.options == [%{key: :kind, value: "equal_to"}, %{key: :number, value: "1"}]
assert sorted_options(message.options) == [%{key: :kind, value: "equal_to"}, %{key: :number, value: "1"}]
assert message.message =~ ~r/1/
assert message.template =~ ~r/%{number}/
end
Expand Down Expand Up @@ -508,10 +512,10 @@ defmodule AbsintheErrorPayload.ChangesetParserTest do

assert [%ValidationMessage{} = message] = ChangesetParser.extract_messages(changeset)

assert message.code == :cast
assert message.code == :inclusion
assert message.key == :language
assert message.field == :language
assert message.options == [%{key: :type, value: "en,fr"}]
assert sorted_options(message.options) == [%{key: :enum, value: "en,fr"}, %{key: :type, value: "en,fr"}]
assert message.message != ""
assert message.template != ""
end
Expand Down

0 comments on commit 584bff3

Please sign in to comment.