Coverage for tests/test_read_with_orm_mode.py: 100%

64 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import Any 1abcde

2 

3from fastapi import FastAPI 1abcde

4from fastapi.testclient import TestClient 1abcde

5from pydantic import BaseModel, ConfigDict 1abcde

6 

7from .utils import needs_pydanticv1, needs_pydanticv2 1abcde

8 

9 

10@needs_pydanticv2 1abcde

11def test_read_with_orm_mode() -> None: 1abcde

12 class PersonBase(BaseModel): 1abcde

13 name: str 1abcde

14 lastname: str 1abcde

15 

16 class Person(PersonBase): 1abcde

17 @property 1abcde

18 def full_name(self) -> str: 1abcde

19 return f"{self.name} {self.lastname}" 1abcde

20 

21 model_config = ConfigDict(from_attributes=True) 1abcde

22 

23 class PersonCreate(PersonBase): 1abcde

24 pass 1abcde

25 

26 class PersonRead(PersonBase): 1abcde

27 full_name: str 1abcde

28 

29 model_config = {"from_attributes": True} 1abcde

30 

31 app = FastAPI() 1abcde

32 

33 @app.post("/people/", response_model=PersonRead) 1abcde

34 def create_person(person: PersonCreate) -> Any: 1abcde

35 db_person = Person.model_validate(person) 1abcde

36 return db_person 1abcde

37 

38 client = TestClient(app) 1abcde

39 

40 person_data = {"name": "Dive", "lastname": "Wilson"} 1abcde

41 response = client.post("/people/", json=person_data) 1abcde

42 data = response.json() 1abcde

43 assert response.status_code == 200, response.text 1abcde

44 assert data["name"] == person_data["name"] 1abcde

45 assert data["lastname"] == person_data["lastname"] 1abcde

46 assert data["full_name"] == person_data["name"] + " " + person_data["lastname"] 1abcde

47 

48 

49@needs_pydanticv1 1abcde

50def test_read_with_orm_mode_pv1() -> None: 1abcde

51 class PersonBase(BaseModel): 1abcde

52 name: str 1abcde

53 lastname: str 1abcde

54 

55 class Person(PersonBase): 1abcde

56 @property 1abcde

57 def full_name(self) -> str: 1abcde

58 return f"{self.name} {self.lastname}" 1abcde

59 

60 class Config: 1abcde

61 orm_mode = True 1abcde

62 read_with_orm_mode = True 1abcde

63 

64 class PersonCreate(PersonBase): 1abcde

65 pass 1abcde

66 

67 class PersonRead(PersonBase): 1abcde

68 full_name: str 1abcde

69 

70 class Config: 1abcde

71 orm_mode = True 1abcde

72 

73 app = FastAPI() 1abcde

74 

75 @app.post("/people/", response_model=PersonRead) 1abcde

76 def create_person(person: PersonCreate) -> Any: 1abcde

77 db_person = Person.from_orm(person) 1abcde

78 return db_person 1abcde

79 

80 client = TestClient(app) 1abcde

81 

82 person_data = {"name": "Dive", "lastname": "Wilson"} 1abcde

83 response = client.post("/people/", json=person_data) 1abcde

84 data = response.json() 1abcde

85 assert response.status_code == 200, response.text 1abcde

86 assert data["name"] == person_data["name"] 1abcde

87 assert data["lastname"] == person_data["lastname"] 1abcde

88 assert data["full_name"] == person_data["name"] + " " + person_data["lastname"] 1abcde