root / elixir / trunk / tests / test_custombase.py

Revision 516, 5.4 kB (checked in by ged, 3 years ago)

added tests for custom collection, or custom session on base class (they serve
more as examples than real tests but that's already quite useful).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1"""
2test having entities using a custom base class
3"""
4
5from elixir import *
6import elixir
7
8def setup():
9    metadata.bind = 'sqlite://'
10
11    global MyBase
12
13    class MyBase(object):
14        __metaclass__ = EntityMeta
15
16        def __init__(self, **kwargs):
17            for key, value in kwargs.items():
18                setattr(self, key, value)
19
20class TestCustomBase(object):
21    def teardown(self):
22        cleanup_all(True)
23
24    def test_simple(self):
25        class A(MyBase):
26            name = Field(String(30))
27
28        setup_all(True)
29
30        a1 = A(name="a1")
31
32        session.commit()
33        session.expunge_all()
34
35        a = A.query.filter_by(name="a1").one()
36
37        assert a.name == 'a1'
38
39    def test_bad_property(self):
40        # create a meta entity which mimick cases where dir() method
41        # will report attribute that can't be directly accessed.
42        # Note: this happens with zope.interface. See ticket #98.
43        class BrokenDescriptor(object):
44            def __get__(*args):
45                raise AttributeError
46
47        class MyEntity(EntityBase):
48            __metaclass__ = EntityMeta
49
50            d = BrokenDescriptor()
51
52        class A(MyEntity):
53            value = Field(Unicode)
54
55        # we just check that the instrument_class phases doesn't trigger an
56        # exception
57
58    def test_inherit(self):
59        class A(MyBase):
60            name = Field(String(30))
61
62        class B(A):
63            data = Field(String(30))
64
65        setup_all(True)
66
67        a1 = A(name="a1")
68        b1 = B(name="b1", data="-b1-")
69
70        session.commit()
71        session.expunge_all()
72
73        b = A.query.filter_by(name="b1").one()
74
75        assert b.data == '-b1-'
76
77    def test_non_object_base(self):
78        class BaseParent(object):
79            def test(self):
80                return "success"
81
82        class InheritedBase(BaseParent):
83            __metaclass__ = EntityMeta
84
85        class A(InheritedBase):
86            name = Field(String(30))
87
88        setup_all(True)
89
90        a1 = A()
91        a1.name = "a1"
92
93        session.commit()
94        session.expunge_all()
95
96        a = A.query.filter_by(name="a1").one()
97
98        assert a.name == 'a1'
99        assert a.test() == "success"
100
101    def test_base_with_fields(self):
102        class FieldBase(object):
103            __metaclass__ = EntityMeta
104
105            common = Field(String(32))
106
107        class A(FieldBase):
108            name = Field(String(32))
109
110        class B(FieldBase):
111            pass
112
113        setup_all(True)
114
115        assert 'name' in A.table.columns
116        assert 'common' in A.table.columns
117        assert 'common' in B.table.columns
118
119    def test_base_with_relation(self):
120        class FieldBase(object):
121            __metaclass__ = EntityMeta
122
123            common = ManyToOne('A')
124
125        class A(FieldBase):
126            name = Field(String(32))
127
128        class B(FieldBase):
129            pass
130
131        setup_all(True)
132
133        assert 'name' in A.table.columns
134        assert 'common_id' in A.table.columns
135        assert 'common_id' in B.table.columns
136
137    def test_base_with_fields_in_parent(self):
138        class BaseParent(object):
139            common1 = Field(String(32))
140
141        class FieldBase(BaseParent):
142            __metaclass__ = EntityMeta
143
144            common2 = Field(String(32))
145
146        class A(FieldBase):
147            name = Field(String(32))
148
149        class B(FieldBase):
150            pass
151
152        setup_all(True)
153
154        assert 'name' in A.table.columns
155        assert 'common1' in A.table.columns
156        assert 'common1' in B.table.columns
157        assert 'common2' in A.table.columns
158        assert 'common2' in B.table.columns
159
160    def test_base_with_options(self):
161        import re
162
163        def camel_to_underscore(entity):
164            return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower()
165
166        class OptionBase(object):
167            __metaclass__ = EntityMeta
168
169            options_defaults = dict(tablename=camel_to_underscore)
170            using_options_defaults(identity=camel_to_underscore)
171            using_options_defaults(inheritance='multi')
172
173        class TestA(OptionBase):
174            name = Field(String(32))
175
176        class SuperTestB(TestA):
177            pass
178
179        setup_all(True)
180
181        assert TestA.table.name == 'test_a'
182        assert SuperTestB.table.name == 'super_test_b'
183        assert TestA._descriptor.identity == 'test_a'
184
185    def test_base_custom_collection(self):
186        global A, B
187
188        collection = elixir.collection.RelativeEntityCollection()
189
190        class Base(object):
191            __metaclass__ = EntityMeta
192            using_options_defaults(collection=collection)
193
194        class A(Base):
195            b = ManyToOne('B')
196
197        class B(Base):
198            a = OneToOne('A')
199
200        assert not elixir.entities
201        assert A.table is None
202        assert B.table is None
203
204        setup_entities(collection)
205
206        assert A.table is not None
207        assert B.table is not None
208
209        del A
210        del B
211
212    def test_base_custom_session(self):
213        from sqlalchemy.orm import sessionmaker
214
215        class Base(object):
216            __metaclass__ = EntityMeta
217            using_options_defaults(session=None)
218
219        class A(Base):
220            b = ManyToOne('B')
221
222        class B(Base):
223            a = OneToOne('A')
224       
225        setup_all(True)
226
227        a = A()
228
229        assert a not in elixir.session
230
231        session = sessionmaker()()
232        session.add(a)
233        assert a in session
Note: See TracBrowser for help on using the browser.