Skip to content

Commit

Permalink
Merge pull request #70 from lyz-code/feat/make-search-case-insensitive
Browse files Browse the repository at this point in the history
feat: make string search case insensitive
  • Loading branch information
lyz-code authored Dec 22, 2021
2 parents a3ef379 + 8a6adda commit 0a5ff3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/repository_orm/adapters/data/pypika.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlite3 import OperationalError
from typing import Dict, List, Type, Union

from pypika import Query, Table
from pypika import Query, Table, functions
from yoyo import get_backend, read_migrations

from ...exceptions import EntityNotFoundError, TooManyEntitiesError
Expand Down Expand Up @@ -234,7 +234,9 @@ def search(
if key == "id_":
key = "id"
if isinstance(value, str):
query = query.where(getattr(table, key).regexp(value))
query = query.where(
functions.Lower(getattr(table, key)).regexp(value.lower())
)
else:
query = query.where(getattr(table, key) == value)

Expand Down
2 changes: 1 addition & 1 deletion src/repository_orm/adapters/data/tinydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def _build_search_query(
if isinstance(value, str):
model_query_parts.append(
(Query().model_type_ == model.__name__.lower())
& (Query()[field].search(value))
& (Query()[field].search(value, flags=re.IGNORECASE))
)
else:
model_query_parts.append(
Expand Down
24 changes: 23 additions & 1 deletion tests/integration/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ def test_repository_can_search_regular_expression(
When: We search using a regular expression
Then: The matching entity is found
"""
# ignore: we know that all the entities we test have the name property
expected_entities = [
entity_
for entity_ in inserted_entities
Expand All @@ -500,6 +499,29 @@ def test_repository_can_search_regular_expression(
assert result == expected_entities


def test_repository_search_by_regular_expression_is_case_insensitive(
repo: Repository, inserted_entities: List[Entity]
) -> None:
"""
Given: More than one entity is inserted in the repository.
When: We search using a regular expression with the wrong capitalization
Then: The matching entity is found
If you come to disable this functionality, make it configurable instead, being the
default the search as case insensitive
"""
expected_entities = [
entity_
for entity_ in inserted_entities
if entity_.name == inserted_entities[0].name
]
regular_expression = fr"^{expected_entities[0].name.upper()}.*"

result = repo.search({"name": regular_expression}, type(expected_entities[0]))

assert result == expected_entities


def test_repository_search_raises_error_if_searching_by_inexistent_field(
repo: Repository,
inserted_entities: List[Entity],
Expand Down

0 comments on commit 0a5ff3f

Please sign in to comment.