Files
deb-python-sqlalchemy-utils/tests/functions/test_non_indexed_foreign_keys.py
Jacob Magnusson 815f07d6c1 Use pytest fixtures to reduce complexity and repetition
Also:

Allow override of database name and user in tests (important for me as I would have to mess with my PSQL and MySQL database users otherwise)
Use dict.items instead of six.iteritems as it sporadically caused RuntimeError: dictionary changed size during iteration in Python 2.6 tests.
Fix typo DNS to DSN
Adds Python 3.5 to tox.ini
Added an .editorconfig
Import babel.dates in sqlalchemy_utils.i18n as an exception would be raised when using the latest versions of babel.
2016-01-19 10:52:30 +01:00

67 lines
1.9 KiB
Python

from itertools import chain
import pytest
import sqlalchemy as sa
from sqlalchemy_utils.functions import non_indexed_foreign_keys
class TestFindNonIndexedForeignKeys(object):
@pytest.fixture
def User(self, Base):
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
name = sa.Column(sa.Unicode(255))
return User
@pytest.fixture
def Category(self, Base):
class Category(Base):
__tablename__ = 'category'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
return Category
@pytest.fixture
def Article(self, Base, User, Category):
class Article(Base):
__tablename__ = 'article'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
author_id = sa.Column(
sa.Integer, sa.ForeignKey(User.id), index=True
)
category_id = sa.Column(sa.Integer, sa.ForeignKey(Category.id))
category = sa.orm.relationship(
Category,
primaryjoin=category_id == Category.id,
backref=sa.orm.backref(
'articles',
)
)
return Article
@pytest.fixture
def init_models(self, User, Category, Article):
pass
def test_finds_all_non_indexed_fks(self, session, Base, engine):
fks = non_indexed_foreign_keys(Base.metadata, engine)
assert (
'article' in
fks
)
column_names = list(chain(
*(
names for names in (
fk.columns.keys()
for fk in fks['article']
)
)
))
assert 'category_id' in column_names
assert 'author_id' not in column_names