Coverage for tests/test_field_sa_relationship.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import List, Optional 1mnopqr

2 

3import pytest 1mnopqr

4from sqlalchemy.orm import relationship 1mnopqr

5from sqlmodel import Field, Relationship, SQLModel 1mnopqr

6 

7 

8def test_sa_relationship_no_args() -> None: 1mnopqr

9 with pytest.raises(RuntimeError): # pragma: no cover 1cdaefg

10 

11 class Team(SQLModel, table=True): 1cdaefg

12 id: Optional[int] = Field(default=None, primary_key=True) 1cdaefg

13 name: str = Field(index=True) 1cdaefg

14 headquarters: str 1cdaefg

15 

16 heroes: List["Hero"] = Relationship( 1cdaefg

17 back_populates="team", 1cdaefg

18 sa_relationship_args=["Hero"], 1cdaefg

19 sa_relationship=relationship("Hero", back_populates="team"), 1cdaefg

20 ) 

21 

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) 

27 

28 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 

29 team: Optional[Team] = Relationship(back_populates="heroes") 

30 

31 

32def test_sa_relationship_no_kwargs() -> None: 1mnopqr

33 with pytest.raises(RuntimeError): # pragma: no cover 1hibjkl

34 

35 class Team(SQLModel, table=True): 1hibjkl

36 id: Optional[int] = Field(default=None, primary_key=True) 1hibjkl

37 name: str = Field(index=True) 1hibjkl

38 headquarters: str 1hibjkl

39 

40 heroes: List["Hero"] = Relationship( 1hibjkl

41 back_populates="team", 1hibjkl

42 sa_relationship_kwargs={"lazy": "selectin"}, 1hibjkl

43 sa_relationship=relationship("Hero", back_populates="team"), 1hibjkl

44 ) 

45 

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) 

51 

52 team_id: Optional[int] = Field(default=None, foreign_key="team.id") 

53 team: Optional[Team] = Relationship(back_populates="heroes")