Skip to content

Commit 915c51a

Browse files
authored
Simplify tests
1 parent 7e7520f commit 915c51a

File tree

1 file changed

+6
-38
lines changed

1 file changed

+6
-38
lines changed

tests/test_validation.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from pydantic.error_wrappers import ValidationError
55
from sqlmodel import Session, SQLModel, create_engine
6+
from sqlmodel._compat import IS_PYDANTIC_V2
67
from sqlmodel.main import Field, Relationship
78

89
from .conftest import needs_pydanticv1, needs_pydanticv2
@@ -66,43 +67,7 @@ def reject_none(cls, v):
6667
Hero.model_validate({"name": None, "age": 25})
6768

6869

69-
@needs_pydanticv1
70-
def test_validation_related_object_not_in_session_pydantic_v1(clear_sqlmodel):
71-
class Team(SQLModel, table=True):
72-
id: Optional[int] = Field(default=None, primary_key=True)
73-
name: str
74-
heroes: List["Hero"] = Relationship(back_populates="team")
75-
76-
class Hero(SQLModel, table=True):
77-
id: Optional[int] = Field(default=None, primary_key=True)
78-
name: str
79-
80-
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
81-
team: Optional[Team] = Relationship(back_populates="heroes")
82-
83-
engine = create_engine("sqlite://")
84-
SQLModel.metadata.create_all(engine)
85-
team = Team(name="team")
86-
hero = Hero(name="hero", team=team)
87-
with Session(engine) as session:
88-
session.add(team)
89-
session.add(hero)
90-
session.commit()
91-
92-
with Session(engine) as session:
93-
hero = session.get(Hero, 1)
94-
assert session._is_clean()
95-
96-
new_hero = Hero.validate(hero)
97-
98-
assert session._is_clean()
99-
# The new hero is a different instance, but the team is the same
100-
assert id(new_hero) != id(hero)
101-
assert id(new_hero.team) == id(hero.team)
102-
103-
104-
@needs_pydanticv2
105-
def test_validation_related_object_not_in_session_pydantic_v2(clear_sqlmodel):
70+
def test_validation_related_object_not_in_session(clear_sqlmodel):
10671
class Team(SQLModel, table=True):
10772
id: Optional[int] = Field(default=None, primary_key=True)
10873
name: str
@@ -128,7 +93,10 @@ class Hero(SQLModel, table=True):
12893
hero = session.get(Hero, 1)
12994
assert session._is_clean()
13095

131-
new_hero = Hero.model_validate(hero)
96+
if IS_PYDANTIC_V2:
97+
new_hero = Hero.model_validate(hero)
98+
else:
99+
new_hero = Hero.validate(hero)
132100

133101
assert session._is_clean()
134102
# The new hero is a different instance, but the team is the same

0 commit comments

Comments
 (0)