Skip to content

Commit

Permalink
fix: blacksheep example unittest error (#1534) (#1539)
Browse files Browse the repository at this point in the history
* fix: blacksheep example unittest error (#1534)

* Update poetry lock file

* Upgrade coveralls to fix report upload error
  • Loading branch information
waketzheng committed Apr 28, 2024
1 parent c619625 commit 986e1b2
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 513 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Fixed
- Fix pydantic v2.5 unittest error. (#1535)
- Fix pydantic_model_creator `exclude_readonly` parameter not working.
- Fix annotation propagation for non-filter queries. (#1590)
- Fix blacksheep example unittest error. (#1534)

0.20.0
------
Expand Down
33 changes: 17 additions & 16 deletions examples/blacksheep/_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import asyncio

import pytest
import pytest_asyncio
from blacksheep import JSONContent
from blacksheep.testing import TestClient
from models import Users
from server import app


@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def client(api):
return TestClient(api)

Expand All @@ -21,42 +22,42 @@ def event_loop(request):
loop.close()


@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def api():
await app.start()
yield app
await app.stop()


@pytest.mark.asyncio
async def test_create_user(client: TestClient) -> None:
async def test_get_uses_list(client: TestClient) -> None:
username = "john"
await Users.create(username=username)

response = await client.post("/", content=JSONContent({"username": username}))
assert response.status == 201 # nosec
response = await client.get("/")
assert response.status == 200 # nosec

data = await response.json()
assert data["username"] == username # nosec
user_id = data["id"]
assert len(data) == 1 # nosec

user_data = data[0]
assert user_data["username"] == username # nosec
user_id = user_data["id"]

user_obj = await Users.get(id=user_id)
assert str(user_obj.id) == user_id # nosec


@pytest.mark.asyncio
async def test_get_uses_list(client: TestClient) -> None:
async def test_create_user(client: TestClient) -> None:
username = "john"
await Users.create(username=username)

response = await client.get("/")
assert response.status == 200 # nosec
response = await client.post("/", content=JSONContent({"username": username}))
assert response.status == 201 # nosec

data = await response.json()
assert len(data) == 1 # nosec

user_data = data[0]
assert user_data["username"] == username # nosec
user_id = user_data["id"]
assert data["username"] == username # nosec
user_id = data["id"]

user_obj = await Users.get(id=user_id)
assert str(user_obj.id) == user_id # nosec
Expand Down
4 changes: 0 additions & 4 deletions examples/blacksheep/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import tortoise
from tortoise import fields, models
from tortoise.contrib.pydantic import pydantic_model_creator

if tortoise.__version__ >= "0.20":
raise RuntimeError("blacksheep not support pydantic V2, use tortoise-orm<0.20 instead!")


class Users(models.Model):
id = fields.UUIDField(pk=True)
Expand Down
4 changes: 2 additions & 2 deletions examples/blacksheep/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ async def users_list() -> Union[UserPydanticOut]:

@app.router.post("/")
async def users_create(user: UserPydanticIn) -> UserPydanticOut:
user = await Users.create(**user.dict(exclude_unset=True))
user = await Users.create(**user.model_dump(exclude_unset=True))
return created(await UserPydanticOut.from_tortoise_orm(user))


@app.router.patch("/{id}")
async def users_patch(id: UUID, user: UserPydanticIn) -> UserPydanticOut:
await Users.filter(id=id).update(**user.dict(exclude_unset=True))
await Users.filter(id=id).update(**user.model_dump(exclude_unset=True))
return ok(await UserPydanticOut.from_tortoise_orm(await Users.get(id=id)))


Expand Down
Loading

0 comments on commit 986e1b2

Please sign in to comment.