-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
293 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
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]") |
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,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") |
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,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") |