Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add like_any_of, like_all_of, ilike_any_of and ilike_all_of filters #358

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ remove_pytest_asyncio_from_sync:
sed -i 's/@pytest.mark.asyncio//g' tests/_sync/test_client.py

sleep:
sleep 5
sleep 2
40 changes: 40 additions & 0 deletions postgrest/base_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,46 @@ def like(self: Self, column: str, pattern: str) -> Self:
"""
return self.filter(column, Filters.LIKE, pattern)

def like_all_of(self: Self, column: str, pattern: str) -> Self:
"""A 'LIKE' filter, to use for pattern matching.

Args:
column: The name of the column to apply a filter on
pattern: The pattern to filter by
"""

return self.filter(column, Filters.LIKE_ALL, f"{{{pattern}}}")

def like_any_of(self: Self, column: str, pattern: str) -> Self:
"""A 'LIKE' filter, to use for pattern matching.

Args:
column: The name of the column to apply a filter on
pattern: The pattern to filter by
"""

return self.filter(column, Filters.LIKE_ANY, f"{{{pattern}}}")

def ilike_all_of(self: Self, column: str, pattern: str) -> Self:
"""A 'ILIKE' filter, to use for pattern matching (case insensitive).

Args:
column: The name of the column to apply a filter on
pattern: The pattern to filter by
"""

return self.filter(column, Filters.ILIKE_ALL, f"{{{pattern}}}")

def ilike_any_of(self: Self, column: str, pattern: str) -> Self:
"""A 'ILIKE' filter, to use for pattern matching (case insensitive).

Args:
column: The name of the column to apply a filter on
pattern: The pattern to filter by
"""

return self.filter(column, Filters.ILIKE_ANY, f"{{{pattern}}}")

def ilike(self: Self, column: str, pattern: str) -> Self:
"""An 'ILIKE' filter, to use for pattern matching (case insensitive).

Expand Down
4 changes: 4 additions & 0 deletions postgrest/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class Filters(StrEnum):
LTE = "lte"
IS = "is"
LIKE = "like"
LIKE_ALL = "like(all)"
LIKE_ANY = "like(any)"
ILIKE = "ilike"
ILIKE_ALL = "ilike(all)"
ILIKE_ANY = "ilike(any)"
FTS = "fts"
PLFTS = "plfts"
PHFTS = "phfts"
Expand Down
24 changes: 24 additions & 0 deletions tests/_async/test_filter_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ def test_ilike(filter_request_builder):
assert str(builder.params) == "x=ilike.%25a%25"


def test_like_all_of(filter_request_builder):
builder = filter_request_builder.like_all_of("x", "A*,*b")

assert str(builder.params) == "x=like%28all%29.%7BA%2A%2C%2Ab%7D"


def test_like_any_of(filter_request_builder):
builder = filter_request_builder.like_any_of("x", "a*,*b")

assert str(builder.params) == "x=like%28any%29.%7Ba%2A%2C%2Ab%7D"


def test_ilike_all_of(filter_request_builder):
builder = filter_request_builder.ilike_all_of("x", "A*,*b")

assert str(builder.params) == "x=ilike%28all%29.%7BA%2A%2C%2Ab%7D"


def test_ilike_any_of(filter_request_builder):
builder = filter_request_builder.ilike_any_of("x", "A*,*b")

assert str(builder.params) == "x=ilike%28any%29.%7BA%2A%2C%2Ab%7D"


def test_is_(filter_request_builder):
builder = filter_request_builder.is_("x", "a")

Expand Down
54 changes: 54 additions & 0 deletions tests/_async/test_filter_request_builder_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,60 @@ async def test_ilike():
assert res.data == [{"country_name": "ALBANIA", "iso": "AL"}]


async def test_like_all_of():
res = (
await rest_client()
.from_("countries")
.select("nicename, iso")
.like_all_of("nicename", "A*,*n")
.execute()
)

assert res.data == [{"iso": "AF", "nicename": "Afghanistan"}]


async def test_like_any_of():
res = (
await rest_client()
.from_("countries")
.select("nicename, iso")
.like_any_of("nicename", "Al*,*ia")
.execute()
)

assert res.data == [
{"iso": "AL", "nicename": "Albania"},
{"iso": "DZ", "nicename": "Algeria"},
]


async def test_ilike_all_of():
res = (
await rest_client()
.from_("countries")
.select("nicename, iso")
.ilike_all_of("nicename", "a*,*n")
.execute()
)

assert res.data == [{"iso": "AF", "nicename": "Afghanistan"}]


async def test_ilike_any_of():
res = (
await rest_client()
.from_("countries")
.select("nicename, iso")
.ilike_any_of("nicename", "al*,*ia")
.execute()
)

assert res.data == [
{"iso": "AL", "nicename": "Albania"},
{"iso": "DZ", "nicename": "Algeria"},
]


async def test_is_():
res = (
await rest_client()
Expand Down
24 changes: 24 additions & 0 deletions tests/_sync/test_filter_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ def test_ilike(filter_request_builder):
assert str(builder.params) == "x=ilike.%25a%25"


def test_like_all_of(filter_request_builder):
builder = filter_request_builder.like_all_of("x", "A*,*b")

assert str(builder.params) == "x=like%28all%29.%7BA%2A%2C%2Ab%7D"


def test_like_any_of(filter_request_builder):
builder = filter_request_builder.like_any_of("x", "a*,*b")

assert str(builder.params) == "x=like%28any%29.%7Ba%2A%2C%2Ab%7D"


def test_ilike_all_of(filter_request_builder):
builder = filter_request_builder.ilike_all_of("x", "A*,*b")

assert str(builder.params) == "x=ilike%28all%29.%7BA%2A%2C%2Ab%7D"


def test_ilike_any_of(filter_request_builder):
builder = filter_request_builder.ilike_any_of("x", "A*,*b")

assert str(builder.params) == "x=ilike%28any%29.%7BA%2A%2C%2Ab%7D"


def test_is_(filter_request_builder):
builder = filter_request_builder.is_("x", "a")

Expand Down
54 changes: 54 additions & 0 deletions tests/_sync/test_filter_request_builder_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,60 @@ def test_ilike():
assert res.data == [{"country_name": "ALBANIA", "iso": "AL"}]


def test_like_all_of():
res = (
rest_client()
.from_("countries")
.select("nicename, iso")
.like_all_of("nicename", "A*,*n")
.execute()
)

assert res.data == [{"iso": "AF", "nicename": "Afghanistan"}]


def test_like_any_of():
res = (
rest_client()
.from_("countries")
.select("nicename, iso")
.like_any_of("nicename", "Al*,*ia")
.execute()
)

assert res.data == [
{"iso": "AL", "nicename": "Albania"},
{"iso": "DZ", "nicename": "Algeria"},
]


def test_ilike_all_of():
res = (
rest_client()
.from_("countries")
.select("nicename, iso")
.ilike_all_of("nicename", "a*,*n")
.execute()
)

assert res.data == [{"iso": "AF", "nicename": "Afghanistan"}]


def test_ilike_any_of():
res = (
rest_client()
.from_("countries")
.select("nicename, iso")
.ilike_any_of("nicename", "al*,*ia")
.execute()
)

assert res.data == [
{"iso": "AL", "nicename": "Albania"},
{"iso": "DZ", "nicename": "Algeria"},
]


def test_is_():
res = (
rest_client()
Expand Down