Coverage for tests/test_annotated_uuid.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1import uuid 1ghijkl

2from typing import Optional 1ghijkl

3 

4from sqlmodel import Field, Session, SQLModel, create_engine, select 1ghijkl

5 

6from tests.conftest import needs_pydanticv2 1ghijkl

7 

8 

9@needs_pydanticv2 1ghijkl

10def test_annotated_optional_types(clear_sqlmodel) -> None: 1ghijkl

11 from pydantic import UUID4 1abcdef

12 

13 class Hero(SQLModel, table=True): 1abcdef

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

15 id: Optional[UUID4] = Field(default_factory=uuid.uuid4, primary_key=True) 1abcdef

16 

17 engine = create_engine("sqlite:///:memory:") 1abcdef

18 SQLModel.metadata.create_all(engine) 1abcdef

19 with Session(engine) as db: 1abcdef

20 hero = Hero() 1abcdef

21 db.add(hero) 1abcdef

22 db.commit() 1abcdef

23 statement = select(Hero) 1abcdef

24 result = db.exec(statement).all() 1abcdef

25 assert len(result) == 1 1abcdef

26 assert isinstance(hero.id, uuid.UUID) 1abcdef