Coverage for tests / test_read_with_orm_mode.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Any 1efgh
3from fastapi import FastAPI 1efgh
4from fastapi.testclient import TestClient 1efgh
5from pydantic import BaseModel, ConfigDict 1efgh
8def test_read_with_orm_mode() -> None: 1efgh
9 class PersonBase(BaseModel): 1adbc
10 name: str 1abc
11 lastname: str 1abc
13 class Person(PersonBase): 1adbc
14 @property 1adbc
15 def full_name(self) -> str: 1adbc
16 return f"{self.name} {self.lastname}" 1adbc
18 model_config = ConfigDict(from_attributes=True) 1adbc
20 class PersonCreate(PersonBase): 1adbc
21 pass 1adbc
23 class PersonRead(PersonBase): 1adbc
24 full_name: str 1abc
26 model_config = {"from_attributes": True} 1adbc
28 app = FastAPI() 1adbc
30 @app.post("/people/", response_model=PersonRead) 1adbc
31 def create_person(person: PersonCreate) -> Any: 1adbc
32 db_person = Person.model_validate(person) 1adbc
33 return db_person 1adbc
35 client = TestClient(app) 1adbc
37 person_data = {"name": "Dive", "lastname": "Wilson"} 1adbc
38 response = client.post("/people/", json=person_data) 1adbc
39 data = response.json() 1adbc
40 assert response.status_code == 200, response.text 1adbc
41 assert data["name"] == person_data["name"] 1adbc
42 assert data["lastname"] == person_data["lastname"] 1adbc
43 assert data["full_name"] == person_data["name"] + " " + person_data["lastname"] 1adbc