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:
Sepehr Bazyar
2022-07-14 12:35:30 +04:30
committed by GitHub
parent 3ad563e5dd
commit b6e057c303
9 changed files with 160 additions and 10 deletions

View File

@ -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")