1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
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:
def test_observable_root_obj_is_none(self, session, Device, Order):
order = Order(device=Device(name='Something'))
session.add(order)
session.flush()
|