Skip to content

Commit

Permalink
add test for ecto enum operators
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Aug 19, 2024
1 parent 1c1ef1e commit d2e2a58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/flop/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
:==,
Expand Down
35 changes: 35 additions & 0 deletions test/flop/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d2e2a58

Please sign in to comment.