|
1 |
| -from typing import Optional |
| 1 | +from typing import List, Optional |
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from pydantic.error_wrappers import ValidationError
|
5 |
| -from sqlmodel import SQLModel |
| 5 | +from sqlmodel import Session, SQLModel, create_engine |
| 6 | +from sqlmodel.main import Field, Relationship |
6 | 7 |
|
7 | 8 | from .conftest import needs_pydanticv1, needs_pydanticv2
|
8 | 9 |
|
@@ -63,3 +64,71 @@ def reject_none(cls, v):
|
63 | 64 |
|
64 | 65 | with pytest.raises(ValidationError):
|
65 | 66 | Hero.model_validate({"name": None, "age": 25})
|
| 67 | + |
| 68 | + |
| 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 not session.dirty |
| 95 | + assert not session.new |
| 96 | + |
| 97 | + Hero.validate(hero) |
| 98 | + |
| 99 | + assert not session.dirty |
| 100 | + assert not session.new |
| 101 | + |
| 102 | + |
| 103 | +@needs_pydanticv2 |
| 104 | +def test_validation_related_object_not_in_session_pydantic_v2(clear_sqlmodel): |
| 105 | + class Team(SQLModel, table=True): |
| 106 | + id: Optional[int] = Field(default=None, primary_key=True) |
| 107 | + name: str |
| 108 | + heroes: List["Hero"] = Relationship(back_populates="team") |
| 109 | + |
| 110 | + class Hero(SQLModel, table=True): |
| 111 | + id: Optional[int] = Field(default=None, primary_key=True) |
| 112 | + name: str |
| 113 | + |
| 114 | + team_id: Optional[int] = Field(default=None, foreign_key="team.id") |
| 115 | + team: Optional[Team] = Relationship(back_populates="heroes") |
| 116 | + |
| 117 | + engine = create_engine("sqlite://") |
| 118 | + SQLModel.metadata.create_all(engine) |
| 119 | + team = Team(name="team") |
| 120 | + hero = Hero(name="hero", team=team) |
| 121 | + with Session(engine) as session: |
| 122 | + session.add(team) |
| 123 | + session.add(hero) |
| 124 | + session.commit() |
| 125 | + |
| 126 | + with Session(engine) as session: |
| 127 | + hero = session.get(Hero, 1) |
| 128 | + assert not session.dirty |
| 129 | + assert not session.new |
| 130 | + |
| 131 | + Hero.model_validate(hero) |
| 132 | + |
| 133 | + assert not session.dirty |
| 134 | + assert not session.new |
0 commit comments