Source

sqlalchemy / test / orm / test_manytomany.py

  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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from test.lib.testing import assert_raises, \
    assert_raises_message, eq_
import sqlalchemy as sa
from test.lib import testing
from sqlalchemy import Integer, String, ForeignKey
from test.lib.schema import Table
from test.lib.schema import Column
from sqlalchemy.orm import mapper, relationship, create_session, \
    exc as orm_exc, sessionmaker
from test.lib import fixtures


class M2MTest(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table('place', metadata,
            Column('place_id', Integer, sa.Sequence('pid_seq', optional=True),
                   primary_key=True),
            Column('name', String(30), nullable=False),
            test_needs_acid=True,
            )

        Table('transition', metadata,
            Column('transition_id', Integer,
                   sa.Sequence('tid_seq', optional=True), primary_key=True),
            Column('name', String(30), nullable=False),
            test_needs_acid=True,
            )

        Table('place_thingy', metadata,
            Column('thingy_id', Integer, sa.Sequence('thid_seq', optional=True),
                   primary_key=True),
            Column('place_id', Integer, ForeignKey('place.place_id'),
                   nullable=False),
            Column('name', String(30), nullable=False),
            test_needs_acid=True,
            )

        # association table #1
        Table('place_input', metadata,
            Column('place_id', Integer, ForeignKey('place.place_id')),
            Column('transition_id', Integer,
                   ForeignKey('transition.transition_id')),
                   test_needs_acid=True,
                   )

        # association table #2
        Table('place_output', metadata,
            Column('place_id', Integer, ForeignKey('place.place_id')),
            Column('transition_id', Integer,
                   ForeignKey('transition.transition_id')),
                   test_needs_acid=True,
                   )

        Table('place_place', metadata,
              Column('pl1_id', Integer, ForeignKey('place.place_id')),
              Column('pl2_id', Integer, ForeignKey('place.place_id')),
              test_needs_acid=True,
              )

    @classmethod
    def setup_classes(cls):
        class Place(cls.Basic):
            def __init__(self, name=None):
                self.name = name
            def __str__(self):
                return "(Place '%s')" % self.name
            __repr__ = __str__

        class PlaceThingy(cls.Basic):
            def __init__(self, name=None):
                self.name = name

        class Transition(cls.Basic):
            def __init__(self, name=None):
                self.name = name
                self.inputs = []
                self.outputs = []
            def __repr__(self):
                return ' '.join((object.__repr__(self),
                                 repr(self.inputs),
                                 repr(self.outputs)))

    def test_error(self):
        place, Transition, place_input, Place, transition = (self.tables.place,
                                self.classes.Transition,
                                self.tables.place_input,
                                self.classes.Place,
                                self.tables.transition)

        mapper(Place, place, properties={
            'transitions':relationship(Transition, secondary=place_input, backref='places')
        })
        mapper(Transition, transition, properties={
            'places':relationship(Place, secondary=place_input, backref='transitions')
        })
        assert_raises_message(sa.exc.ArgumentError, "Error creating backref",
                                 sa.orm.configure_mappers)

    def test_circular(self):
        """test a many-to-many relationship from a table to itself."""

        place, Place, place_place = (self.tables.place,
                                self.classes.Place,
                                self.tables.place_place)

        mapper(Place, place, properties={
            'places': relationship(
                        Place,
                        secondary=place_place,
                        primaryjoin=place.c.place_id==place_place.c.pl1_id,
                        secondaryjoin=place.c.place_id==place_place.c.pl2_id,
                        order_by=place_place.c.pl2_id
                )
        })

        sess = create_session()
        p1 = Place('place1')
        p2 = Place('place2')
        p3 = Place('place3')
        p4 = Place('place4')
        p5 = Place('place5')
        p6 = Place('place6')
        p7 = Place('place7')
        sess.add_all((p1, p2, p3, p4, p5, p6, p7))
        p1.places.append(p2)
        p1.places.append(p3)
        p5.places.append(p6)
        p6.places.append(p1)
        p7.places.append(p1)
        p1.places.append(p5)
        p4.places.append(p3)
        p3.places.append(p4)
        sess.flush()

        sess.expunge_all()
        l = sess.query(Place).order_by(place.c.place_id).all()
        (p1, p2, p3, p4, p5, p6, p7) = l
        assert p1.places == [p2,p3,p5]
        assert p5.places == [p6]
        assert p7.places == [p1]
        assert p6.places == [p1]
        assert p4.places == [p3]
        assert p3.places == [p4]
        assert p2.places == []

        for p in l:
            pp = p.places
            print "Place " + str(p) +" places " + repr(pp)

        [sess.delete(p) for p in p1,p2,p3,p4,p5,p6,p7]
        sess.flush()

    def test_circular_mutation(self):
        """Test that a mutation in a self-ref m2m of both sides succeeds."""


        place, Place, place_place = (self.tables.place,
                                self.classes.Place,
                                self.tables.place_place)

        mapper(Place, place, properties={
            'child_places': relationship(
                        Place,
                        secondary=place_place,
                        primaryjoin=place.c.place_id==place_place.c.pl1_id,
                        secondaryjoin=place.c.place_id==place_place.c.pl2_id,
                        order_by=place_place.c.pl2_id,
                        backref='parent_places'
                )
        })

        sess = create_session()
        p1 = Place('place1')
        p2 = Place('place2')
        p2.parent_places = [p1]
        sess.add_all([p1, p2])
        p1.parent_places.append(p2)
        sess.flush()

        sess.expire_all()
        assert p1 in p2.parent_places
        assert p2 in p1.parent_places


    def test_double(self):
        """test that a mapper can have two eager relationships to the same table, via
        two different association tables.  aliases are required."""

        place_input, transition, Transition, PlaceThingy, place, place_thingy, Place, place_output = (self.tables.place_input,
                                self.tables.transition,
                                self.classes.Transition,
                                self.classes.PlaceThingy,
                                self.tables.place,
                                self.tables.place_thingy,
                                self.classes.Place,
                                self.tables.place_output)


        Place.mapper = mapper(Place, place, properties = {
            'thingies':relationship(mapper(PlaceThingy, place_thingy), lazy='joined')
        })

        Transition.mapper = mapper(Transition, transition, properties = dict(
            inputs = relationship(Place.mapper, place_output, lazy='joined'),
            outputs = relationship(Place.mapper, place_input, lazy='joined'),
            )
        )

        tran = Transition('transition1')
        tran.inputs.append(Place('place1'))
        tran.outputs.append(Place('place2'))
        tran.outputs.append(Place('place3'))
        sess = create_session()
        sess.add(tran)
        sess.flush()

        sess.expunge_all()
        r = sess.query(Transition).all()
        self.assert_unordered_result(r, Transition,
            {'name': 'transition1',
            'inputs': (Place, [{'name':'place1'}]),
            'outputs': (Place, [{'name':'place2'}, {'name':'place3'}])
            })

    def test_bidirectional(self):
        place_input, transition, Transition, Place, place, place_output = (self.tables.place_input,
                                self.tables.transition,
                                self.classes.Transition,
                                self.classes.Place,
                                self.tables.place,
                                self.tables.place_output)

        Place.mapper = mapper(Place, place)
        Transition.mapper = mapper(Transition, transition, properties = dict(
            inputs = relationship(Place.mapper, place_output, lazy='select', backref='inputs'),
            outputs = relationship(Place.mapper, place_input, lazy='select', backref='outputs'),
            )
        )

        t1 = Transition('transition1')
        t2 = Transition('transition2')
        t3 = Transition('transition3')
        p1 = Place('place1')
        p2 = Place('place2')
        p3 = Place('place3')

        t1.inputs.append(p1)
        t1.inputs.append(p2)
        t1.outputs.append(p3)
        t2.inputs.append(p1)
        p2.inputs.append(t2)
        p3.inputs.append(t2)
        p1.outputs.append(t1)
        sess = create_session()
        sess.add_all((t1, t2, t3,p1, p2, p3))
        sess.flush()

        self.assert_result([t1], Transition, {'outputs': (Place, [{'name':'place3'}, {'name':'place1'}])})
        self.assert_result([p2], Place, {'inputs': (Transition, [{'name':'transition1'},{'name':'transition2'}])})

    @testing.requires.sane_multi_rowcount
    def test_stale_conditions(self):
        Place, Transition, place_input, place, transition = (self.classes.Place,
                                self.classes.Transition,
                                self.tables.place_input,
                                self.tables.place,
                                self.tables.transition)

        mapper(Place, place, properties={
            'transitions':relationship(Transition, secondary=place_input,
                                            passive_updates=False)
        })
        mapper(Transition, transition)

        p1 = Place('place1')
        t1 = Transition('t1')
        p1.transitions.append(t1)
        sess = sessionmaker()()
        sess.add_all([p1, t1])
        sess.commit()

        p1.place_id
        p1.transitions

        sess.execute("delete from place_input", mapper=Place)
        p1.place_id = 7

        assert_raises_message(
            orm_exc.StaleDataError,
            r"UPDATE statement on table 'place_input' expected to "
            r"update 1 row\(s\); Only 0 were matched.",
            sess.commit
        )
        sess.rollback()

        p1.place_id
        p1.transitions
        sess.execute("delete from place_input", mapper=Place)
        p1.transitions.remove(t1)
        assert_raises_message(
            orm_exc.StaleDataError,
            r"DELETE statement on table 'place_input' expected to "
            r"delete 1 row\(s\); Only 0 were matched.",
            sess.commit
        )

class M2MTest2(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table('student', metadata,
              Column('name', String(20), primary_key=True))

        Table('course', metadata,
              Column('name', String(20), primary_key=True))

        Table('enroll', metadata,
              Column('student_id', String(20), ForeignKey('student.name'),
                     primary_key=True),
            Column('course_id', String(20), ForeignKey('course.name'),
                   primary_key=True))

    @classmethod
    def setup_classes(cls):
        class Student(cls.Basic):
            def __init__(self, name=''):
                self.name = name
        class Course(cls.Basic):
            def __init__(self, name=''):
                self.name = name

    def test_circular(self):
        course, enroll, Student, student, Course = (self.tables.course,
                                self.tables.enroll,
                                self.classes.Student,
                                self.tables.student,
                                self.classes.Course)


        mapper(Student, student)
        mapper(Course, course, properties={
            'students': relationship(Student, enroll, backref='courses')})

        sess = create_session()
        s1 = Student('Student1')
        c1 = Course('Course1')
        c2 = Course('Course2')
        c3 = Course('Course3')
        s1.courses.append(c1)
        s1.courses.append(c2)
        c3.students.append(s1)
        self.assert_(len(s1.courses) == 3)
        self.assert_(len(c1.students) == 1)
        sess.add(s1)
        sess.flush()
        sess.expunge_all()
        s = sess.query(Student).filter_by(name='Student1').one()
        c = sess.query(Course).filter_by(name='Course3').one()
        self.assert_(len(s.courses) == 3)
        del s.courses[1]
        self.assert_(len(s.courses) == 2)

    def test_dupliates_raise(self):
        """test constraint error is raised for dupe entries in a list"""

        course, enroll, Student, student, Course = (self.tables.course,
                                self.tables.enroll,
                                self.classes.Student,
                                self.tables.student,
                                self.classes.Course)


        mapper(Student, student)
        mapper(Course, course, properties={
            'students': relationship(Student, enroll, backref='courses')})

        sess = create_session()
        s1 = Student("s1")
        c1 = Course('c1')
        s1.courses.append(c1)
        s1.courses.append(c1)
        sess.add(s1)
        assert_raises(sa.exc.DBAPIError, sess.flush)

    def test_delete(self):
        """A many-to-many table gets cleared out with deletion from the backref side"""

        course, enroll, Student, student, Course = (self.tables.course,
                                self.tables.enroll,
                                self.classes.Student,
                                self.tables.student,
                                self.classes.Course)


        mapper(Student, student)
        mapper(Course, course, properties = {
            'students': relationship(Student, enroll, lazy='select',
                                 backref='courses')})

        sess = create_session()
        s1 = Student('Student1')
        c1 = Course('Course1')
        c2 = Course('Course2')
        c3 = Course('Course3')
        s1.courses.append(c1)
        s1.courses.append(c2)
        c3.students.append(s1)
        sess.add(s1)
        sess.flush()
        sess.delete(s1)
        sess.flush()
        assert enroll.count().scalar() == 0

class M2MTest3(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table('c', metadata,
            Column('c1', Integer, primary_key = True),
            Column('c2', String(20)))

        Table('a', metadata,
            Column('a1', Integer, primary_key=True),
            Column('a2', String(20)),
            Column('c1', Integer, ForeignKey('c.c1')))

        Table('c2a1', metadata,
            Column('c1', Integer, ForeignKey('c.c1')),
            Column('a1', Integer, ForeignKey('a.a1')))

        Table('c2a2', metadata,
            Column('c1', Integer, ForeignKey('c.c1')),
            Column('a1', Integer, ForeignKey('a.a1')))

        Table('b', metadata,
            Column('b1', Integer, primary_key=True),
            Column('a1', Integer, ForeignKey('a.a1')),
            Column('b2', sa.Boolean))

    def test_basic(self):
        a, c, b, c2a1, c2a2 = (self.tables.a,
                                self.tables.c,
                                self.tables.b,
                                self.tables.c2a1,
                                self.tables.c2a2)

        class C(object):pass
        class A(object):pass
        class B(object):pass

        mapper(B, b)

        mapper(A, a, properties={
            'tbs': relationship(B, primaryjoin=sa.and_(b.c.a1 == a.c.a1,
                                                   b.c.b2 == True),
                            lazy='joined')})

        mapper(C, c, properties={
            'a1s': relationship(A, secondary=c2a1, lazy='joined'),
            'a2s': relationship(A, secondary=c2a2, lazy='joined')})

        assert create_session().query(C).with_labels().statement is not None

        # TODO: seems like just a test for an ancient exception throw.
        # how about some data/inserts/queries/assertions for this one

class M2MTest4(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        table1 = Table("table1", metadata,
            Column('col1', Integer, primary_key=True, test_needs_autoincrement=True),
            Column('col2', String(30))
            )

        table2 = Table("table2", metadata,
            Column('col1', Integer, primary_key=True, test_needs_autoincrement=True),
            Column('col2', String(30)),
            )

        table3 = Table('table3', metadata,
            Column('t1', Integer, ForeignKey('table1.col1')),
            Column('t2', Integer, ForeignKey('table2.col1')),
            )

    def test_delete_parent(self):
        table2, table3, table1 = (self.tables.table2,
                                self.tables.table3,
                                self.tables.table1)

        class A(fixtures.ComparableEntity):
            pass
        class B(fixtures.ComparableEntity):
            pass

        mapper(A, table1, properties={
            'bs':relationship(B, secondary=table3, backref='as', order_by=table3.c.t1)
        })
        mapper(B, table2)

        sess = create_session()
        a1 = A(col2='a1')
        a2 = A(col2='a2')
        b1 = B(col2='b1')
        b2 = B(col2='b2')
        a1.bs.append(b1)
        a2.bs.append(b2)
        for x in [a1,a2]:
            sess.add(x)
        sess.flush()
        sess.expunge_all()

        alist = sess.query(A).order_by(A.col1).all()
        eq_(
            [
                A(bs=[B(col2='b1')]), A(bs=[B(col2='b2')])
            ],
            alist)

        for a in alist:
            sess.delete(a)
        sess.flush()
        eq_(sess.query(table3).count(), 0)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.