Skip to content

Commit

Permalink
Fix/databases (#26)
Browse files Browse the repository at this point in the history
* Add support for lilya
* Add meta errors test for database
* Update requirements
* Fix linting
  • Loading branch information
tarsil authored Mar 28, 2024
1 parent b9fc884 commit 1f40a9f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mongoz/conf/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __deepcopy__(self, memo: Any) -> Any:
__getitem__ = new_method_proxy(operator.getitem)
__setitem__ = new_method_proxy(operator.setitem)
__delitem__ = new_method_proxy(operator.delitem)
__iter__ = new_method_proxy(iter)
__iter__ = new_method_proxy(iter) # type: ignore
__len__ = new_method_proxy(len)
__contains__ = new_method_proxy(operator.contains)

Expand Down
4 changes: 2 additions & 2 deletions mongoz/core/connection/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def address(self) -> Tuple[str, int]:

@property
def host(self) -> str:
return cast(str, self._client.HOST)
return self._client.HOST

@property
def port(self) -> str:
return cast(str, self._client.PORT)

@property
def driver(self) -> AsyncIOMotorDatabase:
return cast(AsyncIOMotorDatabase, self._client.driver)
return self._client.driver

async def drop_database(self, database: Union[str, Database]) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion mongoz/core/db/documents/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _check_document_inherited_database(

if not found_database:
raise ImproperlyConfigured(
"Database for the table not found in the Meta class or any of the superclasses. You must set the database in the Meta."
"'database' for the table not found in the Meta class or any of the superclasses. You must set the database in the Meta."
)
return found_database

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ test = [
"a2wsgi>1.10.0,<2.0.0",
"autoflake>=2.0.2,<3.0.0",
"black==24.1.1,<25.0",
"esmerald>=2.0.6",
"esmerald>=3.0.0",
"httpx>=0.25.0,<0.30.0",
"isort>=5.12.0,<6.0.0",
"mypy==1.5.1",
"mypy==1.9.0",
"pytest>=7.2.2,<9.0.0",
"pytest-asyncio>=0.21.1,<1.0.0",
"pytest-cov>=4.0.0,<5.0.0",
Expand Down Expand Up @@ -100,7 +100,7 @@ implicit_reexport = false
no_implicit_optional = false
show_error_codes = true
disallow_incomplete_defs = true
disable_error_code = "attr-defined,arg-type,override,misc,valid-type,call-overload"
disable_error_code = "attr-defined,arg-type,override,misc,valid-type,call-overload,no-any-return"
warn_unused_ignores = true
warn_redundant_casts = true

Expand Down
70 changes: 70 additions & 0 deletions tests/integrations/test_lilya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json

import pytest
from lilya import status
from lilya.apps import Lilya
from lilya.requests import Request
from lilya.responses import Ok
from lilya.routing import Path
from lilya.testclient import TestClient
from tests.conftest import client

import mongoz
from mongoz import Document

pytestmark = pytest.mark.anyio


class Movie(Document):
name: str = mongoz.String()
year: int = mongoz.Integer()

class Meta:
registry = client
database = "test_db"


@pytest.fixture(scope="module", autouse=True)
async def prepare_database() -> None:
await Movie.query().delete()


async def create_movies(request: Request):
data = await request.json()
movie = await Movie(**data).create()
return Ok(
json.loads(movie.model_dump_json()),
status_code=status.HTTP_201_CREATED,
)


async def get_movies(request: Request):
movie = await Movie.query().get()
return Ok(json.loads(movie.model_dump_json()))


app = Lilya(
routes=[
Path("/all", handler=get_movies),
Path("/create", handler=create_movies, methods=["POST"]),
]
)


async def test_lilya_integration_create() -> None:
with TestClient(app) as client:
response = client.post("/create", json={"name": "Barbie", "year": 2023})

assert response.json()["name"] == "Barbie"
assert response.json()["year"] == 2023
assert response.status_code == 201


async def test_lilya_integration_read() -> None:
with TestClient(app) as client:
client.post("/create", json={"name": "Barbie", "year": 2023})
response = client.get("/all")

assert response.json()["name"] == "Barbie"
assert response.json()["year"] == 2023
assert response.status_code == 200
31 changes: 31 additions & 0 deletions tests/test_meta_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List, Optional

import pydantic
import pytest
from tests.conftest import client

import mongoz
from mongoz import Document, ObjectId
from mongoz.exceptions import ImproperlyConfigured

pytestmark = pytest.mark.anyio
pydantic_version = pydantic.__version__[:3]


async def test_improperly_configured_for_missing_database():
with pytest.raises(ImproperlyConfigured) as raised:

class Movie(Document):
name: str = mongoz.String()
year: int = mongoz.Integer()
tags: Optional[List[str]] = mongoz.Array(str, null=True)
uuid: Optional[ObjectId] = mongoz.ObjectId(null=True)
is_published: bool = mongoz.Boolean(default=False)

class Meta:
registry = client

assert (
raised.value.args[0]
== "'database' for the table not found in the Meta class or any of the superclasses. You must set the database in the Meta."
)

0 comments on commit 1f40a9f

Please sign in to comment.