File: test_options.py

package info (click to toggle)
elixir 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 640 kB
  • ctags: 1,079
  • sloc: python: 4,936; makefile: 10
file content (248 lines) | stat: -rw-r--r-- 6,725 bytes parent folder | download
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
"""
test options
"""

from sqlalchemy import UniqueConstraint, create_engine, Column
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.exceptions import SQLError, ConcurrentModificationError
from elixir import *

class TestOptions(object):
    def setup(self):
        metadata.bind = 'sqlite:///'

    def teardown(self):
        cleanup_all()

    # this test is a rip-off SQLAlchemy's activemapper's update test
    def test_version_id_col(self):
        class Person(Entity):
            name = Field(String(30))

            using_options(version_id_col=True)

        setup_all()
        Person.table.create()

        p1 = Person(name='Daniel')
        session.commit()
        session.clear()

        person = Person.query.first()
        person.name = 'Gaetan'
        session.commit()
        assert person.row_version == 2
        session.clear()

        person = Person.query.first()
        person.name = 'Jonathan'
        session.commit()
        assert person.row_version == 3
        session.clear()

        # check that a concurrent modification raises exception
        p1 = Person.query.first()
        s2 = sessionmaker()()
        p2 = s2.query(Person).first()
        p1.name = "Daniel"
        p2.name = "Gaetan"
        s2.commit()
        try:
            session.commit()
            assert False
        except ConcurrentModificationError:
            pass
        s2.close()

    def test_allowcoloverride_false(self):
        class MyEntity(Entity):
            name = Field(String(30))

        setup_all(True)

        raised = False
        try:
            MyEntity._descriptor.add_column(Column('name', String(30)))
        except Exception:
            raised = True

        assert raised

    def test_allowcoloverride_true(self):
        class MyEntity(Entity):
            name = Field(String(30))
            using_options(allowcoloverride=True)

        setup_all()

        # Note that this test is bogus as you cannot just change a column this
        # way since the mapper is already constructed at this point and will
        # use the old column!!! This test is only meant as a way to check no
        # exception is raised.
        #TODO: provide a proper test (using autoloaded tables)
        MyEntity._descriptor.add_column(Column('name', String(30),
                                               default='test'))

    def test_tablename_func(self):
        import re

        def camel_to_underscore(entity):
            return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower()

        options_defaults['tablename'] = camel_to_underscore

        class MyEntity(Entity):
            name = Field(String(30))

        class MySuperTestEntity(Entity):
            name = Field(String(30))

        setup_all(True)

        assert MyEntity.table.name == 'my_entity'
        assert MySuperTestEntity.table.name == 'my_super_test_entity'

        options_defaults['tablename'] = None


class TestSessionOptions(object):
    def setup(self):
        metadata.bind = None

    def teardown(self):
        cleanup_all()

    def test_manual_session(self):
        engine = create_engine('sqlite:///')

        class Person(Entity):
            using_options(session=None)
            firstname = Field(String(30))
            surname = Field(String(30))

        setup_all()
        create_all(engine)

        Session = sessionmaker(bind=engine)
        session = Session()

        homer = Person(firstname="Homer", surname='Simpson')
        bart = Person(firstname="Bart", surname='Simpson')

        session.save(homer)
        session.save(bart)
        session.commit()

        bart.delete()
        session.commit()

        assert session.query(Person).filter_by(firstname='Homer').one() is homer
        assert session.query(Person).count() == 1

    def test_scoped_session(self):
        engine = create_engine('sqlite:///')
        Session = scoped_session(sessionmaker(bind=engine))

        class Person(Entity):
            using_options(session=Session)
            firstname = Field(String(30))
            surname = Field(String(30))

        setup_all()
        create_all(engine)

        homer = Person(firstname="Homer", surname='Simpson')
        bart = Person(firstname="Bart", surname='Simpson')
        Session.commit()

        assert Person.query.session is Session()
        assert Person.query.filter_by(firstname='Homer').one() is homer

    def test_global_scoped_session(self):
        global __session__

        engine = create_engine('sqlite:///')
        session = scoped_session(sessionmaker(bind=engine))
        __session__ = session

        class Person(Entity):
            firstname = Field(String(30))
            surname = Field(String(30))

        setup_all()
        create_all(engine)

        homer = Person(firstname="Homer", surname='Simpson')
        bart = Person(firstname="Bart", surname='Simpson')
        session.commit()

        assert Person.query.session is session()
        assert Person.query.filter_by(firstname='Homer').one() is homer

        del __session__

class TestTableOptions(object):
    def setup(self):
        metadata.bind = 'sqlite:///'

    def teardown(self):
        cleanup_all()

    def test_unique_constraint(self):

        class Person(Entity):
            firstname = Field(String(30))
            surname = Field(String(30))

            using_table_options(UniqueConstraint('firstname', 'surname'))

        setup_all(True)

        homer = Person(firstname="Homer", surname='Simpson')
        bart = Person(firstname="Bart", surname='Simpson')

        session.commit()

        homer2 = Person(firstname="Homer", surname='Simpson')

        raised = False
        try:
            session.commit()
        except SQLError:
            raised = True

        assert raised

    def test_unique_constraint_many_to_one(self):
        class Author(Entity):
            name = Field(String(50))

        class Book(Entity):
            title = Field(String(200), required=True)
            author = ManyToOne("Author")

            using_table_options(UniqueConstraint("title", "author_id"))

        setup_all(True)

        tolkien = Author(name="J. R. R. Tolkien")
        lotr = Book(title="The Lord of the Rings", author=tolkien)
        hobbit = Book(title="The Hobbit", author=tolkien)

        session.commit()

        tolkien2 = Author(name="Tolkien")
        hobbit2 = Book(title="The Hobbit", author=tolkien2)

        session.commit()

        hobbit3 = Book(title="The Hobbit", author=tolkien)

        raised = False
        try:
            session.commit()
        except SQLError:
            raised = True

        assert raised