Skip to content

Commit

Permalink
Merge pull request #497 from woylie/like-ilike-sqlite-tests
Browse files Browse the repository at this point in the history
like ilike sqlite tests
  • Loading branch information
woylie authored Sep 12, 2024
2 parents 0bdfeb9 + c514521 commit 6854a31
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lib/flop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 60 additions & 14 deletions test/adapters/ecto/cases/flop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,14 @@ defmodule Flop.Adapters.Ecto.FlopTest do
end
end

property "applies like filter" do
property "applies like filter", %{ecto_adapter: ecto_adapter} do
check all pet_count <- integer(@pet_count_range),
pets = insert_list_and_sort(pet_count, :pet_with_owner),
field <- filterable_pet_field(:string),
pet <- member_of(pets),
value = Pet.get_field(pet, field),
query_value <- substring(value) do
expected = filter_items(pets, field, :like, query_value)
expected = filter_items(pets, field, :like, query_value, ecto_adapter)

assert query_pets_with_owners(%{
filters: [%{field: field, op: :like, value: query_value}]
Expand All @@ -404,44 +404,78 @@ 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, :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

property "applies not like filter" do
property "applies not like filter", %{ecto_adapter: ecto_adapter} do
check all pet_count <- integer(@pet_count_range),
pets = insert_list_and_sort(pet_count, :pet_with_owner),
field <- filterable_pet_field(:string),
pet <- member_of(pets),
value = Pet.get_field(pet, field),
query_value <- substring(value) do
expected = filter_items(pets, field, :not_like, query_value)
expected =
filter_items(pets, field, :not_like, query_value, ecto_adapter)

assert query_pets_with_owners(%{
filters: [%{field: field, op: :not_like, value: query_value}]
Expand All @@ -451,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),
Expand All @@ -469,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),
Expand All @@ -486,14 +522,21 @@ defmodule Flop.Adapters.Ecto.FlopTest do
end
end

property "applies like_and filter" do
property "applies like_and filter", %{ecto_adapter: ecto_adapter} do
check all pet_count <- integer(@pet_count_range),
pets = insert_list_and_sort(pet_count, :pet_with_owner),
field <- filterable_pet_field(:string),
pet <- member_of(pets),
value = Pet.get_field(pet, field),
search_text_or_list <- search_text_or_list(value) do
expected = filter_items(pets, field, :like_and, search_text_or_list)
expected =
filter_items(
pets,
field,
:like_and,
search_text_or_list,
ecto_adapter
)

assert query_pets_with_owners(%{
filters: [
Expand All @@ -505,14 +548,15 @@ defmodule Flop.Adapters.Ecto.FlopTest do
end
end

property "applies like_or filter" do
property "applies like_or filter", %{ecto_adapter: ecto_adapter} do
check all pet_count <- integer(@pet_count_range),
pets = insert_list_and_sort(pet_count, :pet_with_owner),
field <- filterable_pet_field(:string),
pet <- member_of(pets),
value = Pet.get_field(pet, field),
search_text_or_list <- search_text_or_list(value) do
expected = filter_items(pets, field, :like_or, search_text_or_list)
expected =
filter_items(pets, field, :like_or, search_text_or_list, ecto_adapter)

assert query_pets_with_owners(%{
filters: [
Expand All @@ -524,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),
Expand All @@ -543,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),
Expand Down
4 changes: 4 additions & 0 deletions test/adapters/ecto/postgres/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ defmodule Flop.Integration.Case do
setup do
:ok = Sandbox.checkout(Flop.Repo)
end

setup do
%{ecto_adapter: :postgres}
end
end

Code.require_file("migration.exs", __DIR__)
Expand Down
6 changes: 5 additions & 1 deletion test/adapters/ecto/sqlite/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ defmodule Flop.Integration.Case do
setup do
:ok = Sandbox.checkout(Flop.Repo)
end

setup do
%{ecto_adapter: :sqlite}
end
end

Code.require_file("migration.exs", __DIR__)
Expand All @@ -36,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])
Loading

0 comments on commit 6854a31

Please sign in to comment.