Coverage for tests/test_annotated_uuid.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

1import uuid 1nopqrstuvwxyz

2 

3from sqlalchemy import Engine 1nopqrstuvwxyz

4from sqlmodel import Field, Session, SQLModel, select 1nopqrstuvwxyz

5 

6 

7def test_annotated_optional_types(database_engine: Engine) -> None: 1nopqrstuvwxyz

8 from pydantic import UUID4 1abcdefghijklm

9 

10 class Hero(SQLModel, table=True): 1abcdefghijklm

11 # Pydantic UUID4 is: Annotated[UUID, UuidVersion(4)] 

12 id: UUID4 | None = Field(default_factory=uuid.uuid4, primary_key=True) 1abcdefghijklm

13 

14 engine = database_engine 1abcdefghijklm

15 SQLModel.metadata.create_all(engine) 1abcdefghijklm

16 with Session(engine) as db: 1abcdefghijklm

17 hero = Hero() 1abcdefghijklm

18 db.add(hero) 1abcdefghijklm

19 db.commit() 1abcdefghijklm

20 statement = select(Hero) 1abcdefghijklm

21 result = db.exec(statement).all() 1abcdefghijklm

22 assert len(result) == 1 1abcdefghijklm

23 assert isinstance(hero.id, uuid.UUID) 1abcdefghijklm