Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larsschwegmann committed Sep 25, 2024
1 parent b2c667e commit 2a3cf0d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ In PostgreSQL and MYSQL, you can use the ``contains``, ``contained_by`` and ``fi
obj5 = await JSONModel.filter(data__filter={"owner__name__isnull": True}).first()
obj6 = await JSONModel.filter(data__filter={"owner__last__not_isnull": False}).first()
In PostgreSQL and MySQL, you can use ``posix_regex`` to make comparisons using POSIX regular expressions:
In PostgreSQL and MySQL, you can use ``postgres_posix_regex`` to make comparisons using POSIX regular expressions:
On PostgreSQL, this uses the ``~`` operator, on MySQL it uses the ``REGEXP`` operator.

.. code-block:: python3
Expand Down
1 change: 1 addition & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async def test_equal_null(self):
)



class TestDecimalFieldFilters(test.TestCase):
async def asyncSetUp(self):
await super().asyncSetUp()
Expand Down
25 changes: 25 additions & 0 deletions tests/test_posix_regex_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from tortoise.contrib import test
from tests import testmodels

@test.requireCapability(dialect="postgres")
class TestPosixRegexFilterPostgres(test.IsolatedTestCase):
tortoise_test_modules = ["tests.testmodels"]

async def test_regex_filter(self):
author = await testmodels.Author.create(name="Johann Wolfgang von Goethe")
self.assertEqual(
set(await testmodels.Author.filter(name__posix_regex="^Johann [a-zA-Z]+ von Goethe$").values_list("name", flat=True)),
{"Johann Wolfgang von Goethe"},
)


@test.requireCapability(dialect="mysql")
class TestPosixRegexFilterPostgres(test.IsolatedTestCase):
tortoise_test_modules = ["tests.testmodels"]

async def test_regex_filter(self):
author = await testmodels.Author.create(name="Johann Wolfgang von Goethe")
self.assertEqual(
set(await testmodels.Author.filter(name__posix_regex="^Johann [a-zA-Z]+ von Goethe$").values_list("name", flat=True)),
{"Johann Wolfgang von Goethe"},
)
6 changes: 3 additions & 3 deletions tortoise/backends/base_postgres/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
postgres_json_filter,
)
from tortoise.contrib.postgres.search import SearchCriterion
from tortoise.filters import json_contained_by, json_contains, json_filter, search
from tortoise.contrib.postgres.regex import posix_regex
from tortoise.filters import json_contained_by, json_contains, json_filter, search, posix_regex
from tortoise.contrib.postgres.regex import postgres_posix_regex


def postgres_search(field: Term, value: Term):
Expand All @@ -29,7 +29,7 @@ class BasePostgresExecutor(BaseExecutor):
json_contains: postgres_json_contains,
json_contained_by: postgres_json_contained_by,
json_filter: postgres_json_filter,
posix_regex: posix_regex,
posix_regex: postgres_posix_regex,
}

def parameter(self, pos: int) -> Parameter:
Expand Down
7 changes: 5 additions & 2 deletions tortoise/contrib/postgres/regex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from pypika.terms import Term, BasicCriterion
from pypika.enums import Comparator

class PostgresRegexMatching(Comparator):
posix_regex = " ~ "

def posix_regex(field: Term, value: str):
return BasicCriterion(" ~ ", field, value)
def postgres_posix_regex(field: Term, value: str):
return BasicCriterion(PostgresRegexMatching.posix_regex, field, field.wrap_constant(value))
8 changes: 7 additions & 1 deletion tortoise/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def search(field: Term, value: str):

def posix_regex(field: Term, value: str):
# Will be overridden in each executor
pass
raise NotImplementedError("The postgres_posix_regex filter operator is not supported by your database backend")


def starts_with(field: Term, value: str) -> Criterion:
Expand Down Expand Up @@ -478,6 +478,12 @@ def get_filters_for_field(
"operator": insensitive_ends_with,
"value_encoder": string_encoder,
},
f"{field_name}__posix_regex": {
"field": actual_field_name,
"source_field": source_field,
"operator": posix_regex,
"value_encoder": string_encoder,
},
f"{field_name}__year": {
"field": actual_field_name,
"source_field": source_field,
Expand Down

0 comments on commit 2a3cf0d

Please sign in to comment.