-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
84 lines (56 loc) · 2.06 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
''' Usage: python tests.py'''
import unittest
from file_handling import CreateFile
from hashlib import md5
import os
from logger import logger
class hashingTests(unittest.TestCase):
'''Tests the hashing process.'''
def setUp(self):
''' Runs before the start of every test in this class.'''
cwd = os.getcwd()
self.temp_file_path = os.path.join(cwd, 'temp_test_file')
self.example_file_name = os.path.join(cwd, 'hashing_test_file')
self.example_file = open(self.example_file_name, 'wb')
self.example_file.write(b'example'*20000)
self.example_file.close()
self.example_file = open(self.example_file_name, 'rb')
def hash_by_chunk(self, file):
'''
Expects full file path like 'file'-argument.
Writes it chunk by chunk in the new file,
creates hash of the file in parallel.
'''
file = open(file, 'rb')
hash_code = md5()
temp_file = open(self.temp_file_path, 'wb')
while True:
chunk = file.read(2000)
if not chunk:
break
hash_code.update(chunk)
temp_file.write(chunk)
file.close()
temp_file.close()
return hash_code.hexdigest()
def test_chunk_by_chunk_hashing(self):
'''Tests the result of 'chunk by chunk' hashing.'''
hashing_chunk_by_chunk = self.hash_by_chunk(self.example_file_name)
hashing_entire_file = md5(self.example_file.read()).hexdigest()
logger.info(
'Hash chunk by chunk:\n'+
str(hashing_chunk_by_chunk)+
'\nHash of the entire file:\n'+
str(hashing_entire_file)
)
self.assertEqual(
hashing_chunk_by_chunk,
hashing_entire_file,
)
def tearDown(self):
''' Runs after every test in this class.'''
for file in [self.temp_file_path, self.example_file_name,]:
os.remove(file)
self.example_file.close()
if __name__ == '__main__':
unittest.main()