Coverage for tests/test_ondelete_raises.py: 100%
22 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-12 19:13 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-12 19:13 +0000
1from typing import Any, List, Union 1abcdef
3import pytest 1abcdef
4from sqlmodel import Field, Relationship, SQLModel 1abcdef
7def test_ondelete_requires_nullable(clear_sqlmodel: Any) -> None: 1abcdef
8 with pytest.raises(RuntimeError) as exc: 1abcdef
10 class Team(SQLModel, table=True): 1abcdef
11 id: Union[int, None] = Field(default=None, primary_key=True) 1abcdef
13 heroes: List["Hero"] = Relationship( 1abcdef
14 back_populates="team", passive_deletes="all"
15 )
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
23 team_id: int = Field(foreign_key="team.id", ondelete="SET NULL") 1abcdef
24 team: Team = Relationship(back_populates="heroes") 1abcdef
26 assert 'ondelete="SET NULL" requires nullable=True' in str(exc.value) 1abcdef
29def test_ondelete_requires_foreign_key(clear_sqlmodel: Any) -> None: 1abcdef
30 with pytest.raises(RuntimeError) as exc: 1abcdef
32 class Team(SQLModel, table=True): 1abcdef
33 id: Union[int, None] = Field(default=None, primary_key=True) 1abcdef
35 age: int = Field(ondelete="CASCADE") 1abcdef
37 assert "ondelete can only be used with foreign_key" in str(exc.value) 1abcdef