-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqliteKVStoreTest.py
57 lines (47 loc) · 1.72 KB
/
SqliteKVStoreTest.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
import unittest, os, gc
from SqliteKVStore import SqliteKVStore, KeyExistError
import string, random
import timeit
def generateRandomString(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
class TestSqliteKVStore(unittest.TestCase):
def setUp(self):
self.dbfile = "/tmp/testshortener.db"
self.sql = SqliteKVStore(self.dbfile)
def tearDown(self):
os.remove(self.dbfile)
def assignKeyToValue(self, key, value):
self.sql[key] = value
def test_get(self):
self.assignKeyToValue("Key", "Value")
# Get a non-existent key
self.assertRaises(KeyError, lambda: self.sql["NonExistentKey"])
# Get an existing key
self.assertEquals(self.sql["Key"], "Value")
def test_set(self):
self.assignKeyToValue("Key", "Value")
self.assertRaises(KeyExistError, lambda: self.assignKeyToValue("Key", "Value"))
def test_delete(self):
self.assignKeyToValue("Key", "Value")
self.sql["Key"]
self.assertRaises(KeyExistError, lambda: self.assignKeyToValue("Key", "Value"))
del self.sql["Key"]
self.assertRaises(KeyError, lambda: self.sql["Key"])
# No error after deletion
self.assignKeyToValue("Key", "Value")
def test_persistence(self):
self.assignKeyToValue("Key", "Value")
self.assignKeyToValue("Key2", "Value2")
self.sql = None
gc.collect()
self.sql = SqliteKVStore(self.dbfile)
self.sql["Key"]
self.assertRaises(KeyExistError, lambda: self.assignKeyToValue("Key", "Value"))
del self.sql["Key"]
self.sql = None
gc.collect()
self.sql = SqliteKVStore(self.dbfile)
self.assertRaises(KeyError, lambda: self.sql["Key"])
self.sql["Key2"]
if __name__ == '__main__':
unittest.main()