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

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