Coverage for tests/test_main.py: 100%

87 statements  

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

1from typing import List, Optional 1yzABCD

2 

3import pytest 1yzABCD

4from sqlalchemy.exc import IntegrityError 1yzABCD

5from sqlalchemy.orm import RelationshipProperty 1yzABCD

6from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select 1yzABCD

7 

8 

9def test_should_allow_duplicate_row_if_unique_constraint_is_not_passed(clear_sqlmodel): 1yzABCD

10 class Hero(SQLModel, table=True): 1abcdef

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

12 name: str 1abcdef

13 secret_name: str 1abcdef

14 age: Optional[int] = None 1abcdef

15 

16 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef

17 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef

18 

19 engine = create_engine("sqlite://") 1abcdef

20 

21 SQLModel.metadata.create_all(engine) 1abcdef

22 

23 with Session(engine) as session: 1abcdef

24 session.add(hero_1) 1abcdef

25 session.commit() 1abcdef

26 session.refresh(hero_1) 1abcdef

27 

28 with Session(engine) as session: 1abcdef

29 session.add(hero_2) 1abcdef

30 session.commit() 1abcdef

31 session.refresh(hero_2) 1abcdef

32 

33 with Session(engine) as session: 1abcdef

34 heroes = session.exec(select(Hero)).all() 1abcdef

35 assert len(heroes) == 2 1abcdef

36 assert heroes[0].name == heroes[1].name 1abcdef

37 

38 

39def test_should_allow_duplicate_row_if_unique_constraint_is_false(clear_sqlmodel): 1yzABCD

40 class Hero(SQLModel, table=True): 1ghijkl

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

42 name: str 1ghijkl

43 secret_name: str = Field(unique=False) 1ghijkl

44 age: Optional[int] = None 1ghijkl

45 

46 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1ghijkl

47 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1ghijkl

48 

49 engine = create_engine("sqlite://") 1ghijkl

50 

51 SQLModel.metadata.create_all(engine) 1ghijkl

52 

53 with Session(engine) as session: 1ghijkl

54 session.add(hero_1) 1ghijkl

55 session.commit() 1ghijkl

56 session.refresh(hero_1) 1ghijkl

57 

58 with Session(engine) as session: 1ghijkl

59 session.add(hero_2) 1ghijkl

60 session.commit() 1ghijkl

61 session.refresh(hero_2) 1ghijkl

62 

63 with Session(engine) as session: 1ghijkl

64 heroes = session.exec(select(Hero)).all() 1ghijkl

65 assert len(heroes) == 2 1ghijkl

66 assert heroes[0].name == heroes[1].name 1ghijkl

67 

68 

69def test_should_raise_exception_when_try_to_duplicate_row_if_unique_constraint_is_true( 1yzABCD

70 clear_sqlmodel, 

71): 

72 class Hero(SQLModel, table=True): 1stuvwx

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

74 name: str 1stuvwx

75 secret_name: str = Field(unique=True) 1stuvwx

76 age: Optional[int] = None 1stuvwx

77 

78 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1stuvwx

79 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1stuvwx

80 

81 engine = create_engine("sqlite://") 1stuvwx

82 

83 SQLModel.metadata.create_all(engine) 1stuvwx

84 

85 with Session(engine) as session: 1stuvwx

86 session.add(hero_1) 1stuvwx

87 session.commit() 1stuvwx

88 session.refresh(hero_1) 1stuvwx

89 

90 with pytest.raises(IntegrityError): 1stuvwx

91 with Session(engine) as session: 1stuvwx

92 session.add(hero_2) 1stuvwx

93 session.commit() 1stuvwx

94 

95 

96def test_sa_relationship_property(clear_sqlmodel): 1yzABCD

97 """Test https://github.com/tiangolo/sqlmodel/issues/315#issuecomment-1272122306""" 

98 

99 class Team(SQLModel, table=True): 1mnopqr

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

101 name: str = Field(unique=True) 1mnopqr

102 heroes: List["Hero"] = Relationship( # noqa: F821 1mnopqr

103 sa_relationship=RelationshipProperty("Hero", back_populates="team") 

104 ) 

105 

106 class Hero(SQLModel, table=True): 1mnopqr

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

108 name: str = Field(unique=True) 1mnopqr

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

110 team: Optional[Team] = Relationship( 1mnopqr

111 sa_relationship=RelationshipProperty("Team", back_populates="heroes") 

112 ) 

113 

114 team_preventers = Team(name="Preventers") 1mnopqr

115 hero_rusty_man = Hero(name="Rusty-Man", team=team_preventers) 1mnopqr

116 

117 engine = create_engine("sqlite://", echo=True) 1mnopqr

118 

119 SQLModel.metadata.create_all(engine) 1mnopqr

120 

121 with Session(engine) as session: 1mnopqr

122 session.add(hero_rusty_man) 1mnopqr

123 session.commit() 1mnopqr

124 session.refresh(hero_rusty_man) 1mnopqr

125 # The next statement should not raise an AttributeError 

126 assert hero_rusty_man.team 1mnopqr

127 assert hero_rusty_man.team.name == "Preventers" 1mnopqr