root / elixir / trunk / tests / test_abstract.py

Revision 516, 6.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 inheritance with abstract entities
3"""
4
5import re
6
7from elixir import *
8import elixir
9
10def camel_to_underscore(entity):
11    return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower()
12
13def setup():
14    # this is an ugly hack because globally defined entities of other tests
15    # (a.*, b.*, db1.* and db2.*) leak into this one because of nosetests
16    # habit of importing all modules before running the first test.
17    # test_abstract is usually the first test to be run, so it gets all the
18    # crap.
19    cleanup_all()
20
21    metadata.bind = 'sqlite://'
22    elixir.options_defaults['shortnames'] = True
23
24def teardown():
25    elixir.options_defaults['shortnames'] = False
26
27class TestAbstractInheritance(object):
28    def teardown(self):
29        cleanup_all(True)
30
31    def test_abstract_alone(self):
32        class AbstractPerson(Entity):
33            using_options(abstract=True)
34
35            firstname = Field(String(30))
36            lastname = Field(String(30))
37
38        setup_all(True)
39
40        assert not hasattr(AbstractPerson, 'table')
41
42    def test_inheritance(self):
43        class AbstractPerson(Entity):
44            using_options(abstract=True)
45            using_options_defaults(tablename=camel_to_underscore)
46
47            firstname = Field(String(30))
48            lastname = Field(String(30))
49
50        class AbstractEmployee(AbstractPerson):
51            using_options(abstract=True)
52            using_options_defaults(identity=camel_to_underscore)
53
54            corporation = Field(String(30))
55
56        class ConcreteEmployee(AbstractEmployee):
57            service = Field(String(30))
58
59        class ConcreteCitizen(AbstractPerson):
60            country = Field(String(30))
61
62        setup_all(True)
63
64        assert not hasattr(AbstractPerson, 'table')
65        assert not hasattr(AbstractEmployee, 'table')
66        assert hasattr(ConcreteEmployee, 'table')
67        assert hasattr(ConcreteCitizen, 'table')
68        assert ConcreteEmployee.table.name == 'concrete_employee'
69        assert ConcreteCitizen.table.name == 'concrete_citizen'
70
71        assert 'firstname' in ConcreteEmployee.table.columns
72        assert 'lastname' in ConcreteEmployee.table.columns
73        assert 'corporation' in ConcreteEmployee.table.columns
74        assert 'service' in ConcreteEmployee.table.columns
75
76        assert 'firstname' in ConcreteCitizen.table.columns
77        assert 'lastname' in ConcreteCitizen.table.columns
78        assert 'corporation' not in ConcreteCitizen.table.columns
79        assert 'country' in ConcreteCitizen.table.columns
80        # test that the options_defaults do not leak into the parent base
81        assert ConcreteCitizen._descriptor.identity == 'concretecitizen'
82
83    def test_simple_relation(self):
84        class Page(Entity):
85            title = Field(String(30))
86            content = Field(String(30))
87            comments = OneToMany('Comment')
88
89        class AbstractAttachment(Entity):
90            using_options(abstract=True)
91
92            page = ManyToOne('Page')
93            is_spam = Field(Boolean())
94
95        class Comment(AbstractAttachment):
96            message = Field(String(100))
97
98        setup_all(True)
99
100        p1 = Page(title="My title", content="My content")
101        p1.comments.append(Comment(message='My first comment', is_spam=False))
102
103        assert p1.comments[0].page == p1
104        session.commit()
105
106    def test_simple_relation_abstract_wh_multiple_children(self):
107        class Page(Entity):
108            title = Field(String(30))
109            content = Field(String(30))
110            comments = OneToMany('Comment')
111            links = OneToMany('Link')
112
113        class AbstractAttachment(Entity):
114            using_options(abstract=True)
115
116            page = ManyToOne('Page')
117            is_spam = Field(Boolean())
118
119        class Link(AbstractAttachment):
120            url = Field(String(30))
121
122        class Comment(AbstractAttachment):
123            message = Field(String(100))
124
125        setup_all(True)
126
127        p1 = Page(title="My title", content="My content")
128        p1.comments.append(Comment(message='My first comment', is_spam=False))
129        p1.links.append(Link(url="My url", is_spam=True))
130
131        assert p1.comments[0].page == p1
132        session.commit()
133
134    def test_multiple_inheritance(self):
135        class AbstractDated(Entity):
136            using_options(abstract=True)
137            using_options_defaults(tablename=camel_to_underscore)
138
139            #TODO: add defaults
140            created_date = Field(DateTime)
141            modified_date = Field(DateTime)
142
143        class AbstractContact(Entity):
144            using_options(abstract=True)
145            using_options_defaults(identity=camel_to_underscore)
146
147            first_name = Field(Unicode(50))
148            last_name = Field(Unicode(50))
149
150        class DatedContact(AbstractContact, AbstractDated):
151            pass
152
153        setup_all(True)
154
155        assert 'created_date' in DatedContact.table.columns
156        assert 'modified_date' in DatedContact.table.columns
157        assert 'first_name' in DatedContact.table.columns
158        assert 'last_name' in DatedContact.table.columns
159        assert DatedContact._descriptor.identity == 'dated_contact'
160        assert DatedContact.table.name == 'dated_contact'
161
162        contact1 = DatedContact(first_name=u"Guido", last_name=u"van Rossum")
163        session.commit()
164
165    def test_mixed_inheritance(self):
166        class AbstractDated(Entity):
167            using_options(abstract=True)
168            using_options_defaults(tablename=camel_to_underscore)
169
170            #TODO: add defaults
171            created_date = Field(DateTime)
172            modified_date = Field(DateTime)
173
174        class AbstractContact(Entity):
175            using_options(abstract=True)
176            using_options_defaults(identity=camel_to_underscore)
177
178            first_name = Field(Unicode(50))
179            last_name = Field(Unicode(50))
180
181        class Contact(AbstractContact):
182            using_options(inheritance='multi')
183
184        class MixedDatedContact(AbstractDated, Contact):
185            using_options(inheritance='multi')
186
187        setup_all(True)
188
189        assert 'created_date' in MixedDatedContact.table.columns
190        assert 'modified_date' in MixedDatedContact.table.columns
191        assert MixedDatedContact._descriptor.identity == 'mixed_dated_contact'
192        assert MixedDatedContact.table.name == 'mixed_dated_contact'
193
194        contact1 = MixedDatedContact(first_name=u"Guido",
195                                     last_name=u"van Rossum")
196        session.commit()
Note: See TracBrowser for help on using the browser.