forked from danthedeckie/DictLiteStore
/
tests.py
403 lines (280 loc) · 10.1 KB
/
tests.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Tests for DictLiteStore.
------------------------
'''
import sys
reload(sys)
sys.setdefaultencoding('utf-8') # pylint: disable=no-member
import os
import os.path
from dictlitestore import DictLiteStore, NoJSON, json_or_raw
import unittest
# pylint: disable=missing-docstring, invalid-name, too-many-public-methods
############################################
class TestJson_or_raw(unittest.TestCase):
def testNoJson(self):
self.assertEqual("Text", NoJSON("Text"))
self.assertEqual("Text", json_or_raw(NoJSON("Text")))
def testNormal(self):
self.assertEqual('"Text"', json_or_raw("Text"))
self.assertEqual('"T\' []ext"', json_or_raw("T' []ext"))
self.assertEqual('"T\\"ext"', json_or_raw("T\"ext"))
def testNone(self):
self.assertEqual(None, json_or_raw(None))
def testNonJSONable(self):
class Bug(object):
pass
bug = Bug()
with self.assertRaises(TypeError):
json_or_raw(bug)
###########################################
#
# Two boilerplate removal functions for later:
#
###########################################
class Basic(unittest.TestCase):
def store_and_get(self, d):
''' stores a dictionary in a :memory: / default DictLiteStore,
and returns all rows in that table '''
with DictLiteStore() as s:
s.store(d)
dd = s.get()
self.assertEqual(len(dd), 1)
return dd
def store_and_compare(self, original, result=False):
''' stores a dictionary, retrieves it again,
and compares with an expected result. '''
retrieved = self.store_and_get(original)[0]
if not result:
self.assertEqual(retrieved, original)
else:
self.assertEqual(retrieved, result)
#########################################
#
# Very simple store and return tests for basic data types.
#
#########################################
class TestBasicDataTypes(Basic):
def test_text_only(self):
a = {'col1':'data1', 'col2':'data2'}
self.store_and_compare(a)
def test_unicode_text(self):
a = {'colενα': u'πραγμα', 'colδυο': 'älles güt'}
self.store_and_compare(a)
def test_int(self):
a = {'col1': 42}
self.store_and_compare(a)
def test_float(self):
a = {'col1': 3.14 }
self.store_and_compare(a)
def test_list(self):
a = {'col1': ['this', 'is', 'a', 'list']}
self.store_and_compare(a)
def test_unicode_list(self):
a = {'col1': ['αὐτο', 'είναι', 'εναν', 'unicode', 'list']}
self.store_and_compare(a)
def test_dict(self):
a = {'col1':{'subdict':'value'}}
self.store_and_compare(a)
def test_function(self):
a = {'col1':'should work', 'col2': map}
b = {'col1':'should work', 'col2': '<built-in function map>'}
self.store_and_compare(a, b)
def test_class(self):
c = object()
a = {'col1': c }
b = self.store_and_get(a)[0]
self.assertTrue(b['col1'].startswith('<object object at'))
##########################################
#
# OK. We've passed basic sanity tests,
# let's try doing more interesting tests.
#
##########################################
# some generic data:
ROW1 = {'col1': 'data1', 'col2': 'data2'}
ROW2 = {'col3': 'data3', 'col4': 'data4'}
# a generic update:
UPDATE1 = {'col1': 'UPDATED'}
# a generic WHERE clause, which matches ROW1
GOODWHERE = ('col1', '==', 'data1')
# a generic WHERE clause, which doesn't match
BADWHERE = ('col1', '==', 'bogus')
# 'Bad Names' (for columns) tests:
SILLY_COLUMN_NAMES = ('"', # "
'""', # ""
'"col1', # "col1
"'", # '
"''", # ''
'\\', # \\
'(', # (
';', # ;
'INSERT', # INSERT
'==', # ==
'\";', # \"
)
# a boilerplate reduction function:
def copy_change(original, updates):
''' makes a copy of dict $original, and updates it with $updates '''
# I suspect there is a standard library function for this.
new = original.copy()
new.update(updates)
return new
class TestSort(Basic):
def test_basic_good_sorts(self):
with DictLiteStore() as s:
a = {"a": 0, "b": "y"}
b = {"a": 2, "b": "x"}
c = {"a": 20917203912, "b": "z"}
s.store(a)
s.store(b)
s.store(c)
rows = s.get()
self.assertEqual(len(rows), 3)
self.assertEqual([a, b, c], rows)
rows = s.get(order="b")
self.assertEqual([b, a, c], rows)
rows = s.get(order=None)
self.assertEqual([a, b, c], rows)
rows = s.get(order=[("b", "DESC")])
self.assertEqual([c, a, b], rows)
rows = s.get(order=[("a", "ASC")])
self.assertEqual([a, b, c], rows)
rows = s.get(order=[("a", "DESC")])
self.assertEqual([c, b, a], rows)
class TestDelete(Basic):
def test_basic_delete(self):
with DictLiteStore() as s:
a = {"a": 0, "b": "y"}
b = {"a": 2, "b": "x"}
c = {"a": 20917203912, "b": "z"}
s.store(a)
s.store(b)
s.store(c)
rows = s.get()
self.assertEqual(len(rows), 3)
self.assertEqual([a, b, c], rows)
s.delete(("a", "==", 0))
rows = s.get()
self.assertEqual(len(rows), 2)
self.assertEqual([b, c], rows)
s.delete()
rows = s.get()
self.assertEqual(len(rows), 0)
# And the update tests:
class TestUpdates(Basic):
def test_multiple_rows_same_columns(self):
with DictLiteStore() as s:
s.store(ROW1)
s.store(ROW1)
c = s.get()
self.assertEqual(c, [ROW1, ROW1])
def test_getting_rows_using_NoJSON(self):
with DictLiteStore() as s:
s.store(ROW1)
s.store(ROW2)
c = s.get(("col1", "==", NoJSON('"data1"')))
self.assertEqual(c, [ROW1])
def test_rows_with_different_columns(self):
with DictLiteStore() as s:
s.store(ROW1)
s.store(ROW2)
c = s.get()
self.assertEqual(c[0], ROW1)
self.assertEqual(c[1], ROW2)
def test_update_all_rows_with_one_entry(self):
post_update = copy_change(ROW1, UPDATE1)
with DictLiteStore() as s:
s.store(ROW1)
s.update(UPDATE1)
from_db = s.get()
self.assertEqual(from_db[0], post_update)
def test_update_all_rows_with_multiple_entries(self):
post_update_a = copy_change(ROW1, UPDATE1)
post_update_b = copy_change(ROW2, UPDATE1)
with DictLiteStore() as s:
s.store(ROW1)
s.store(ROW2)
s.update(UPDATE1)
from_db = s.get()
self.assertEqual(from_db[0], post_update_a)
self.assertEqual(from_db[1], post_update_b)
def test_update_single_row(self):
post_update_a = copy_change(ROW1, UPDATE1)
with DictLiteStore() as s:
s.store(ROW1)
s.store(ROW2)
s.update(UPDATE1, False, GOODWHERE)
from_db = s.get()
self.assertEqual(from_db[0], post_update_a)
self.assertEqual(from_db[1], ROW2)
def test_update_fallbackto_insert(self):
with DictLiteStore() as s:
s.store(ROW1)
s.update(UPDATE1, True, BADWHERE)
from_db = s.get()
self.assertEqual(from_db[0], ROW1)
self.assertEqual(from_db[1], UPDATE1)
def test_update_fallbackto_nothing(self):
with DictLiteStore() as s:
s.store(ROW1)
s.update(UPDATE1, False, BADWHERE)
from_db = s.get()
self.assertEqual(len(from_db), 1)
self.assertEqual(from_db[0], ROW1)
def test_update_empty_table(self):
with DictLiteStore() as s:
s.update(UPDATE1, False, BADWHERE)
from_db = s.get()
self.assertEqual(from_db, [])
def test_update_empty_table_fallbackto_insert(self):
with DictLiteStore() as s:
s.update(UPDATE1, True, BADWHERE)
from_db = s.get()
self.assertEqual(from_db, [UPDATE1])
def test_various_badnames_store_get_only(self):
for x in SILLY_COLUMN_NAMES:
a = {x:'data1', 'col2':'data2'}
self.store_and_compare(a)
def test_various_badname_update(self):
for x in SILLY_COLUMN_NAMES:
a = {x:'data1'}
with DictLiteStore() as s:
s.store(a)
s.update({x:'UPDATED'})
c = s.get()
self.assertEqual(c, [{x:'UPDATED'}])
def test_invalid_operator(self):
with DictLiteStore() as s:
with self.assertRaises(KeyError):
s.update(UPDATE1, True, ("thing", '"should break"', "value"))
class TestUsingFile(unittest.TestCase):
# Other tests:
def test_db_file(self):
# First test it doesn't already exist:
self.assertEqual(os.path.exists('__test.db'), False)
# Store some data:
with DictLiteStore('__test.db') as s:
s.store(ROW1)
s.store(ROW2)
# Should be saved.
self.assertTrue(os.path.exists('__test.db'))
# Retreive it again, check it's alright:
with DictLiteStore('__test.db') as s:
c = s.get()
self.assertEqual(c, [ROW1, ROW2])
# remove it.
os.remove('__test.db')
self.assertEqual(os.path.exists('__test.db'), False)
# TODO:
# - test a different table name.
# - test deletion
# - test custom SQL schema databases.
if __name__ == '__main__':
try:
import nose
nose.run()
except ImportError:
unittest.main()