-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for lilya * Add meta errors test for database * Update requirements * Fix linting
- Loading branch information
Showing
6 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
) |