Skip to content

Commit

Permalink
Add tests to unique constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Aug 2, 2023
1 parent 752817d commit a276617
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/uniques/test_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import datetime
from enum import Enum

import pytest
from asyncpg.exceptions import UniqueViolationError
from tests.settings import DATABASE_URL

import edgy
from edgy.testclient import DatabaseTestClient as Database

pytestmark = pytest.mark.anyio

database = Database(DATABASE_URL)
models = edgy.Registry(database=database)


def time():
return datetime.datetime.now().time()


class StatusEnum(Enum):
DRAFT = "Draft"
RELEASED = "Released"


class BaseModel(edgy.Model):
class Meta:
registry = models


class User(BaseModel):
name = edgy.CharField(max_length=255, unique=True)
email = edgy.CharField(max_length=60)


@pytest.fixture(autouse=True, scope="module")
async def create_test_database():
await models.create_all()
yield
await models.drop_all()


@pytest.fixture(autouse=True)
async def rollback_transactions():
with database.force_rollback():
async with database:
yield


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique():
await User.query.create(name="Tiago", email="[email protected]")

with pytest.raises(UniqueViolationError):
await User.query.create(name="Tiago", email="[email protected]")
121 changes: 121 additions & 0 deletions tests/uniques/test_unique_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import datetime
from enum import Enum

import pytest
from asyncpg.exceptions import UniqueViolationError
from tests.settings import DATABASE_URL

import edgy
from edgy.core.db.datastructures import UniqueConstraint
from edgy.testclient import DatabaseTestClient as Database

pytestmark = pytest.mark.anyio

database = Database(DATABASE_URL)
models = edgy.Registry(database=database)


def time():
return datetime.datetime.now().time()


class StatusEnum(Enum):
DRAFT = "Draft"
RELEASED = "Released"


class BaseModel(edgy.Model):
class Meta:
registry = models


class User(BaseModel):
name = edgy.CharField(max_length=255)
email = edgy.CharField(max_length=60)

class Meta:
unique_together = [UniqueConstraint(fields=["name", "email"])]


class HubUser(BaseModel):
name = edgy.CharField(max_length=255)
email = edgy.CharField(max_length=60, null=True)
age = edgy.IntegerField(minimum=18, null=True)

class Meta:
unique_together = [
UniqueConstraint(fields=["name", "email"]),
("email", "age"),
]


class Product(BaseModel):
name = edgy.CharField(max_length=255)
sku = edgy.CharField(max_length=255)

class Meta:
unique_together = [UniqueConstraint(fields=["name"]), UniqueConstraint(fields=["sku"])]


class NewProduct(BaseModel):
name = edgy.CharField(max_length=255)
sku = edgy.CharField(max_length=255)

class Meta:
unique_together = [UniqueConstraint(fields=["name"]), "sku"]


@pytest.fixture(autouse=True, scope="module")
async def create_test_database():
await models.create_all()
yield
await models.drop_all()


@pytest.fixture(autouse=True)
async def rollback_transactions():
with database.force_rollback():
async with database:
yield


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together():
await User.query.create(name="Test", email="[email protected]")
await User.query.create(name="Test", email="[email protected]")

with pytest.raises(UniqueViolationError):
await User.query.create(name="Test", email="[email protected]")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple():
await HubUser.query.create(name="Test", email="[email protected]")
await HubUser.query.create(name="Test", email="[email protected]")

with pytest.raises(UniqueViolationError):
await HubUser.query.create(name="Test", email="[email protected]")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_name_age():
await HubUser.query.create(name="NewTest", email="[email protected]", age=18)

with pytest.raises(UniqueViolationError):
await HubUser.query.create(name="Test", email="[email protected]", age=18)


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_single_string():
await Product.query.create(name="android", sku="12345")

with pytest.raises(UniqueViolationError):
await Product.query.create(name="android", sku="12345")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_single_string_two():
await Product.query.create(name="android", sku="12345")

with pytest.raises(UniqueViolationError):
await Product.query.create(name="iphone", sku="12345")
117 changes: 117 additions & 0 deletions tests/uniques/test_unique_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import datetime
from enum import Enum

import pytest
from asyncpg.exceptions import UniqueViolationError
from tests.settings import DATABASE_URL

import edgy
from edgy.testclient import DatabaseTestClient as Database

pytestmark = pytest.mark.anyio

database = Database(DATABASE_URL)
models = edgy.Registry(database=database)


def time():
return datetime.datetime.now().time()


class StatusEnum(Enum):
DRAFT = "Draft"
RELEASED = "Released"


class BaseModel(edgy.Model):
class Meta:
registry = models


class User(BaseModel):
name = edgy.CharField(max_length=255)
email = edgy.CharField(max_length=60)

class Meta:
unique_together = [("name", "email")]


class HubUser(BaseModel):
name = edgy.CharField(max_length=255)
email = edgy.CharField(max_length=60, null=True)
age = edgy.IntegerField(minimum=18, null=True)

class Meta:
unique_together = [("name", "email"), ("email", "age")]


class Product(BaseModel):
name = edgy.CharField(max_length=255)
sku = edgy.CharField(max_length=255)

class Meta:
unique_together = ["name", "sku"]


class NewProduct(BaseModel):
name = edgy.CharField(max_length=255)
sku = edgy.CharField(max_length=255)

class Meta:
unique_together = ["name", "sku"]


@pytest.fixture(autouse=True, scope="module")
async def create_test_database():
await models.create_all()
yield
await models.drop_all()


@pytest.fixture(autouse=True)
async def rollback_transactions():
with database.force_rollback():
async with database:
yield


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together():
await User.query.create(name="Test", email="[email protected]")
await User.query.create(name="Test", email="[email protected]")

with pytest.raises(UniqueViolationError):
await User.query.create(name="Test", email="[email protected]")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple():
await HubUser.query.create(name="Test", email="[email protected]")
await HubUser.query.create(name="Test", email="[email protected]")

with pytest.raises(UniqueViolationError):
await HubUser.query.create(name="Test", email="[email protected]")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_name_age():
await HubUser.query.create(name="NewTest", email="[email protected]", age=18)

with pytest.raises(UniqueViolationError):
await HubUser.query.create(name="Test", email="[email protected]", age=18)


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_single_string():
await Product.query.create(name="android", sku="12345")

with pytest.raises(UniqueViolationError):
await Product.query.create(name="android", sku="12345")


@pytest.mark.skipif(database.url.dialect == "mysql", reason="Not supported on MySQL")
async def test_unique_together_multiple_single_string_two():
await Product.query.create(name="android", sku="12345")

with pytest.raises(UniqueViolationError):
await Product.query.create(name="iphone", sku="12345")

0 comments on commit a276617

Please sign in to comment.