Files
deb-python-sqlalchemy-utils/tests/observes/test_o2o_o2o.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.6 KiB
Python

import pytest
import sqlalchemy as sa
from sqlalchemy_utils.observer import observes
@pytest.fixture
def Device(Base):
class Device(Base):
__tablename__ = 'device'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
return Device
@pytest.fixture
def Order(Base):
class Order(Base):
__tablename__ = 'order'
id = sa.Column(sa.Integer, primary_key=True)
device_id = sa.Column(
'device', sa.ForeignKey('device.id'), nullable=False
)
device = sa.orm.relationship('Device', backref='orders')
return Order
@pytest.fixture
def SalesInvoice(Base):
class SalesInvoice(Base):
__tablename__ = 'sales_invoice'
id = sa.Column(sa.Integer, primary_key=True)
order_id = sa.Column(
'order',
sa.ForeignKey('order.id'),
nullable=False
)
order = sa.orm.relationship(
'Order',
backref=sa.orm.backref(
'invoice',
uselist=False
)
)
device_name = sa.Column(sa.String)
@observes('order.device')
def process_device(self, device):
self.device_name = device.name
return SalesInvoice
@pytest.fixture
def init_models(Device, Order, SalesInvoice):
pass
@pytest.mark.usefixtures('postgresql_dsn')
class TestObservesForOneToManyToOneToMany(object):
def test_observable_root_obj_is_none(self, session, Device, Order):
order = Order(device=Device(name='Something'))
session.add(order)
session.flush()