From d2e2a587b8459b54b29f65489ee9238265185a8a Mon Sep 17 00:00:00 2001 From: woylie <13847569+woylie@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:50:06 +0900 Subject: [PATCH] add test for ecto enum operators --- lib/flop/filter.ex | 16 ++++++++++++++++ test/flop/filter_test.exs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/flop/filter.ex b/lib/flop/filter.ex index 85310b2..e7e7bc9 100644 --- a/lib/flop/filter.ex +++ b/lib/flop/filter.ex @@ -368,6 +368,22 @@ defmodule Flop.Filter do ] end + # for backward compatibility with Ecto < 3.12.0 + defp get_allowed_operators({:parameterized, Ecto.Enum, _}) do + [ + :==, + :!=, + :empty, + :not_empty, + :<=, + :<, + :>=, + :>, + :in, + :not_in + ] + end + defp get_allowed_operators(_) do [ :==, diff --git a/test/flop/filter_test.exs b/test/flop/filter_test.exs index def4814..1117d24 100644 --- a/test/flop/filter_test.exs +++ b/test/flop/filter_test.exs @@ -52,6 +52,41 @@ defmodule Flop.FilterTest do end end + test "returns list of operators for enum" do + types = [ + # by internal representation Ecto < 3.12.0 + {:parameterized, Ecto.Enum, %{type: :string}}, + # by internal representation Ecto >= 3.12.0 + {:parameterized, {Ecto.Enum, %{type: :string}}}, + # same with init function + Ecto.ParameterizedType.init(Ecto.Enum, values: [:one, :two]), + # by convenience format + {:ecto_enum, [:one, :two]}, + # by reference + {:from_schema, MyApp.Pet, :mood} + ] + + expected_ops = [ + :==, + :!=, + :empty, + :not_empty, + :<=, + :<, + :>=, + :>, + :in, + :not_in + ] + + for type <- types do + assert Filter.allowed_operators(type) == expected_ops + + assert Filter.allowed_operators(%Flop.FieldInfo{ecto_type: type}) == + expected_ops + end + end + test "returns a list of operators for unknown types" do assert [op | _] = Filter.allowed_operators(:unicorn) assert is_atom(op)