Coverage for tests/test_ondelete_raises.py: 100%

22 statements  

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

1from typing import Any, List, Union 1ghijkl

2 

3import pytest 1ghijkl

4from sqlmodel import Field, Relationship, SQLModel 1ghijkl

5 

6 

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

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

9 

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

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

12 

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

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

15 ) 

16 

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

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

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

20 secret_name: str 1abcdef

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

22 

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

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

25 

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

27 

28 

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

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

31 

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

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

34 

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

36 

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