-
Notifications
You must be signed in to change notification settings - Fork 68
/
test_activerecord.py
297 lines (220 loc) · 9 KB
/
test_activerecord.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
import unittest
import sqlalchemy
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Query, Session, DeclarativeBase, declarative_base
from sqlalchemy_mixins import ActiveRecordMixin
from sqlalchemy_mixins.activerecord import ModelNotFoundError
class Base(DeclarativeBase):
__abstract__ = True
AlternativeBase = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=False)
sess = Session(engine)
# sess = scoped_session(sessionmaker(bind=engine))
class BaseModel(Base, ActiveRecordMixin):
__abstract__ = True
pass
class BaseModelAlternative(AlternativeBase, ActiveRecordMixin):
__abstract__ = True
class User(BaseModel):
__tablename__ = 'user'
__repr_attrs__ = ['name']
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
posts = sa.orm.relationship('Post', backref='user')
posts_viewonly = sa.orm.relationship('Post', viewonly=True)
class UserAlternative(BaseModelAlternative):
__tablename__ = 'user_alt'
__repr_attrs__ = ['name']
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
class Post(BaseModel):
__tablename__ = 'post'
id = sa.Column(sa.Integer, primary_key=True)
body = sa.Column(sa.String)
user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
archived = sa.Column(sa.Boolean, default=False)
# user = backref from User.post
comments = sa.orm.relationship('Comment', backref='post')
@hybrid_property
def public(self):
return not self.archived
@public.setter
def public(self, public):
self.archived = not public
class Comment(BaseModel):
__tablename__ = 'comment'
__repr_attrs__ = ['body']
id = sa.Column(sa.Integer, primary_key=True)
body = sa.Column(sa.String)
user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
post_id = sa.Column(sa.Integer, sa.ForeignKey('post.id'))
user = sa.orm.relationship('User', backref='comments')
# post = backref from Post.comments
class TestActiveRecord(unittest.TestCase):
def setUp(self):
sess.rollback()
BaseModel.set_session(None)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
BaseModel.set_session(sess)
def test_settable_attributes(self):
self.assertEqual(set(User.settable_attributes),
{'id', 'name', # normal columns
'posts', 'comments' # relations
})
self.assertNotIn('posts_viewonly', set(User.settable_attributes))
self.assertEqual(set(Post.settable_attributes),
{'id', 'body', 'user_id', 'archived',
# normal columns
'user', 'comments', # relations
'public' # hybrid attributes
})
self.assertEqual(set(Comment.settable_attributes),
{'id', 'body', 'post_id', 'user_id', # normal columns
'user', 'post' # hybrid attributes
})
def test_fill_and_save(self):
u1 = User()
u1.fill(name='Bill u1')
u1.save()
self.assertEqual(u1, sess.query(User).first())
p11 = Post()
p11.fill(body='p11', user=u1, public=False)
p11.save()
self.assertEqual(p11, sess.query(Post).first())
self.assertEqual(p11.archived, True)
def test_save_commits(self):
with self.assertRaises(sqlalchemy.exc.InvalidRequestError):
with sess.begin():
u1 = User()
u1.fill(name='Bill u1')
u1.save()
u2 = User()
u2.fill(name='Bill u2')
u2.save()
self.assertEqual([u1, u2], sess.query(User).order_by(User.id.asc()).all())
# The first user is saved even when the block raises a Exception
self.assertEqual([u1], sess.query(User).order_by(User.id.asc()).all())
def test_save_do_not_commit(self):
with sess.begin():
u1 = User()
u1.fill(name='Bill u1')
u1.save(commit=False)
u2 = User()
u2.fill(name='Bill u2')
u2.save(commit=False)
self.assertEqual([u1,u2], sess.query(User).order_by(User.id.asc()).all())
def test_create(self):
u1 = User.create(name='Bill u1')
self.assertEqual(u1, sess.query(User).first())
p11 = Post.create(body='p11', user=u1, public=False)
self.assertEqual(p11, sess.query(Post).first())
self.assertEqual(p11.archived, True)
@staticmethod
def _seed():
u1 = User(name='Bill', id=1)
sess.add(u1)
sess.flush()
u2 = User(name='Bishop', id=2)
sess.add(u2)
sess.flush()
p11 = Post(body='p11', user=u1, public=False, id=11)
sess.add(p11)
sess.flush()
p12 = Post(body='p12', user=u2, id=12)
sess.add(p12)
sess.flush()
p13 = Post(body='p13', user=u1, id=13)
sess.add(p13)
sess.flush()
return u1, u2, p11, p12, p13
def test_update(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(sess.query(Post).get(11).body, 'p11')
self.assertEqual(sess.query(Post).get(11).public, False)
self.assertEqual(sess.query(Post).get(11).user, u1)
p11.update(body='new body', public=True, user=u2)
self.assertEqual(sess.query(Post).get(11).body, 'new body')
self.assertEqual(sess.query(Post).get(11).public, True)
self.assertEqual(sess.query(Post).get(11).user, u2)
def test_update_no_commit(self):
u1 = User(name='Bill', id=1)
u1.save()
u1.update(name='Joe', commit=False)
self.assertEqual('Joe', sess.query(User).where(User.id==1).first().name)
sess.rollback()
self.assertEqual('Bill', sess.query(User).where(User.id==1).first().name)
def test_fill_wrong_attribute(self):
u1 = User(name='Bill u1')
sess.add(u1)
sess.flush()
with self.assertRaises(KeyError):
u1.fill(INCORRECT_ATTRUBUTE='nomatter')
with self.assertRaises(KeyError):
u1.update(INCORRECT_ATTRUBUTE='nomatter')
with self.assertRaises(KeyError):
User.create(INCORRECT_ATTRUBUTE='nomatter')
def test_delete(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(sess.query(User).get(1), u1)
u1.delete()
self.assertEqual(sess.query(User).get(1), None)
def test_delete_without_commit(self):
u1 = User()
u1.save()
u1.delete(commit=False)
self.assertIsNone(sess.query(User).one_or_none())
sess.rollback()
self.assertIsNotNone(sess.query(User).one_or_none())
def test_destroy(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(set(sess.query(Post).all()), {p11, p12, p13})
Post.destroy(11, 12)
self.assertEqual(set(sess.query(Post).all()), {p13})
def test_destroy_no_commit(self):
u1, u2, p11, p12, p13 = self._seed()
sess.commit()
self.assertEqual(set(sess.query(Post).order_by(Post.id).all()), {p11, p12, p13})
Post.destroy(11, 12, commit=False)
self.assertEqual(set(sess.query(Post).order_by(Post.id).all()), {p13})
sess.rollback()
self.assertEqual(set(sess.query(Post).order_by(Post.id).all()), {p11, p12, p13})
def test_all(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(set(User.all()), {u1, u2})
self.assertEqual(set(Post.all()), {p11, p12, p13})
def test_first(self):
u1 = User()
sess.add(u1)
sess.flush()
self.assertEqual(User.first(), u1)
def test_find(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(User.find(1), u1)
self.assertEqual(User.find(2), u2)
self.assertEqual(User.find(123456789), None)
def test_find_or_fail(self):
u1, u2, p11, p12, p13 = self._seed()
self.assertEqual(User.find_or_fail(1), u1)
self.assertEqual(User.find_or_fail(2), u2)
with self.assertRaises(ModelNotFoundError):
_ = User.find_or_fail(123456789)
class TestActiveRecordAlternative(unittest.TestCase):
def setUp(self):
sess.rollback()
BaseModelAlternative.set_session(None)
AlternativeBase.metadata.drop_all(engine)
AlternativeBase.metadata.create_all(engine)
BaseModelAlternative.set_session(sess)
def test_create(self):
u1 = UserAlternative.create(name='Bill u1')
self.assertEqual(u1, sess.query(UserAlternative).first())
def test_create_no_commit(self):
u1 = UserAlternative.create(name='Bill u1', commit=False)
self.assertEqual(u1, sess.query(UserAlternative).first())
sess.rollback()
self.assertIsNone(sess.query(UserAlternative).one_or_none())
if __name__ == '__main__': # pragma: no cover
unittest.main()