diff --git a/mongoz/core/db/documents/metaclasses.py b/mongoz/core/db/documents/metaclasses.py index 2f24293..271bb23 100644 --- a/mongoz/core/db/documents/metaclasses.py +++ b/mongoz/core/db/documents/metaclasses.py @@ -337,7 +337,7 @@ def __search_for_fields(base: Type, attrs: Any) -> None: # and not the MongozField itself for name, field in new_class.model_fields.items(): if isinstance(field.default, MongozField): - new_class.model_fields[name].default = field.default.pydantic_field.default + new_class.model_fields[name] = field.default.pydantic_field # Register the signals _register_document_signals(new_class) diff --git a/tests/inheritance/test_inheritance.py b/tests/inheritance/test_inheritance.py new file mode 100644 index 0000000..dcaa06d --- /dev/null +++ b/tests/inheritance/test_inheritance.py @@ -0,0 +1,68 @@ +from typing import Any, AsyncGenerator, List, Optional + +import pydantic +import pytest +from tests.conftest import client + +import mongoz +from mongoz import Document, ObjectId + +pytestmark = pytest.mark.anyio +pydantic_version = pydantic.__version__[:3] + + +class BaseDocument(Document): + name: str = mongoz.String() + year: int = mongoz.Integer() + tags: Optional[List[str]] = mongoz.Array(str, null=True) + + class Meta: + abstract = True + registry = client + database = "test_db" + + +class Movie(BaseDocument): + uuid: Optional[ObjectId] = mongoz.ObjectId(null=True) + + +class Actor(BaseDocument): + movies: Optional[List[Any]] = mongoz.ArrayList(null=True) + + +@pytest.fixture(scope="function", autouse=True) +async def prepare_database() -> AsyncGenerator: + await Movie.query().delete() + await Actor.query().delete() + yield + await Movie.query().delete() + await Actor.query().delete() + + +async def test_model_all() -> None: + movies = await Movie.query().all() + assert len(movies) == 0 + + await Movie(name="Barbie", year=2003).create() + + movies = await Movie.query().all() + assert len(movies) == 1 + + cursor = Movie.query() + async for movie in cursor: + assert movie.name == "Barbie" + + +async def test_model_nested_inherited() -> None: + actors = await Actor.query().all() + assert len(actors) == 0 + + await Actor(name="Paul Rudd", year=1972, movies=["Only Murders in the Building"]).create() + + actors = await Actor.query().all() + assert len(actors) == 1 + + actors = Actor.query() + async for movie in actors: + assert movie.name == "Paul Rudd" + assert movie.movies[0] == "Only Murders in the Building" diff --git a/tests/models/test_array_and_array_field.py b/tests/models/test_array_and_array_field.py new file mode 100644 index 0000000..31b771e --- /dev/null +++ b/tests/models/test_array_and_array_field.py @@ -0,0 +1,94 @@ +from typing import Any, AsyncGenerator, List, Optional + +import pydantic +import pytest +from pydantic import ValidationError +from tests.conftest import client + +import mongoz +from mongoz import Document, ObjectId + +pytestmark = pytest.mark.anyio +pydantic_version = pydantic.__version__[:3] + + +class BaseDocument(Document): + name: str = mongoz.String() + year: int = mongoz.Integer() + tags: Optional[List[str]] = mongoz.Array(str, null=True) + + class Meta: + abstract = True + registry = client + database = "test_db" + + +class Movie(BaseDocument): + uuid: Optional[ObjectId] = mongoz.ObjectId(null=True) + + +class Actor(BaseDocument): + movies: Optional[List[Any]] = mongoz.ArrayList(null=True) + + +@pytest.fixture(scope="function", autouse=True) +async def prepare_database() -> AsyncGenerator: + await Movie.query().delete() + await Actor.query().delete() + yield + await Movie.query().delete() + await Actor.query().delete() + + +async def test_array() -> None: + movies = await Movie.query().all() + assert len(movies) == 0 + + await Movie(name="Barbie", year=2023, tags=["barbie"]).create() + + movies = await Movie.query().all() + assert len(movies) == 1 + + cursor = Movie.query() + async for movie in cursor: + assert movie.name == "Barbie" + assert movie.tags == ["barbie"] + + await Movie(name="Batman", year=2022, tags=["detective", "dc"]).create() + + movies = await Movie.query().all() + + movie = movies[1] + assert movie.name == "Batman" + assert movie.tags == ["detective", "dc"] + + +async def test_array_error() -> None: + movies = await Movie.query().all() + assert len(movies) == 0 + + with pytest.raises(ValidationError): + await Movie(name="Barbie", year=2003, tags=["barbie", 1]).create() + + +async def test_model_nested_inherited() -> None: + actors = await Actor.query().all() + assert len(actors) == 0 + + await Actor(name="Paul Rudd", year=1972, movies=["Only Murders in the Building"]).create() + + actors = await Actor.query().all() + assert len(actors) == 1 + + actors = Actor.query() + async for movie in actors: + assert movie.name == "Paul Rudd" + assert movie.movies[0] == "Only Murders in the Building" + + await Actor(name="Margot Robbie", year=1990, movies=["Barbie", 3, 2.25]).create() + + actors = await Actor.query().all() + actor = actors[1] + + assert actor.name == "Margot Robbie" + assert actor.movies == ["Barbie", 3, 2.25]