Replies: 1 comment
-
This passes for me import uuid
from http import HTTPStatus
from typing import List
import databases
import pytest
import sqlalchemy
from fastapi import FastAPI
from starlette.testclient import TestClient
import ormar
from tests.settings import DATABASE_URL
metadata = sqlalchemy.MetaData()
database = databases.Database(DATABASE_URL)
class BaseMeta(ormar.ModelMeta):
database = database
metadata = metadata
class Trigger(ormar.Model):
class Meta(BaseMeta):
tablename = 'trigger'
# note uuid4 not uuid(), set a callable to set it on instance creation, setting uuid4() is setting the same one for all
id: uuid.UUID = ormar.UUID(primary_key=True, uuid_format='string', default=uuid.uuid4)
name: str = ormar.String(max_length=300)
description: str = ormar.Text()
class Notification(ormar.Model):
class Meta(BaseMeta):
tablename = 'notification'
id: uuid.UUID = ormar.UUID(primary_key=True, uuid_format='string', default=uuid.uuid4)
name: str = ormar.String(max_length=300)
triggers: List[Trigger] = ormar.ManyToMany(Trigger)
app = FastAPI()
@app.get(
'/notifications',
response_model=List[Notification]
)
async def list_notifications():
notifications = await Notification.objects.select_related("triggers").all()
return notifications
test_trigger = {
'id': '3d2fbf75-c35c-46c2-ae5d-8f19c8046b2d',
'name': 'Test Trigger 1',
'description': 'This trigger exists for testing',
}
test_notification = {
'id': '260ff0e7-e3d4-4967-8de0-c16e619bfe51',
'name': 'Test Notification 1',
}
@pytest.mark.asyncio
@pytest.fixture(autouse=True, scope='function')
async def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
BaseMeta.metadata.drop_all(engine)
BaseMeta.metadata.create_all(engine)
trigger1 = Trigger(**test_trigger)
await trigger1.save()
notification1 = Notification(**test_notification)
await notification1.save()
await notification1.triggers.add(trigger1)
await notification1.update()
yield
BaseMeta.metadata.drop_all(engine)
@pytest.fixture
def api_client():
client = TestClient(app=app)
return client
@pytest.mark.asyncio
async def test_list_notifications(api_client):
with api_client:
response = api_client.get('/notifications')
assert response.status_code == HTTPStatus.OK
content = response.json()
assert len(content) == 1
notification = Notification(**content[0])
assert notification.triggers is not None
assert notification.triggers[0].name == "Test Trigger 1" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey there,
I've got some models with many to many relationships. When I want to return a list of one of the models together with a nested model on a FastAPI endpoint it fails on the Reponse model validation.
Here is a condensed version of what I'm doing (some field and Models omitted):
When I run the test following error appears:
When I remove the response model the tests works. I debugged into the query result and it correctly returns a list of one Notification with a list of one Trigger embedded.
I'm out of ideas what the issue could be. This seems pretty analogous to the tutorial but still fails.
Beta Was this translation helpful? Give feedback.
All reactions