File: test_field_sa_relationship.py

package info (click to toggle)
sqlmodel 0.0.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,344 kB
  • sloc: python: 16,107; javascript: 283; sh: 16; makefile: 7
file content (51 lines) | stat: -rw-r--r-- 1,929 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pytest
from sqlalchemy.orm import relationship
from sqlmodel import Field, Relationship, SQLModel


def test_sa_relationship_no_args() -> None:
    with pytest.raises(RuntimeError):  # pragma: no cover

        class Team(SQLModel, table=True):
            id: int | None = Field(default=None, primary_key=True)
            name: str = Field(index=True)
            headquarters: str

            heroes: list["Hero"] = Relationship(
                back_populates="team",
                sa_relationship_args=["Hero"],
                sa_relationship=relationship("Hero", back_populates="team"),
            )

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

            team_id: int | None = Field(default=None, foreign_key="team.id")
            team: Team | None = Relationship(back_populates="heroes")


def test_sa_relationship_no_kwargs() -> None:
    with pytest.raises(RuntimeError):  # pragma: no cover

        class Team(SQLModel, table=True):
            id: int | None = Field(default=None, primary_key=True)
            name: str = Field(index=True)
            headquarters: str

            heroes: list["Hero"] = Relationship(
                back_populates="team",
                sa_relationship_kwargs={"lazy": "selectin"},
                sa_relationship=relationship("Hero", back_populates="team"),
            )

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

            team_id: int | None = Field(default=None, foreign_key="team.id")
            team: Team | None = Relationship(back_populates="heroes")