Coverage for tests / test_ondelete_raises.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1from typing import Any, Union 1jklmnopqr

2 

3import pytest 1jklmnopqr

4from sqlmodel import Field, Relationship, SQLModel 1jklmnopqr

5 

6 

7def test_ondelete_requires_nullable(clear_sqlmodel: Any) -> None: 1jklmnopqr

8 with pytest.raises(RuntimeError) as exc: 1abcdefghi

9 

10 class Team(SQLModel, table=True): 1abcdefghi

11 id: Union[int, None] = Field(default=None, primary_key=True) 1abcdefghi

12 

13 heroes: list["Hero"] = Relationship( 1abcdefghi

14 back_populates="team", passive_deletes="all" 

15 ) 

16 

17 class Hero(SQLModel, table=True): 1abcdefghi

18 id: Union[int, None] = Field(default=None, primary_key=True) 1abcdefghi

19 name: str = Field(index=True) 1abcdefghi

20 secret_name: str 1abcdefghi

21 age: Union[int, None] = Field(default=None, index=True) 1abcdefghi

22 

23 team_id: int = Field(foreign_key="team.id", ondelete="SET NULL") 1abcdefghi

24 team: Team = Relationship(back_populates="heroes") 1abcdefghi

25 

26 assert 'ondelete="SET NULL" requires nullable=True' in str(exc.value) 1abcdefghi

27 

28 

29def test_ondelete_requires_foreign_key(clear_sqlmodel: Any) -> None: 1jklmnopqr

30 with pytest.raises(RuntimeError) as exc: 1stuvwxyzA

31 

32 class Team(SQLModel, table=True): 1stuvwxyzA

33 id: Union[int, None] = Field(default=None, primary_key=True) 1stuvwxyzA

34 

35 age: int = Field(ondelete="CASCADE") 1stuvwxyzA

36 

37 assert "ondelete can only be used with foreign_key" in str(exc.value) 1stuvwxyzA