Coverage for tests/test_field_sa_relationship.py: 100%
6 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
1from typing import List, Optional 1mnopqr
3import pytest 1mnopqr
4from sqlalchemy.orm import relationship 1mnopqr
5from sqlmodel import Field, Relationship, SQLModel 1mnopqr
8def test_sa_relationship_no_args() -> None: 1mnopqr
9 with pytest.raises(RuntimeError): # pragma: no cover 1cadefg
11 class Team(SQLModel, table=True): 1cadefg
12 id: Optional[int] = Field(default=None, primary_key=True) 1cadefg
13 name: str = Field(index=True) 1cadefg
14 headquarters: str 1cadefg
16 heroes: List["Hero"] = Relationship( 1cadefg
17 back_populates="team", 1cadefg
18 sa_relationship_args=["Hero"], 1cadefg
19 sa_relationship=relationship("Hero", back_populates="team"), 1cadefg
20 )
22 class Hero(SQLModel, table=True): 1a
23 id: Optional[int] = Field(default=None, primary_key=True)
24 name: str = Field(index=True)
25 secret_name: str
26 age: Optional[int] = Field(default=None, index=True)
28 team_id: Optional[int] = Field(default=None, foreign_key="team.id")
29 team: Optional[Team] = Relationship(back_populates="heroes")
32def test_sa_relationship_no_kwargs() -> None: 1mnopqr
33 with pytest.raises(RuntimeError): # pragma: no cover 1hbijkl
35 class Team(SQLModel, table=True): 1hbijkl
36 id: Optional[int] = Field(default=None, primary_key=True) 1hbijkl
37 name: str = Field(index=True) 1hbijkl
38 headquarters: str 1hbijkl
40 heroes: List["Hero"] = Relationship( 1hbijkl
41 back_populates="team", 1hbijkl
42 sa_relationship_kwargs={"lazy": "selectin"}, 1hbijkl
43 sa_relationship=relationship("Hero", back_populates="team"), 1hbijkl
44 )
46 class Hero(SQLModel, table=True): 1b
47 id: Optional[int] = Field(default=None, primary_key=True)
48 name: str = Field(index=True)
49 secret_name: str
50 age: Optional[int] = Field(default=None, index=True)
52 team_id: Optional[int] = Field(default=None, foreign_key="team.id")
53 team: Optional[Team] = Relationship(back_populates="heroes")