This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sqlite_mapper.py
314 lines (263 loc) · 9.46 KB
/
test_sqlite_mapper.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Cheetah ORM - SQLite Mapper Unit Tests"""
from datetime import datetime
import unittest
from cheetah_orm.fields import (
BigIntField,
BlobField,
DateTimeField,
DoubleField,
IntField,
FloatField,
PasswordField,
StringField
)
from cheetah_orm.indexes import ForeignKey, Index, UniqueIndex
from cheetah_orm.mappers import SQLiteMapper
from cheetah_orm.model import DataModel
# Classes
# =======
class User(DataModel):
table = "users"
name = StringField(length=32, not_null=True)
pswd = PasswordField(length=128, not_null=True)
email = StringField(length=128, not_null=True)
question = StringField(length=128, not_null=True)
answer = StringField(length=128, not_null=True)
joined = DateTimeField(not_null=True, default="now()")
ban = DateTimeField(not_null=True, default="now()")
name_idx = UniqueIndex("name")
email_idx = UniqueIndex("email")
joined_idx = Index("joined")
class Post(DataModel):
table = "posts"
user = BigIntField(not_null=True)
date = DateTimeField(not_null=True, default="now()")
content = StringField(length=65535, not_null=True)
user_idx = ForeignKey(User, "id")
date_idx = Index("date")
class TestSQLiteMapper(unittest.TestCase):
"""SQLite Mapper Tests"""
@classmethod
def setUpClass(cls):
"""Cleanup data from last test."""
mapper = SQLiteMapper()
mapper.connect(database="test.db")
mapper._cur.execute("DROP TABLE IF EXISTS `posts`;")
mapper._cur.execute("DROP TABLE IF EXISTS `users`;")
mapper.disconnect()
def test_1_connection(self):
"""Test database connection."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Disconnect from the database
mapper.disconnect()
def test_2_init_model(self):
"""Test data model initialization."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Check SQL cache
self.assertEqual(mapper._cache[User]["insert"], "INSERT INTO `users`(`name`,`pswd`,`email`,`question`,`answer`,`joined`,`ban`) VALUES (?,?,?,?,?,?,?);")
self.assertEqual(mapper._cache[User]["update"], "UPDATE `users` SET `name`=?,`pswd`=?,`email`=?,`question`=?,`answer`=?,`joined`=?,`ban`=? WHERE `id`=?;")
self.assertEqual(mapper._cache[User]["delete"], "DELETE FROM `users` WHERE `id`=?;")
self.assertEqual(mapper._cache[User]["select"], "SELECT `id`,`name`,`pswd`,`email`,`question`,`answer`,`joined`,`ban` FROM `users`")
self.assertEqual(mapper._cache[Post]["insert"], "INSERT INTO `posts`(`user`,`date`,`content`) VALUES (?,?,?);")
self.assertEqual(mapper._cache[Post]["update"], "UPDATE `posts` SET `user`=?,`date`=?,`content`=? WHERE `id`=?;")
self.assertEqual(mapper._cache[Post]["delete"], "DELETE FROM `posts` WHERE `id`=?;")
self.assertEqual(mapper._cache[Post]["select"], "SELECT `id`,`user`,`date`,`content` FROM `posts`")
# Disconnect from the database
mapper.disconnect()
def test_3_save_model(self):
"""Test data model saving."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Create some data models and save them
daniel = User(
name="Daniel",
pswd="lion",
email="[email protected]",
question="Favorite species?",
answer="lion"
)
leila = User(
name="Leila",
pswd="lioness",
email="[email protected]",
question="Favorite species?",
answer="lioness"
)
james = User(
name="James",
pswd="cheetah",
email="[email protected]",
question="Favorite species?",
answer="cheetah"
)
abby = User(
name="Abby",
pswd="cheetahess",
email="[email protected]",
question="Favorite species?",
answer="cheetah"
)
fiona = User(
name="Fiona",
pswd="fox",
email="[email protected]",
question="Favorite species?",
answer="fox"
)
unknown = User(
name="Unknown",
pswd="",
email="",
question="",
answer=""
)
mapper.save_model(daniel)
mapper.save_model(leila)
mapper.save_model(james)
mapper.save_model(abby)
mapper.save_model(fiona)
mapper.save_model(unknown)
mapper.commit()
# Check model IDs
self.assertEqual(daniel.id, 1)
self.assertEqual(leila.id, 2)
self.assertEqual(james.id, 3)
self.assertEqual(abby.id, 4)
self.assertEqual(fiona.id, 5)
self.assertEqual(unknown.id, 6)
# Update the models and save them
daniel.question = "Favorite animal?"
leila.question = "Favorite animal?"
james.question = "Favorite animal?"
abby.question = "Favorite animal?"
fiona.question = "Favorite animal?"
mapper.save_model(daniel)
mapper.save_model(leila)
mapper.save_model(james)
mapper.save_model(abby)
mapper.save_model(fiona)
mapper.commit()
# Disconnect from the database
mapper.disconnect()
def test_4_delete_model(self):
"""Test data model deletion."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Delete a model
mapper.delete_model(User(id=6))
mapper.commit()
# Disconnect from the database
mapper.disconnect()
def test_5_filter(self):
"""Test data model filtering."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Fetch all users
users = mapper.filter(User)
self.assertEqual(len(users), 5)
# Fetch Daniel
users = mapper.filter(User, "`name`=?", "Daniel")
self.assertEqual(len(users), 1)
self.assertEqual(users[0].name, "Daniel")
# Fetch Leila and Abby
users = mapper.filter(User, "`name`=? OR `name`=?", "Leila", "Abby")
self.assertEqual(len(users), 2)
for user in users:
self.assertTrue(user.name in ["Leila", "Abby"])
# Fetch all users with "Favorite animal?" as their security question ordered by username
users = mapper.filter(User, "`question`=?", "Favorite animal?", order_by=["name"])
self.assertEqual(len(users), 5)
self.assertEqual(users[0].name, "Abby")
self.assertEqual(users[4].name, "Leila")
# The middle 2 users
users = mapper.filter(User, "`question`=?", "Favorite animal?", order_by=["name"], offset=1, limit=2)
self.assertEqual(len(users), 2)
self.assertEqual(users[0].name, "Daniel")
self.assertEqual(users[1].name, "Fiona")
# Disconnect from the database
mapper.disconnect()
def test_6_foreign_keys(self):
"""Test foreign keys."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Create some posts
post = Post(
user=1,
content="Hello everyone!"
)
mapper.save_model(post)
post = Post(
user=2,
content="Hello!"
)
mapper.save_model(post)
post = Post(
user=3,
content="Hi!"
)
mapper.save_model(post)
post = Post(
user=4,
content="Heya!"
)
mapper.save_model(post)
post = Post(
user=5,
content="Hello darling!"
)
mapper.save_model(post)
post = Post(
user=5,
content="Huh? ...Help!"
)
mapper.save_model(post)
post = Post(
user=1,
content="Oh no!"
)
mapper.save_model(post)
# Now delete a user
mapper.delete_model(User(id=5))
mapper.commit()
# Verify that the user's posts were deleted too
posts = mapper.filter(Post, "`user`=?", 5)
self.assertEqual(len(posts), 0)
# Disconnect from the database
mapper.disconnect()
def test_7_count_results(self):
"""Count the number of results in a result set."""
# Establish a database connection
mapper = SQLiteMapper()
mapper.connect(database="test.db")
# Initialize data models
mapper.init_model(User)
mapper.init_model(Post)
# Test result counting
user_cnt = mapper.count(User)
users = mapper.filter(User)
self.assertEqual(user_cnt, len(users))
# Disconnect from the database
mapper.disconnect()