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