-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_diskbackedlist.py
69 lines (52 loc) · 2.16 KB
/
test_diskbackedlist.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
import pickle
import unittest
import os
from tempfile import TemporaryDirectory
from DiskBackedList import DiskBackedList
class DiskBackedListTest(unittest.TestCase):
def setUp(self):
self.temp_dir = TemporaryDirectory()
self.filename = os.path.join(self.temp_dir.name, 'data.pkl')
self.list = DiskBackedList(self.filename)
def tearDown(self):
self.temp_dir.cleanup()
def test_append_and_retrieve(self):
self.list.append('value1')
self.list.append('value2')
self.assertEqual(self.list[0], 'value1')
self.assertEqual(self.list[1], 'value2')
def test_extend_and_retrieve(self):
self.list.extend(['value1', 'value2'])
self.assertEqual(self.list[0], 'value1')
self.assertEqual(self.list[1], 'value2')
def test_delete(self):
self.list.extend(['value1', 'value2'])
del self.list[0]
self.assertEqual(len(self.list), 1)
self.assertEqual(self.list[0], 'value2')
def test_load_from_disk(self):
self.list.extend(['value1', 'value2'])
self.list.store_to_disk()
new_list = DiskBackedList(self.filename)
new_list.load_from_disk()
self.assertEqual(len(new_list), 2)
self.assertEqual(new_list[0], 'value1')
self.assertEqual(new_list[1], 'value2')
def test_store_to_disk_every_100_keys(self):
for i in range(1000):
self.list.append(f'value{i}')
if (i + 1) % 100 == 99:
file_exists = os.path.isfile(self.filename)
if file_exists:
with open(self.filename, 'rb') as file:
dumped_list = pickle.load(file)
self.assertNotEquals(len(dumped_list), i + 1)
self.assertEqual(len(dumped_list), i - 98)
if (i + 1) % 100 == 0:
file_exists = os.path.isfile(self.filename)
self.assertTrue(file_exists)
with open(self.filename, 'rb') as file:
dumped_list = pickle.load(file)
self.assertEqual(len(dumped_list), i + 1)
if __name__ == '__main__':
unittest.main()