CheckColumns Constraint (#730)
* feat: add check columns class * feat: write document of check columns part * test: write a test for check columns constraints * fix: debuging test exception raise mysql * fix: set pragma no cover to ignore cov * fix: ignore pytest raise in python 3.x not 10 * feat: set constraint name for check columns * refactor: support index and check overwrites * fix: debuging check constraint arguments * fix: debug coverage all code tests * fix: pass the map of type constraint to counter * refactor: edit check name replace sapce underline * refactor: write new function copy constraints * test: write test for invalid constraint type * fix: debug text cluase replaced names * fix: set pragma no cover for result returned * refactor: no coverage for main if statement * perf: change get constraint copy func code * fix: fix bug in mypy typing check
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
# type: ignore
|
||||
import datetime
|
||||
from typing import List, Optional
|
||||
from collections import Counter
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
@ -11,6 +12,7 @@ import ormar
|
||||
import ormar.fields.constraints
|
||||
from ormar import ModelDefinitionError, property_field
|
||||
from ormar.exceptions import ModelError
|
||||
from ormar.models.metaclass import get_constraint_copy
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
metadata = sa.MetaData()
|
||||
@ -47,7 +49,13 @@ class DateFieldsModel(ormar.Model):
|
||||
metadata = metadata
|
||||
database = db
|
||||
constraints = [
|
||||
ormar.fields.constraints.UniqueColumns("creation_date", "modification_date")
|
||||
ormar.fields.constraints.UniqueColumns(
|
||||
"creation_date",
|
||||
"modification_date",
|
||||
),
|
||||
ormar.fields.constraints.CheckColumns(
|
||||
"creation_date <= modification_date",
|
||||
),
|
||||
]
|
||||
|
||||
created_date: datetime.datetime = ormar.DateTime(
|
||||
@ -234,9 +242,13 @@ def test_model_subclassing_non_abstract_raises_error():
|
||||
def test_params_are_inherited():
|
||||
assert Category.Meta.metadata == metadata
|
||||
assert Category.Meta.database == db
|
||||
assert len(Category.Meta.constraints) == 2
|
||||
assert len(Category.Meta.property_fields) == 2
|
||||
|
||||
constraints = Counter(map(lambda c: type(c), Category.Meta.constraints))
|
||||
assert constraints[ormar.fields.constraints.UniqueColumns] == 2
|
||||
assert constraints[ormar.fields.constraints.IndexColumns] == 0
|
||||
assert constraints[ormar.fields.constraints.CheckColumns] == 1
|
||||
|
||||
|
||||
def round_date_to_seconds(
|
||||
date: datetime.datetime,
|
||||
@ -519,3 +531,8 @@ def test_custom_config():
|
||||
sam = ImmutablePerson(name="Sam")
|
||||
with pytest.raises(TypeError):
|
||||
sam.name = "Not Sam"
|
||||
|
||||
|
||||
def test_get_constraint_copy():
|
||||
with pytest.raises(ValueError):
|
||||
get_constraint_copy("INVALID CONSTRAINT")
|
||||
|
||||
57
tests/test_meta_constraints/test_check_constraints.py
Normal file
57
tests/test_meta_constraints/test_check_constraints.py
Normal file
@ -0,0 +1,57 @@
|
||||
import sqlite3
|
||||
|
||||
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.CheckColumns("inventory > buffer"),
|
||||
]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
company: str = ormar.String(max_length=200)
|
||||
inventory: int = ormar.Integer()
|
||||
buffer: int = ormar.Integer()
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_check_columns_exclude_mysql():
|
||||
if Product.Meta.database._backend._dialect.name != "mysql":
|
||||
async with database: # pragma: no cover
|
||||
async with database.transaction(force_rollback=True):
|
||||
await Product.objects.create(
|
||||
name="Mars", company="Nestle", inventory=100, buffer=10
|
||||
)
|
||||
|
||||
with pytest.raises(
|
||||
(
|
||||
sqlite3.IntegrityError,
|
||||
asyncpg.exceptions.CheckViolationError,
|
||||
)
|
||||
):
|
||||
await Product.objects.create(
|
||||
name="Cookies", company="Nestle", inventory=1, buffer=10
|
||||
)
|
||||
Reference in New Issue
Block a user