-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.stochastic_storage.py
More file actions
71 lines (54 loc) · 2.24 KB
/
Copy pathtest.stochastic_storage.py
File metadata and controls
71 lines (54 loc) · 2.24 KB
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
import os
from random import randrange
import unittest
from src.storage.stochastic_storage import StochasticStorage
class TestStochasticStorage(unittest.TestCase):
def setUp(self):
self.storage = StochasticStorage("test_storage")
def tearDown(self):
self.storage.reset()
os.remove(self.storage.root_path)
self.storage = None
def test_pick_avg(self):
# Test when field exists in the storage
self.storage.insert_list("field1", [1, 2, 3, 4, 5])
avg = self.storage.pick_avg("field1")
self.assertEqual(avg, 3.0)
# Test when field does not exist in the storage
with self.assertRaises(KeyError):
self.storage.pick_avg("field2")
def test_pick_random(self):
# Test when field exists in the storage
self.storage.insert_list("field1", [1, 2, 3, 4, 5])
random_value = self.storage.pick_random("field1")
self.assertIn(random_value, [1, 2, 3, 4, 5])
# Test when field does not exist in the storage
with self.assertRaises(KeyError):
self.storage.pick_random("field2")
def test_pick_top5(self):
# Test when field exists in the storage
self.storage.insert_list("field1", [1, 2, 3, 4, 5])
top5 = self.storage.pick_top5("field1")
assert top5 == 5
# Test when field does not exist in the storage
with self.assertRaises(KeyError):
self.storage.pick_top5("field2")
def test_pick_bottom5(self):
# Test when field exists in the storage
self.storage.insert_list("field1", [1, 2, 3, 4, 5])
bottom5 = self.storage.pick_bottom5("field1")
assert bottom5 == 1
# Test when field does not exist in the storage
with self.assertRaises(KeyError):
self.storage.pick_bottom5("field2")
def test_huge_amount_of_data(self):
# Test when inserting a large amount of data
self.storage.insert_list("field1", list(randrange(100) for _ in range(100)))
avg = self.storage.pick_avg("field1")
print(avg)
top5 = self.storage.pick_top5("field1")
print(top5)
bottom5 = self.storage.pick_bottom5("field1")
print(bottom5)
if __name__ == "__main__":
unittest.main()