Coverage for tests/test_field_sa_args_kwargs.py: 100%

21 statements  

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

1from typing import Optional 1lmnopq

2 

3from sqlalchemy import ForeignKey 1lmnopq

4from sqlmodel import Field, SQLModel, create_engine 1lmnopq

5 

6 

7def test_sa_column_args(clear_sqlmodel, caplog) -> None: 1lmnopq

8 class Team(SQLModel, table=True): 1fabcde

9 id: Optional[int] = Field(default=None, primary_key=True) 1fabcde

10 name: str 1fabcde

11 

12 class Hero(SQLModel, table=True): 1fabcde

13 id: Optional[int] = Field(default=None, primary_key=True) 1fabcde

14 team_id: Optional[int] = Field( 1fabcde

15 default=None, 

16 sa_column_args=[ForeignKey("team.id")], 

17 ) 

18 

19 engine = create_engine("sqlite://", echo=True) 1fabcde

20 SQLModel.metadata.create_all(engine) 1fabcde

21 create_table_log = [ 1abcde

22 message for message in caplog.messages if "CREATE TABLE hero" in message 

23 ][0] 

24 assert "FOREIGN KEY(team_id) REFERENCES team (id)" in create_table_log 1fabcde

25 

26 

27def test_sa_column_kargs(clear_sqlmodel, caplog) -> None: 1lmnopq

28 class Item(SQLModel, table=True): 1rghijk

29 id: Optional[int] = Field( 1rghijk

30 default=None, 

31 sa_column_kwargs={"primary_key": True}, 

32 ) 

33 

34 engine = create_engine("sqlite://", echo=True) 1rghijk

35 SQLModel.metadata.create_all(engine) 1rghijk

36 create_table_log = [ 1ghijk

37 message for message in caplog.messages if "CREATE TABLE item" in message 

38 ][0] 

39 assert "PRIMARY KEY (id)" in create_table_log 1rghijk