Skip to content

Commit

Permalink
Add tests to Array and ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Sep 25, 2023
1 parent f46a493 commit b3e00a2
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mongoz/core/db/documents/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions tests/inheritance/test_inheritance.py
Original file line number Diff line number Diff line change
@@ -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"
94 changes: 94 additions & 0 deletions tests/models/test_array_and_array_field.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit b3e00a2

Please sign in to comment.