Files
deb-python-sqlalchemy-utils/tests/generic_relationship/test_hybrid_properties.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

80 lines
1.9 KiB
Python

import pytest
import six
import sqlalchemy as sa
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy_utils import generic_relationship
@pytest.fixture
def User(Base):
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
return User
@pytest.fixture
def UserHistory(Base):
class UserHistory(Base):
__tablename__ = 'user_history'
id = sa.Column(sa.Integer, primary_key=True)
transaction_id = sa.Column(sa.Integer, primary_key=True)
return UserHistory
@pytest.fixture
def Event(Base):
class Event(Base):
__tablename__ = 'event'
id = sa.Column(sa.Integer, primary_key=True)
transaction_id = sa.Column(sa.Integer)
object_type = sa.Column(sa.Unicode(255))
object_id = sa.Column(sa.Integer, nullable=False)
object = generic_relationship(
object_type, object_id
)
@hybrid_property
def object_version_type(self):
return self.object_type + 'History'
@object_version_type.expression
def object_version_type(cls):
return sa.func.concat(cls.object_type, 'History')
object_version = generic_relationship(
object_version_type, (object_id, transaction_id)
)
return Event
@pytest.fixture
def init_models(User, UserHistory, Event):
pass
class TestGenericRelationship(object):
def test_set_manual_and_get(self, session, User, UserHistory, Event):
user = User(id=1)
history = UserHistory(id=1, transaction_id=1)
session.add(user)
session.add(history)
session.commit()
event = Event(transaction_id=1)
event.object_id = user.id
event.object_type = six.text_type(type(user).__name__)
assert event.object is None
session.add(event)
session.commit()
assert event.object == user
assert event.object_version == history