-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from tarsil/fix/uniques
Add unique index validation
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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,41 @@ | ||
from typing import AsyncGenerator | ||
|
||
import pydantic | ||
import pytest | ||
from pymongo.errors import DuplicateKeyError | ||
from tests.conftest import client | ||
|
||
import mongoz | ||
from mongoz import Document | ||
|
||
pytestmark = pytest.mark.anyio | ||
pydantic_version = pydantic.__version__[:3] | ||
|
||
|
||
class Movie(Document): | ||
name: str = mongoz.String(unique=True) | ||
is_published: bool = mongoz.Boolean(default=False) | ||
|
||
class Meta: | ||
registry = client | ||
database = "test_db" | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
async def prepare_database() -> AsyncGenerator: | ||
await Movie.drop_indexes(force=True) | ||
await Movie.objects.delete() | ||
await Movie.create_indexes() | ||
yield | ||
await Movie.drop_indexes(force=True) | ||
await Movie.objects.delete() | ||
await Movie.create_indexes() | ||
|
||
|
||
async def test_unique(): | ||
movie = await Movie.objects.create(name="Barbie") | ||
|
||
assert movie.is_published is False | ||
|
||
with pytest.raises(DuplicateKeyError): | ||
await Movie.objects.create(name="Barbie") |