Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fastapi example test not working and make deps raise SolverProblemError. #1247

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Added
- Added config_class option to pydantic model genator that allows the developer to customize the generated pydantic model's `Config` class. (#1048)
Fixed
^^^^^
- Fastapi example test not working. (#1029)
- Fix create index sql error. (#1202)
- Fix dependencies resolve error. (#1246)

0.19.2
------
Expand Down
32 changes: 12 additions & 20 deletions examples/fastapi/_tests.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
# mypy: no-disallow-untyped-decorators
# pylint: disable=E0611,E0401
import asyncio
from typing import Generator

import pytest
from fastapi.testclient import TestClient
from asgi_lifespan import LifespanManager
from httpx import AsyncClient
from main import app
from models import Users

from tortoise.contrib.test import finalizer, initializer


@pytest.fixture(scope="module")
def client() -> Generator:
initializer(["models"])
with TestClient(app) as c:
yield c
finalizer()
def anyio_backend():
return "asyncio"


@pytest.fixture(scope="module")
def event_loop(client: TestClient) -> Generator:
yield client.task.get_loop() # type: ignore
async def client():
async with LifespanManager(app):
async with AsyncClient(app=app, base_url="http://test") as c:
yield c


def test_create_user(client: TestClient, event_loop: asyncio.AbstractEventLoop): # nosec
response = client.post("/users", json={"username": "admin"})
@pytest.mark.anyio
async def test_create_user(client: AsyncClient): # nosec
response = await client.post("/users", json={"username": "admin"})
assert response.status_code == 200, response.text
data = response.json()
assert data["username"] == "admin"
assert "id" in data
user_id = data["id"]

async def get_user_by_db():
user = await Users.get(id=user_id)
return user

user_obj = event_loop.run_until_complete(get_user_by_db())
user_obj = await Users.get(id=user_id)
assert user_obj.id == user_id