From 584bff3451f403c3dcb6a4172588102f778f6a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Pr=C3=A9vost?= Date: Sun, 22 Sep 2024 12:38:26 -0400 Subject: [PATCH] =?UTF-8?q?Let=E2=80=99s=20try=20this?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../changeset_parser.ex | 2 +- test/changeset_parser_test.exs | 26 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/absinthe_error_payload/changeset_parser.ex b/lib/absinthe_error_payload/changeset_parser.ex index a45a64b..e9ea037 100644 --- a/lib/absinthe_error_payload/changeset_parser.ex +++ b/lib/absinthe_error_payload/changeset_parser.ex @@ -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) diff --git a/test/changeset_parser_test.exs b/test/changeset_parser_test.exs index c6f8c96..73f4464 100644 --- a/test/changeset_parser_test.exs +++ b/test/changeset_parser_test.exs @@ -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"]}) @@ -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 @@ -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"} @@ -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 @@ -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"} @@ -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 @@ -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"} ] @@ -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"} @@ -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"} ] @@ -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 @@ -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