Skip to content

Commit

Permalink
Merge pull request #57 from lyz-code/fix/tinydb_bool_search_failed
Browse files Browse the repository at this point in the history
fix: make tinydb search return only entities of the asked model
  • Loading branch information
lyz-code committed Sep 23, 2021
2 parents 1792feb + 55954a4 commit e5a934d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/repository_orm/adapters/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def search(
) from error

for path in entities_with_value["matched_values"]:
entity_id = re.sub(r"root\[(.*?)\]\[.*", r"\1", path)
entity_id = re.sub(r"root\['?(.*?)'?\]\[.*", r"\1", path)

# Convert int ids from str to int
try:
Expand Down
5 changes: 4 additions & 1 deletion src/repository_orm/adapters/tinydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ def _build_search_query(
& (Query()[field].search(value))
)
else:
model_query_parts.append(Query()[field] == value)
model_query_parts.append(
(Query().model_type_ == model.__name__.lower())
& (Query()[field] == value)
)
if len(model_query_parts) != 0:
query_parts.append(self._merge_query(model_query_parts, mode="and"))
if len(query_parts) == 0:
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def case_genre(self) -> factory.Factory:
class AuthorFactory(factory.Factory): # type: ignore
"""Factory to generate fake authors."""

id_ = factory.Faker("word")
name = factory.Faker("word")
id_ = factory.Faker("sentence")
name = factory.Faker("sentence")
last_name = factory.Faker("last_name")
country = factory.Faker("country")
rating = factory.Faker("pyint")
Expand Down Expand Up @@ -75,7 +75,7 @@ class GenreFactory(factory.Factory): # type: ignore
"""Factory to generate fake genres."""

id_ = factory.Faker("pyint")
name = factory.Faker("name")
name = factory.Faker("sentence")
description = factory.Faker("sentence")
rating = factory.Faker("pyint")

Expand All @@ -89,7 +89,7 @@ class ListEntityFactory(factory.Factory): # type: ignore
"""Factory to generate fake list of entities."""

id_ = factory.Faker("pyint")
name = factory.Faker("name")
name = factory.Faker("sentence")
elements = factory.Faker("pylist", value_types=str)

class Meta:
Expand Down
1 change: 1 addition & 0 deletions tests/cases/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Entity(EntityModel):

name: str
state: str = "open"
active: bool = True


class Author(Entity):
Expand Down
40 changes: 39 additions & 1 deletion tests/integration/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from ..cases import Entity, OtherEntity, RepositoryTester
from ..cases.entities import ListEntityFactory
from ..cases.model import BoolEntity, ListEntity
from ..cases.model import Author, Book, BoolEntity, ListEntity


def test_apply_repository_creates_schema( # noqa: AAA01
Expand Down Expand Up @@ -552,6 +552,44 @@ def test_repository_can_search_by_multiple_properties(
assert result == [entity]


def test_repo_can_search_entity_if_two_different_entities_match(
repo: Repository,
) -> None:
"""
Given: Two different entities with the same ID
When: we search by a property equal in both entities and give only one model.
Then: only the entity that matches the model is returned
"""
author = Author(id_="author_id", name="common name")
book = Book(id_=1, name="common name")
repo.add(author)
repo.add(book)
repo.commit()

result = repo.search({"name": "common name"}, [Author])

assert result == [author]


def test_repo_can_search_entity_if_two_different_entities_match_giving_both_models(
repo: Repository,
) -> None:
"""
Given: Two different entities with the same ID
When: we search by a property equal in both entities and give both models.
Then: both entities that matches the model are returned
"""
author = Author(id_="author_id", name="common name")
book = Book(id_=1, name="common name")
repo.add(author)
repo.add(book)
repo.commit()

result = repo.search({"name": "common name"}, [Author, Book])

assert result == [author, book]


@pytest.mark.skip(
"Supported by Fake and TinyDB, not by Pypika yet. Once mappers are supported "
"it should be easy to add this particular case."
Expand Down
5 changes: 5 additions & 0 deletions tests/migrations/pypika/0001_initial_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"last_name VARCHAR(20), "
"country VARCHAR(20), "
"state VARCHAR(20), "
"active BOOL, "
"rating INT, "
"PRIMARY KEY (id))",
"DROP TABLE author",
Expand All @@ -25,6 +26,7 @@
"summary VARCHAR(255), "
"state VARCHAR(20), "
"released DATETIME, "
"active BOOL, "
"rating INT, "
"PRIMARY KEY (id))",
"DROP TABLE book",
Expand All @@ -36,6 +38,7 @@
"description VARCHAR(255), "
"state VARCHAR(20), "
"rating INT, "
"active BOOL, "
"PRIMARY KEY (id))",
"DROP TABLE genre",
),
Expand All @@ -46,6 +49,7 @@
"state VARCHAR(20), "
"description VARCHAR(255), "
"rating INT, "
"active BOOL, "
"PRIMARY KEY (id))",
"DROP TABLE otherentity",
),
Expand All @@ -55,6 +59,7 @@
"name VARCHAR(20), "
"state VARCHAR(20), "
"description VARCHAR(255), "
"active BOOL, "
"PRIMARY KEY (id))",
"DROP TABLE listentity",
),
Expand Down

0 comments on commit e5a934d

Please sign in to comment.