~ubuntu-branches/debian/sid/sqlalchemy/sid

« back to all changes in this revision

Viewing changes to test/orm/test_subquery_relations.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2014-06-27 20:17:13 UTC
  • mfrom: (1.4.28)
  • Revision ID: package-import@ubuntu.com-20140627201713-g6p1kq8q1qenztrv
Tags: 0.9.6-1
* New upstream release
* Remove Python 3.X build tag files, thanks to Matthias Urlichs for the
  patch (closes: #747852)
* python-fdb isn't in the Debian archive yet so default dialect for firebird://
  URLs is changed to obsolete kinterbasdb, thanks to Russell Stuart for the
  patch (closes: #752145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
976
976
        self.assert_sql_count(testing.db, go, 2)
977
977
 
978
978
 
979
 
from .inheritance._poly_fixtures import _Polymorphic, Person, Engineer, Paperwork
 
979
from .inheritance._poly_fixtures import _Polymorphic, Person, Engineer, \
 
980
                Paperwork, Machine, MachineType, Company
980
981
 
981
982
class BaseRelationFromJoinedSubclassTest(_Polymorphic):
982
983
    @classmethod
1132
1133
            )
1133
1134
        )
1134
1135
 
1135
 
 
 
1136
class SubRelationFromJoinedSubclassMultiLevelTest(_Polymorphic):
 
1137
    @classmethod
 
1138
    def define_tables(cls, metadata):
 
1139
        Table('companies', metadata,
 
1140
            Column('company_id', Integer,
 
1141
                primary_key=True,
 
1142
                test_needs_autoincrement=True),
 
1143
            Column('name', String(50)))
 
1144
 
 
1145
        Table('people', metadata,
 
1146
            Column('person_id', Integer,
 
1147
                primary_key=True,
 
1148
                test_needs_autoincrement=True),
 
1149
            Column('company_id', ForeignKey('companies.company_id')),
 
1150
            Column('name', String(50)),
 
1151
            Column('type', String(30)))
 
1152
 
 
1153
        Table('engineers', metadata,
 
1154
            Column('engineer_id', ForeignKey('people.person_id'),
 
1155
                            primary_key=True),
 
1156
            Column('primary_language', String(50)))
 
1157
 
 
1158
        Table('machines', metadata,
 
1159
            Column('machine_id',
 
1160
                 Integer, primary_key=True,
 
1161
                 test_needs_autoincrement=True),
 
1162
            Column('name', String(50)),
 
1163
            Column('engineer_id', ForeignKey('engineers.engineer_id')),
 
1164
            Column('machine_type_id',
 
1165
                            ForeignKey('machine_type.machine_type_id')))
 
1166
 
 
1167
        Table('machine_type', metadata,
 
1168
            Column('machine_type_id',
 
1169
                    Integer, primary_key=True,
 
1170
                        test_needs_autoincrement=True),
 
1171
            Column('name', String(50)))
 
1172
 
 
1173
    @classmethod
 
1174
    def setup_mappers(cls):
 
1175
        companies = cls.tables.companies
 
1176
        people = cls.tables.people
 
1177
        engineers = cls.tables.engineers
 
1178
        machines = cls.tables.machines
 
1179
        machine_type = cls.tables.machine_type
 
1180
 
 
1181
        mapper(Company, companies, properties={
 
1182
                'employees': relationship(Person, order_by=people.c.person_id)
 
1183
                })
 
1184
        mapper(Person, people,
 
1185
            polymorphic_on=people.c.type,
 
1186
            polymorphic_identity='person',
 
1187
            with_polymorphic='*')
 
1188
 
 
1189
        mapper(Engineer, engineers,
 
1190
            inherits=Person,
 
1191
            polymorphic_identity='engineer', properties={
 
1192
                'machines': relationship(Machine,
 
1193
                                        order_by=machines.c.machine_id)
 
1194
            })
 
1195
 
 
1196
        mapper(Machine, machines, properties={
 
1197
                'type': relationship(MachineType)
 
1198
                })
 
1199
        mapper(MachineType, machine_type)
 
1200
 
 
1201
 
 
1202
    @classmethod
 
1203
    def insert_data(cls):
 
1204
        c1 = cls._fixture()
 
1205
        sess = create_session()
 
1206
        sess.add(c1)
 
1207
        sess.flush()
 
1208
 
 
1209
    @classmethod
 
1210
    def _fixture(cls):
 
1211
        mt1 = MachineType(name='mt1')
 
1212
        mt2 = MachineType(name='mt2')
 
1213
        return Company(
 
1214
                employees=[
 
1215
                    Engineer(
 
1216
                        name='e1',
 
1217
                        machines=[
 
1218
                            Machine(name='m1', type=mt1),
 
1219
                            Machine(name='m2', type=mt2)
 
1220
                        ]
 
1221
                    ),
 
1222
                    Engineer(
 
1223
                        name='e2',
 
1224
                        machines=[
 
1225
                            Machine(name='m3', type=mt1),
 
1226
                            Machine(name='m4', type=mt1)
 
1227
                        ]
 
1228
                    )
 
1229
                ])
 
1230
 
 
1231
    def test_chained_subq_subclass(self):
 
1232
        s = Session()
 
1233
        q = s.query(Company).options(
 
1234
                        subqueryload(Company.employees.of_type(Engineer)).
 
1235
                            subqueryload(Engineer.machines).
 
1236
                            subqueryload(Machine.type)
 
1237
                    )
 
1238
 
 
1239
        def go():
 
1240
            eq_(
 
1241
                q.all(),
 
1242
                [self._fixture()]
 
1243
            )
 
1244
        self.assert_sql_count(testing.db, go, 4)
1136
1245
 
1137
1246
 
1138
1247
class SelfReferentialTest(fixtures.MappedTest):