-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
64 lines (53 loc) · 1.85 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
import unittest
import pcache
import os
import time
class TestPesistentCache(unittest.TestCase):
def setUp(self):
self.filename = "testcache"
self.cache = pcache.PersistentCache(filename=self.filename)
def tearDown(self):
try:
os.remove(self.filename + ".db")
except FileNotFoundError:
try:
os.remove(self.filename)
except FileNotFoundError:
pass
def test_cache_is_persistent(self):
self.cache["name"] = "John Doe"
del self.cache
cache = pcache.PersistentCache(filename=self.filename)
self.assertEqual(cache["name"], "John Doe")
def test_add_key(self):
name = "John Doe"
self.cache["name"] = name
self.assertEqual(self.cache["name"], name)
def test_remove_key(self):
country = "Japan"
self.cache["country"] = country
self.assertEqual(self.cache["country"], country)
del self.cache["country"]
self.assertIsNone(self.cache["country"])
def test_key_expiry(self):
count = 10
ttl = 2
self.cache["count"] = count
self.assertEqual(self.cache["count"], count)
self.cache.expire("count", ttl)
time.sleep(3)
self.assertIsNone(self.cache["count"])
def test_expiry_ttl_arg(self):
self.cache["name"] = "John Doe"
self.assertRaises(
TypeError, lambda: self.cache.expire("name", ttl="9"))
def test_cache_str(self):
expected = "{'cost': 10}"
self.cache["cost"] = 10
self.assertEqual(str(self.cache), expected)
def test_cache_items(self):
expected = [("age", 27), ("name", "John Doe")]
self.cache["name"] = "John Doe"
self.cache["age"] = 27
self.assertEqual(
sorted(self.cache.items(), key=lambda x: x[0]), expected)