Coverage for tests / test_pydanticv2_dataclasses_uuid_stringified_annotations.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from __future__ import annotations 1abcd

2 

3import uuid 1abcd

4from dataclasses import dataclass, field 1abcd

5from typing import Union 1abcd

6 

7from dirty_equals import IsUUID 1abcd

8from fastapi import FastAPI 1abcd

9from fastapi.testclient import TestClient 1abcd

10from inline_snapshot import snapshot 1abcd

11 

12 

13@dataclass 1abcd

14class Item: 1abcd

15 id: uuid.UUID 1abcd

16 name: str 1abcd

17 price: float 1abcd

18 tags: list[str] = field(default_factory=list) 1abcd

19 description: Union[str, None] = None 1abcd

20 tax: Union[float, None] = None 1abcd

21 

22 

23app = FastAPI() 1abcd

24 

25 

26@app.get("/item", response_model=Item) 1abcd

27async def read_item(): 1abcd

28 return { 1efg

29 "id": uuid.uuid4(), 

30 "name": "Island In The Moon", 

31 "price": 12.99, 

32 "description": "A place to be be playin' and havin' fun", 

33 "tags": ["breater"], 

34 } 

35 

36 

37client = TestClient(app) 1abcd

38 

39 

40def test_annotations(): 1abcd

41 response = client.get("/item") 1efg

42 assert response.status_code == 200, response.text 1efg

43 assert response.json() == snapshot( 1efg

44 { 

45 "id": IsUUID(), 

46 "name": "Island In The Moon", 

47 "price": 12.99, 

48 "tags": ["breater"], 

49 "description": "A place to be be playin' and havin' fun", 

50 "tax": None, 

51 } 

52 )