Coverage for tests/test_ondelete_raises.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from typing import Any, List, Union 1klmnopqrst

2 

3import pytest 1klmnopqrst

4from sqlmodel import Field, Relationship, SQLModel 1klmnopqrst

5 

6 

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

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

9 

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

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

12 

13 heroes: List["Hero"] = Relationship( 1abcdefghij

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

15 ) 

16 

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

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

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

20 secret_name: str 1abcdefghij

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

22 

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

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

25 

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

27 

28 

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

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

31 

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

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

34 

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

36 

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