Skip to content

Commit db97f9e

Browse files
committed
Add unit test analogous to C++ unit test
1 parent 054e624 commit db97f9e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

test/util_caching.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import unittest
3+
import time
4+
5+
from util_caching_py import number_based, time_based
6+
7+
8+
class CacheTest(unittest.TestCase):
9+
def setUp(self):
10+
self.key1 = 1.0
11+
self.key2 = 1.2
12+
self.key3 = 1.6
13+
self.time1 = time.time()
14+
self.time2 = self.time1 + 0.01 # 10 milliseconds later
15+
self.time3 = self.time1 + 1.1 # 1100 milliseconds later
16+
self.time4 = self.time1 + 2.1 # 2100 milliseconds later
17+
self.cache_by_number = number_based.Cache()
18+
self.cache_by_time = time_based.Cache()
19+
self.approximate_number_policy = number_based.ApproximateNumber(0.5)
20+
self.approximate_time_policy = time_based.ApproximateTime(100)
21+
self.approximate_time_policy_2 = time_based.ApproximateTime(1000)
22+
23+
def test_with_number_key(self):
24+
self.assertIsNone(self.cache_by_number.cached(self.key1))
25+
self.cache_by_number.cache(self.key1, 1.0)
26+
27+
# exact match
28+
self.assertTrue(self.cache_by_number.cached(self.key1))
29+
self.assertEqual(self.cache_by_number.cached(self.key1), 1.0)
30+
31+
# approximate match
32+
self.assertTrue(
33+
self.cache_by_number.cached(self.key2, self.approximate_number_policy)
34+
)
35+
self.assertEqual(
36+
self.cache_by_number.cached(self.key2, self.approximate_number_policy),
37+
1.0,
38+
)
39+
40+
# over threshold
41+
self.assertIsNone(
42+
self.cache_by_number.cached(self.key3, self.approximate_number_policy)
43+
)
44+
45+
def test_with_time_key(self):
46+
self.assertIsNone(self.cache_by_time.cached(self.time1))
47+
self.cache_by_time.cache(self.time1, 1.0)
48+
49+
# exact match
50+
self.assertTrue(self.cache_by_time.cached(self.time1))
51+
self.assertEqual(self.cache_by_time.cached(self.time1), 1.0)
52+
53+
# approximate match with milliseconds
54+
self.assertTrue(
55+
self.cache_by_time.cached(self.time2, self.approximate_time_policy)
56+
)
57+
self.assertEqual(
58+
self.cache_by_time.cached(self.time2, self.approximate_time_policy), 1.0
59+
)
60+
61+
# approximate match with seconds
62+
self.assertTrue(
63+
self.cache_by_time.cached(self.time2, self.approximate_time_policy_2)
64+
)
65+
self.assertEqual(
66+
self.cache_by_time.cached(self.time2, self.approximate_time_policy_2), 1.0
67+
)
68+
69+
# over threshold
70+
self.assertIsNone(
71+
self.cache_by_time.cached(self.time3, self.approximate_time_policy)
72+
)
73+
# expect 2s after rounding to integer which is over threshold
74+
self.assertIsNone(
75+
self.cache_by_time.cached(self.time4, self.approximate_time_policy_2)
76+
)
77+
78+
79+
if __name__ == "__main__":
80+
header = "Running " + os.path.basename(__file__)
81+
82+
print("=" * len(header))
83+
print(header)
84+
print("=" * len(header) + "\n")
85+
unittest.main(exit=False)
86+
print("=" * len(header) + "\n")

0 commit comments

Comments
 (0)