-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unique constraint param to Field function
- Loading branch information
1 parent
4d20051
commit 2b39e32
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pytest | ||
from typing import Optional | ||
|
||
from sqlmodel import Field, Session, SQLModel, create_engine | ||
from sqlalchemy.exc import IntegrityError | ||
|
||
|
||
def test_should_allow_duplicate_row_if_unique_constraint_is_not_passed(clear_sqlmodel): | ||
class Hero(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
secret_name: str | ||
age: Optional[int] = None | ||
|
||
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
with Session(engine) as session: | ||
session.add(hero_1) | ||
session.commit() | ||
session.refresh(hero_1) | ||
|
||
with Session(engine) as session: | ||
session.add(hero_2) | ||
session.commit() | ||
session.refresh(hero_2) | ||
|
||
with Session(engine) as session: | ||
heroes = session.query(Hero).all() | ||
assert len(heroes) == 2 | ||
assert heroes[0].name == heroes[1].name | ||
|
||
|
||
def test_should_allow_duplicate_row_if_unique_constraint_is_false(clear_sqlmodel): | ||
class Hero(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
secret_name: str = Field(unique=False) | ||
age: Optional[int] = None | ||
|
||
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
with Session(engine) as session: | ||
session.add(hero_1) | ||
session.commit() | ||
session.refresh(hero_1) | ||
|
||
with Session(engine) as session: | ||
session.add(hero_2) | ||
session.commit() | ||
session.refresh(hero_2) | ||
|
||
with Session(engine) as session: | ||
heroes = session.query(Hero).all() | ||
assert len(heroes) == 2 | ||
assert heroes[0].name == heroes[1].name | ||
|
||
|
||
def test_should_raise_exception_when_try_to_duplicate_row_if_unique_constraint_is_true(clear_sqlmodel): | ||
class Hero(SQLModel, table=True): | ||
id: Optional[int] = Field(default=None, primary_key=True) | ||
name: str | ||
secret_name: str = Field(unique=True) | ||
age: Optional[int] = None | ||
|
||
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
hero_2 = Hero(name="Deadpond", secret_name="Dive Wilson") | ||
|
||
engine = create_engine("sqlite://") | ||
|
||
SQLModel.metadata.create_all(engine) | ||
|
||
with Session(engine) as session: | ||
session.add(hero_1) | ||
session.commit() | ||
session.refresh(hero_1) | ||
|
||
with pytest.raises(IntegrityError): | ||
with Session(engine) as session: | ||
session.add(hero_2) | ||
session.commit() | ||
session.refresh(hero_2) |