diff --git a/lib/flop.ex b/lib/flop.ex index 5e1fc4e..bcf671c 100644 --- a/lib/flop.ex +++ b/lib/flop.ex @@ -232,7 +232,7 @@ defmodule Flop do ...> %{name: "Patty", age: 2, species: "C. aegagrus"} ...> ]) iex> - iex> params = %{filters: [%{field: :name, op: :=~, value: "Mag"}]} + iex> params = %{filters: [%{field: :name, op: :like, value: "Mag"}]} iex> {:ok, {results, meta}} = Flop.validate_and_run(MyApp.Pet, params) iex> meta.total_count 1 diff --git a/test/adapters/ecto/cases/flop_test.exs b/test/adapters/ecto/cases/flop_test.exs index 1c5e469..5a4be39 100644 --- a/test/adapters/ecto/cases/flop_test.exs +++ b/test/adapters/ecto/cases/flop_test.exs @@ -404,31 +404,64 @@ defmodule Flop.Adapters.Ecto.FlopTest do end end - test "escapes % in (i)like queries" do + test "escapes % in like queries" do %{id: _id1} = insert(:pet, name: "abc") %{id: id2} = insert(:pet, name: "a%c") - for op <- [:like, :ilike, :like_and, :like_or, :ilike_and, :ilike_or] do + for op <- [:like, :like_and, :like_or] do flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a%c"}]} assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) end end - test "escapes _ in (i)like queries" do + @tag :ilike + test "escapes % in ilike queries" do + %{id: _id1} = insert(:pet, name: "abc") + %{id: id2} = insert(:pet, name: "a%c") + + for op <- [:ilike, :ilike_and, :ilike_or] do + flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a%c"}]} + assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) + end + end + + test "escapes _ in like queries" do + %{id: _id1} = insert(:pet, name: "abc") + %{id: id2} = insert(:pet, name: "a_c") + + for op <- [:like, :like_and, :like_or] do + flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a_c"}]} + assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) + end + end + + @tag :ilike + test "escapes _ in ilike queries" do %{id: _id1} = insert(:pet, name: "abc") %{id: id2} = insert(:pet, name: "a_c") - for op <- [:like, :ilike, :like_and, :like_or, :ilike_and, :ilike_or] do + for op <- [:ilike, :ilike_and, :ilike_or] do flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a_c"}]} assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) end end - test "escapes \\ in (i)like queries" do + test "escapes \\ in like queries" do + %{id: _id1} = insert(:pet, name: "abc") + %{id: id2} = insert(:pet, name: "a\\c") + + for op <- [:like, :like_and, :like_or] do + flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a\\c"}]} + assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) + end + end + + @tag :ilike + test "escapes \\ in ilike queries" do %{id: _id1} = insert(:pet, name: "abc") %{id: id2} = insert(:pet, name: "a\\c") - for op <- [:like, :ilike, :like_and, :like_or, :ilike_and, :ilike_or] do + for op <- [:ilike, :ilike_and, :ilike_or] do flop = %Flop{filters: [%Filter{field: :name, op: op, value: "a\\c"}]} assert [%Pet{id: ^id2}] = Flop.all(Pet, flop) end @@ -452,6 +485,7 @@ defmodule Flop.Adapters.Ecto.FlopTest do end end + @tag :ilike property "applies ilike filter" do check all pet_count <- integer(@pet_count_range), pets = insert_list_and_sort(pet_count, :pet_with_owner), @@ -470,6 +504,7 @@ defmodule Flop.Adapters.Ecto.FlopTest do end end + @tag :ilike property "applies not ilike filter" do check all pet_count <- integer(@pet_count_range), pets = insert_list_and_sort(pet_count, :pet_with_owner), @@ -533,6 +568,7 @@ defmodule Flop.Adapters.Ecto.FlopTest do end end + @tag :ilike property "applies ilike_and filter" do check all pet_count <- integer(@pet_count_range), pets = insert_list_and_sort(pet_count, :pet_with_owner), @@ -552,6 +588,7 @@ defmodule Flop.Adapters.Ecto.FlopTest do end end + @tag :ilike property "applies ilike_or filter" do check all pet_count <- integer(@pet_count_range), pets = insert_list_and_sort(pet_count, :pet_with_owner), diff --git a/test/adapters/ecto/sqlite/test_helper.exs b/test/adapters/ecto/sqlite/test_helper.exs index 5081190..6e1fd87 100644 --- a/test/adapters/ecto/sqlite/test_helper.exs +++ b/test/adapters/ecto/sqlite/test_helper.exs @@ -40,4 +40,4 @@ _ = Ecto.Adapters.SQLite3.storage_down(Flop.Repo.config()) :ok = Ecto.Migrator.up(Flop.Repo, 0, Flop.Repo.SQLite.Migration, log: false) {:ok, _} = Application.ensure_all_started(:ex_machina) -ExUnit.start(exclude: [:composite_type, :prefix]) +ExUnit.start(exclude: [:composite_type, :ilike, :prefix])