catch different exc in tests for different backends

This commit is contained in:
collerek
2020-10-01 13:16:04 +02:00
parent d0b6e75470
commit d2d0d042d6
3 changed files with 57 additions and 47 deletions

View File

@ -248,7 +248,10 @@ def populate_meta_sqlalchemy_table_if_required(
) -> Type["Model"]: ) -> Type["Model"]:
if not hasattr(new_model.Meta, "table"): if not hasattr(new_model.Meta, "table"):
new_model.Meta.table = sqlalchemy.Table( new_model.Meta.table = sqlalchemy.Table(
new_model.Meta.tablename, new_model.Meta.metadata, *new_model.Meta.columns, *new_model.Meta.constraints new_model.Meta.tablename,
new_model.Meta.metadata,
*new_model.Meta.columns,
*new_model.Meta.constraints,
) )
return new_model return new_model
@ -306,7 +309,7 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
) )
if hasattr(new_model, "Meta"): if hasattr(new_model, "Meta"):
if not hasattr(new_model.Meta, 'constraints'): if not hasattr(new_model.Meta, "constraints"):
new_model.Meta.constraints = [] new_model.Meta.constraints = []
new_model = populate_meta_orm_model_fields(attrs, new_model) new_model = populate_meta_orm_model_fields(attrs, new_model)
new_model = populate_meta_tablename_columns_and_pk(name, new_model) new_model = populate_meta_tablename_columns_and_pk(name, new_model)

View File

@ -1,10 +1,11 @@
import asyncio import asyncio
import sqlite3 import sqlite3
import asyncpg
import databases import databases
import pymysql
import pytest import pytest
import sqlalchemy import sqlalchemy
from sqlalchemy.exc import IntegrityError
import ormar import ormar
from tests.settings import DATABASE_URL from tests.settings import DATABASE_URL
@ -18,7 +19,7 @@ class Product(ormar.Model):
tablename = "products" tablename = "products"
metadata = metadata metadata = metadata
database = database database = database
constraints = [ormar.UniqueColumns('name', 'company')] constraints = [ormar.UniqueColumns("name", "company")]
id: ormar.Integer(primary_key=True) id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100) name: ormar.String(max_length=100)
@ -45,9 +46,15 @@ async def create_test_database():
async def test_unique_columns(): async def test_unique_columns():
async with database: async with database:
async with database.transaction(force_rollback=True): async with database.transaction(force_rollback=True):
await Product.objects.create(name='Cookies', company='Nestle') await Product.objects.create(name="Cookies", company="Nestle")
await Product.objects.create(name='Mars', company='Mars') await Product.objects.create(name="Mars", company="Mars")
await Product.objects.create(name='Mars', company='Nestle') await Product.objects.create(name="Mars", company="Nestle")
with pytest.raises((IntegrityError, sqlite3.IntegrityError)): with pytest.raises(
await Product.objects.create(name='Mars', company='Mars') (
sqlite3.IntegrityError,
pymysql.IntegrityError,
asyncpg.exceptions.UniqueViolationError,
)
):
await Product.objects.create(name="Mars", company="Mars")