File: test_join_table_inheritance.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (144 lines) | stat: -rw-r--r-- 3,806 bytes parent folder | download | duplicates (2)
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
from decimal import Decimal

import pytest
import sqlalchemy as sa

from sqlalchemy_utils.aggregates import aggregated


@pytest.fixture
def Product(Base):
    class Product(Base):
        __tablename__ = 'product'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.Unicode(255))
        price = sa.Column(sa.Numeric)
        type = sa.Column(sa.String(255))

        __mapper_args__ = {
            'polymorphic_on': type,
            'polymorphic_identity': 'simple'
        }

        catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id'))
    return Product


@pytest.fixture(params=['simple', 'child'])
def AnyProduct(Product, request):
    if request.param == 'simple':
        return Product

    class ChildProduct(Product):
        __tablename__ = 'child_product'
        id = sa.Column(
            sa.Integer, sa.ForeignKey(Product.id), primary_key=True
        )

        __mapper_args__ = {
            'polymorphic_identity': 'child',
        }
    return ChildProduct


@pytest.fixture
def Catalog(Base, Product):
    class Catalog(Base):
        __tablename__ = 'catalog'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.Unicode(255))
        type = sa.Column(sa.Unicode(255))

        __mapper_args__ = {
            'polymorphic_on': type
        }

        @aggregated('products', sa.Column(sa.Numeric, default=0))
        def net_worth(self):
            return sa.func.sum(Product.price)

        products = sa.orm.relationship('Product', backref='catalog')
    return Catalog


@pytest.fixture
def CostumeCatalog(Catalog):
    class CostumeCatalog(Catalog):
        __tablename__ = 'costume_catalog'
        id = sa.Column(
            sa.Integer, sa.ForeignKey(Catalog.id), primary_key=True
        )

        __mapper_args__ = {
            'polymorphic_identity': 'costumes',
        }
    return CostumeCatalog


@pytest.fixture
def CarCatalog(Catalog):
    class CarCatalog(Catalog):
        __tablename__ = 'car_catalog'
        id = sa.Column(
            sa.Integer, sa.ForeignKey(Catalog.id), primary_key=True
        )

        __mapper_args__ = {
            'polymorphic_identity': 'cars',
        }
    return CarCatalog


@pytest.fixture
def init_models(AnyProduct, Catalog, CostumeCatalog, CarCatalog):
    pass


@pytest.mark.usefixtures('postgresql_dsn')
class TestLazyEvaluatedSelectExpressionsForAggregates:

    def test_columns_inherited_from_parent(
        self,
        Catalog,
        CarCatalog,
        CostumeCatalog
    ):
        assert CarCatalog.net_worth
        assert CostumeCatalog.net_worth
        assert Catalog.net_worth
        assert not hasattr(CarCatalog.__table__.c, 'net_worth')
        assert not hasattr(CostumeCatalog.__table__.c, 'net_worth')

    def test_assigns_aggregates_on_insert(self, session, AnyProduct, Catalog):
        catalog = Catalog(
            name='Some catalog'
        )
        session.add(catalog)
        session.commit()
        product = AnyProduct(
            name='Some product',
            price=Decimal('1000'),
            catalog=catalog
        )
        session.add(product)
        session.commit()
        session.refresh(catalog)
        assert catalog.net_worth == Decimal('1000')

    def test_assigns_aggregates_on_update(self, session, Catalog, AnyProduct):
        catalog = Catalog(
            name='Some catalog'
        )
        session.add(catalog)
        session.commit()
        product = AnyProduct(
            name='Some product',
            price=Decimal('1000'),
            catalog=catalog
        )
        session.add(product)
        session.commit()
        product.price = Decimal('500')
        session.commit()
        session.refresh(catalog)
        assert catalog.net_worth == Decimal('500')