add multi column non unique columns
This commit is contained in:
@ -8,6 +8,7 @@ import sqlalchemy as sa
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import ormar
|
||||
import ormar.fields.constraints
|
||||
from ormar import ModelDefinitionError, property_field
|
||||
from ormar.exceptions import ModelError
|
||||
from tests.settings import DATABASE_URL
|
||||
@ -45,7 +46,9 @@ class DateFieldsModel(ormar.Model):
|
||||
abstract = True
|
||||
metadata = metadata
|
||||
database = db
|
||||
constraints = [ormar.UniqueColumns("creation_date", "modification_date")]
|
||||
constraints = [
|
||||
ormar.fields.constraints.UniqueColumns("creation_date", "modification_date")
|
||||
]
|
||||
|
||||
created_date: datetime.datetime = ormar.DateTime(
|
||||
default=datetime.datetime.now, name="creation_date"
|
||||
@ -58,7 +61,7 @@ class DateFieldsModel(ormar.Model):
|
||||
class Category(DateFieldsModel, AuditModel):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "categories"
|
||||
constraints = [ormar.UniqueColumns("name", "code")]
|
||||
constraints = [ormar.fields.constraints.UniqueColumns("name", "code")]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=50, unique=True, index=True)
|
||||
|
||||
68
tests/test_meta_constraints/test_index_constraints.py
Normal file
68
tests/test_meta_constraints/test_index_constraints.py
Normal file
@ -0,0 +1,68 @@
|
||||
import asyncpg # type: ignore
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar.fields.constraints
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Product(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "products"
|
||||
metadata = metadata
|
||||
database = database
|
||||
constraints = [
|
||||
ormar.fields.constraints.IndexColumns("company", "name", name="my_index"),
|
||||
ormar.fields.constraints.IndexColumns("location", "company_type"),
|
||||
]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
company: str = ormar.String(max_length=200)
|
||||
location: str = ormar.String(max_length=200)
|
||||
company_type: str = ormar.String(max_length=200)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
def test_table_structure():
|
||||
assert len(Product.Meta.table.indexes) > 0
|
||||
indexes = sorted(
|
||||
list(Product.Meta.table.indexes), key=lambda x: x.name, reverse=True
|
||||
)
|
||||
test_index = indexes[0]
|
||||
assert test_index.name == "my_index"
|
||||
assert [col.name for col in test_index.columns] == ["company", "name"]
|
||||
|
||||
test_index = indexes[1]
|
||||
assert test_index.name == "ix_products_location_company_type"
|
||||
assert [col.name for col in test_index.columns] == ["location", "company_type"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_index_is_not_unique():
|
||||
async with database:
|
||||
async with database.transaction(force_rollback=True):
|
||||
await Product.objects.create(
|
||||
name="Cookies", company="Nestle", location="A", company_type="B"
|
||||
)
|
||||
await Product.objects.create(
|
||||
name="Mars", company="Mars", location="B", company_type="Z"
|
||||
)
|
||||
await Product.objects.create(
|
||||
name="Mars", company="Nestle", location="C", company_type="X"
|
||||
)
|
||||
await Product.objects.create(
|
||||
name="Mars", company="Mars", location="D", company_type="Y"
|
||||
)
|
||||
@ -1,4 +1,3 @@
|
||||
import asyncio
|
||||
import sqlite3
|
||||
|
||||
import asyncpg # type: ignore
|
||||
@ -7,7 +6,7 @@ import pymysql
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
import ormar.fields.constraints
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
@ -19,22 +18,15 @@ class Product(ormar.Model):
|
||||
tablename = "products"
|
||||
metadata = metadata
|
||||
database = database
|
||||
constraints = [ormar.UniqueColumns("name", "company")]
|
||||
constraints = [ormar.fields.constraints.UniqueColumns("name", "company")]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
company: str = ormar.String(max_length=200)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def event_loop():
|
||||
loop = asyncio.get_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
async def create_test_database():
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
|
||||
Reference in New Issue
Block a user