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
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from typing import List, Optional 1yzABCD
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
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
16 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef
17 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1abcdef
19 engine = create_engine("sqlite://") 1abcdef
21 SQLModel.metadata.create_all(engine) 1abcdef
23 with Session(engine) as session: 1abcdef
24 session.add(hero_1) 1abcdef
25 session.commit() 1abcdef
26 session.refresh(hero_1) 1abcdef
28 with Session(engine) as session: 1abcdef
29 session.add(hero_2) 1abcdef
30 session.commit() 1abcdef
31 session.refresh(hero_2) 1abcdef
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
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
46 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1ghijkl
47 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1ghijkl
49 engine = create_engine("sqlite://") 1ghijkl
51 SQLModel.metadata.create_all(engine) 1ghijkl
53 with Session(engine) as session: 1ghijkl
54 session.add(hero_1) 1ghijkl
55 session.commit() 1ghijkl
56 session.refresh(hero_1) 1ghijkl
58 with Session(engine) as session: 1ghijkl
59 session.add(hero_2) 1ghijkl
60 session.commit() 1ghijkl
61 session.refresh(hero_2) 1ghijkl
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
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
78 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") 1stuvwx
79 hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") 1stuvwx
81 engine = create_engine("sqlite://") 1stuvwx
83 SQLModel.metadata.create_all(engine) 1stuvwx
85 with Session(engine) as session: 1stuvwx
86 session.add(hero_1) 1stuvwx
87 session.commit() 1stuvwx
88 session.refresh(hero_1) 1stuvwx
90 with pytest.raises(IntegrityError): 1stuvwx
91 with Session(engine) as session: 1stuvwx
92 session.add(hero_2) 1stuvwx
93 session.commit() 1stuvwx
96def test_sa_relationship_property(clear_sqlmodel): 1yzABCD
97 """Test https://github.com/tiangolo/sqlmodel/issues/315#issuecomment-1272122306"""
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 )
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 )
114 team_preventers = Team(name="Preventers") 1mnopqr
115 hero_rusty_man = Hero(name="Rusty-Man", team=team_preventers) 1mnopqr
117 engine = create_engine("sqlite://", echo=True) 1mnopqr
119 SQLModel.metadata.create_all(engine) 1mnopqr
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