Coverage for tests / test_ondelete_raises.py: 100%

22 statements  

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

1from typing import Any 1ijklmnop

2 

3import pytest 1ijklmnop

4from sqlmodel import Field, Relationship, SQLModel 1ijklmnop

5 

6 

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

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

9 

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

11 id: int | None = Field(default=None, primary_key=True) 1abcdefgh

12 

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

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

15 ) 

16 

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

18 id: int | None = Field(default=None, primary_key=True) 1abcdefgh

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

20 secret_name: str 1abcdefgh

21 age: int | None = Field(default=None, index=True) 1abcdefgh

22 

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

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

25 

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

27 

28 

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

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

31 

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

33 id: int | None = Field(default=None, primary_key=True) 1qrstuvwx

34 

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

36 

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