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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
import pytest
import sqlalchemy as sa
from sqlalchemy_utils.observer import observes
@pytest.fixture
def Catalog(Base):
catalog_category = sa.Table(
'catalog_category',
Base.metadata,
sa.Column('catalog_id', sa.Integer, sa.ForeignKey('catalog.id')),
sa.Column('category_id', sa.Integer, sa.ForeignKey('category.id'))
)
class Catalog(Base):
__tablename__ = 'catalog'
id = sa.Column(sa.Integer, primary_key=True)
product_count = sa.Column(sa.Integer, default=0)
@observes('categories.sub_categories.products')
def product_observer(self, products):
self.product_count = len(products)
categories = sa.orm.relationship(
'Category',
backref='catalogs',
secondary=catalog_category
)
return Catalog
@pytest.fixture
def Category(Base):
category_subcategory = sa.Table(
'category_subcategory',
Base.metadata,
sa.Column(
'category_id',
sa.Integer,
sa.ForeignKey('category.id')
),
sa.Column(
'subcategory_id',
sa.Integer,
sa.ForeignKey('sub_category.id')
)
)
class Category(Base):
__tablename__ = 'category'
id = sa.Column(sa.Integer, primary_key=True)
sub_categories = sa.orm.relationship(
'SubCategory',
backref='categories',
secondary=category_subcategory
)
return Category
@pytest.fixture
def SubCategory(Base):
subcategory_product = sa.Table(
'subcategory_product',
Base.metadata,
sa.Column(
'subcategory_id',
sa.Integer,
sa.ForeignKey('sub_category.id')
),
sa.Column(
'product_id',
sa.Integer,
sa.ForeignKey('product.id')
)
)
class SubCategory(Base):
__tablename__ = 'sub_category'
id = sa.Column(sa.Integer, primary_key=True)
products = sa.orm.relationship(
'Product',
backref='sub_categories',
secondary=subcategory_product
)
return SubCategory
@pytest.fixture
def Product(Base):
class Product(Base):
__tablename__ = 'product'
id = sa.Column(sa.Integer, primary_key=True)
price = sa.Column(sa.Numeric)
return Product
@pytest.fixture
def init_models(Catalog, Category, SubCategory, Product):
pass
@pytest.fixture
def catalog(session, Catalog, Category, SubCategory, Product):
sub_category = SubCategory(products=[Product()])
category = Category(sub_categories=[sub_category])
catalog = Catalog(categories=[category])
session.add(catalog)
session.flush()
return catalog
@pytest.mark.usefixtures('postgresql_dsn')
class TestObservesForManyToManyToManyToMany:
def test_simple_insert(self, catalog):
assert catalog.product_count == 1
def test_add_leaf_object(self, catalog, session, Product):
product = Product()
catalog.categories[0].sub_categories[0].products.append(product)
session.flush()
assert catalog.product_count == 2
def test_remove_leaf_object(self, catalog, session, Product):
product = Product()
catalog.categories[0].sub_categories[0].products.append(product)
session.flush()
session.delete(product)
session.flush()
assert catalog.product_count == 1
def test_delete_intermediate_object(self, catalog, session):
session.delete(catalog.categories[0].sub_categories[0])
session.commit()
assert catalog.product_count == 0
def test_gathered_objects_are_distinct(
self,
session,
Catalog,
Category,
SubCategory,
Product
):
catalog = Catalog()
category = Category(catalogs=[catalog])
product = Product()
category.sub_categories.append(
SubCategory(products=[product])
)
session.add(
SubCategory(categories=[category], products=[product])
)
session.commit()
assert catalog.product_count == 1
|