root / elixir / trunk / tests / test_m2m.py @ 297

Revision 297, 4.0 kB (checked in by cleverdevil, 5 years ago)

Added a column_format keyword argument to ManyToMany which can be used to specify an alternate format string for column names in the mapping table. This is required to properly integrate with legacy TurboGears identity databases.

Line 
1"""
2test many to many relationships
3"""
4
5from elixir import *
6
7#-----------
8
9class TestManyToMany(object):
10    def setup(self):
11        metadata.bind = 'sqlite:///'
12   
13    def teardown(self):
14        cleanup_all(True)
15       
16    def test_simple(self):
17        class A(Entity):
18            name = Field(String(60))
19            bs_ = ManyToMany('B')
20
21        class B(Entity):
22            name = Field(String(60))
23            as_ = ManyToMany('A')
24
25        setup_all(True)
26
27        b1 = B(name='b1', as_=[A(name='a1')])
28
29        session.flush()
30        session.clear()
31
32        a = A.query.one()
33        b = B.query.one()
34
35        assert a in b.as_
36        assert b in a.bs_
37   
38    def test_column_format(self):
39        class A(Entity):
40            using_options(tablename='aye')
41            name = Field(String(60))
42            bs_ = ManyToMany('B', column_format='%(entity)s_%(key)s')
43
44        class B(Entity):
45            using_options(tablename='bee')
46            name = Field(String(60))
47            as_ = ManyToMany('A', column_format='%(entity)s_%(key)s')
48
49        setup_all(True)
50
51        b1 = B(name='b1', as_=[A(name='a1')])
52
53        session.flush()
54        session.clear()
55
56        a = A.query.one()
57        b = B.query.one()
58
59        assert a in b.as_
60        assert b in a.bs_
61       
62        found_a = False
63        found_b = False
64        for column in A.mapper.get_property('bs_').secondary.columns:
65            if column.name == 'a_id': found_a = True
66            elif column.name == 'b_id': found_b = True
67        assert found_a
68        assert found_b
69   
70    def test_multi_pk_in_target(self):
71        class A(Entity):
72            key1 = Field(Integer, primary_key=True, autoincrement=False)
73            key2 = Field(String(40), primary_key=True)
74
75            bs_ = ManyToMany('B')
76
77        class B(Entity):
78            name = Field(String(60))
79            as_ = ManyToMany('A')
80
81        setup_all(True)
82
83        b1 = B(name='b1', as_=[A(key1=10, key2='a1')])
84
85        session.flush()
86        session.clear()
87
88        a = A.query.one()
89        b = B.query.one()
90
91        assert a in b.as_
92        assert b in a.bs_
93
94    def test_multi(self):
95        class A(Entity):
96            name = Field(String(100))
97
98            rel1 = ManyToMany('B')
99            rel2 = ManyToMany('B')
100           
101        class B(Entity):
102            name = Field(String(20), primary_key=True)
103
104        setup_all(True)
105       
106        b1 = B(name='b1')
107        a1 = A(name='a1', rel1=[B(name='b2'), b1],
108                          rel2=[B(name='b3'), B(name='b4'), b1])
109
110        session.flush()
111        session.clear()
112       
113        a1 = A.query.one()
114        b1 = B.get_by(name='b1')
115        b2 = B.get_by(name='b2')
116
117        assert b1 in a1.rel1 
118        assert b1 in a1.rel2
119        assert b2 in a1.rel1
120
121    def test_selfref(self):
122        class Person(Entity):
123            name = Field(String(30))
124           
125            friends = ManyToMany('Person')
126
127        setup_all(True)
128
129        barney = Person(name="Barney")
130        homer = Person(name="Homer", friends=[barney])
131        barney.friends.append(homer)
132
133        session.flush()
134        session.clear()
135       
136        homer = Person.get_by(name="Homer")
137        barney = Person.get_by(name="Barney")
138
139        assert homer in barney.friends
140        assert barney in homer.friends
141
142    def test_has_and_belongs_to_many(self):
143        class A(Entity):
144            has_field('name', String(100))
145
146            has_and_belongs_to_many('bs', of_kind='B')
147           
148        class B(Entity):
149            has_field('name', String(100), primary_key=True)
150
151        setup_all(True)
152       
153        b1 = B(name='b1')
154        a1 = A(name='a1', bs=[B(name='b2'), b1])
155        a2 = A(name='a2', bs=[B(name='b3'), b1])
156        a3 = A(name='a3')
157
158        session.flush()
159        session.clear()
160       
161        a1 = A.get_by(name='a1')
162        a2 = A.get_by(name='a2')
163        a3 = A.get_by(name='a3')
164        b1 = B.get_by(name='b1')
165        b2 = B.get_by(name='b2')
166
167        assert b1 in a1.bs
168        assert b2 in a1.bs
169        assert b1 in a2.bs
170        assert not a3.bs
Note: See TracBrowser for help on using the browser.