forked from absent1706/sqlalchemy-mixins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_activerecord.py
208 lines (153 loc) · 6.11 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
import unittest
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Query, Session
from sqlalchemy_mixins import ActiveRecordMixin
from sqlalchemy_mixins.activerecord import ModelNotFoundError
Base = 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 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 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_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_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_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_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)
if __name__ == '__main__': # pragma: no cover
unittest.main()