File: test_ondelete_raises.py

package info (click to toggle)
sqlmodel 0.0.24-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,128 kB
  • sloc: python: 34,496; javascript: 280; sh: 15; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 1,284 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import Any, List, Union

import pytest
from sqlmodel import Field, Relationship, SQLModel


def test_ondelete_requires_nullable(clear_sqlmodel: Any) -> None:
    with pytest.raises(RuntimeError) as exc:

        class Team(SQLModel, table=True):
            id: Union[int, None] = Field(default=None, primary_key=True)

            heroes: List["Hero"] = Relationship(
                back_populates="team", passive_deletes="all"
            )

        class Hero(SQLModel, table=True):
            id: Union[int, None] = Field(default=None, primary_key=True)
            name: str = Field(index=True)
            secret_name: str
            age: Union[int, None] = Field(default=None, index=True)

            team_id: int = Field(foreign_key="team.id", ondelete="SET NULL")
            team: Team = Relationship(back_populates="heroes")

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


def test_ondelete_requires_foreign_key(clear_sqlmodel: Any) -> None:
    with pytest.raises(RuntimeError) as exc:

        class Team(SQLModel, table=True):
            id: Union[int, None] = Field(default=None, primary_key=True)

            age: int = Field(ondelete="CASCADE")

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