tests.test_hash   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 164
Duplicated Lines 71.34 %

Importance

Changes 0
Metric Value
eloc 115
dl 117
loc 164
rs 9.52
c 0
b 0
f 0
wmc 36

28 Methods

Rating   Name   Duplication   Size   Complexity  
A TestHashFilesRecursive.test_xxh64() 3 3 1
A TestHashFilesRecursive.setUpClass() 4 4 1
A TestHashFilesRecursive.tearDownClass() 4 4 1
A TestHashFilesRecursive.test_xxh32() 3 3 1
A TestHashFilesRecursive.test_md5() 3 3 1
A TestHashString.test_blake2b() 3 3 1
A TestHashString.tearDownClass() 4 4 1
A TestHashFile.test_sha1() 3 3 1
A TestHashString.test_xxh64() 2 3 1
A TestHashFile.test_xxh32() 3 3 1
A TestHashString.setUpClass() 3 3 1
A TestHashString.test_md5() 3 3 1
A TestHashString.test_xxh32() 3 3 1
A TestHashFile.test_blake2s() 3 3 1
A TestHashFile.test_sha512() 3 3 1
A TestHashFile.test_shake_128() 3 3 1
A TestHashString.test_sha512() 3 3 1
A TestHashFile.test_xxh64() 2 3 1
A TestHashFile.test_blake2b() 3 3 1
A TestHashFile.tearDownClass() 4 4 1
A TestHashFile.setUpClass() 4 4 2
A TestHashString.test_shake_128() 3 3 1
A TestHashString.test_sha1() 3 3 1
A TestHashFile.test_md5() 3 3 1
A TestHashString.test_blake2s() 3 3 1
A TestHashString.test_sha256() 3 3 1
A TestHashFilesRecursive.test_sha256() 2 3 1
A TestHashFile.test_sha256() 3 3 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A hash_file() 0 3 2
A get_paths() 0 2 1
A hash_file_xxh64() 0 2 1
A hash_file_md5() 0 2 1
A hash_file_sha256() 0 2 1
A hash_file_xxh32() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import unittest
2
import os
3
from looptools import Timer
4
from tests import directory
5
from dirutility import Hash, DirPaths, PoolProcess
6
7
FILE = os.path.join(os.path.dirname(__file__), 'document.pdf')
8
DIRECTORY = directory
9
10
11
def get_paths():
12
    return DirPaths(DIRECTORY, full_paths=True, parallelize=False).walk()
13
14
15
def hash_file(path):
16
    with open(path, 'rb') as fp:
17
        return Hash(fp.read())
18
19
20
def hash_file_xxh64(path):
21
    return hash_file(path).xxh64()
22
23
24
def hash_file_xxh32(path):
25
    return hash_file(path).xxh32()
26
27
28
def hash_file_md5(path):
29
    return hash_file(path).md5()
30
31
32
def hash_file_sha256(path):
33
    return hash_file(path).sha256()
34
35
36 View Code Duplication
class TestHashFilesRecursive(unittest.TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
38
    @classmethod
39
    def setUpClass(cls):
40
        cls.paths = get_paths()
41
        print(len(cls.paths))
42
43
    @classmethod
44
    def tearDownClass(cls):
45
        print('\n\nRecursive File Hashing results\n' + '-' * 68)
46
        Timer().print_times('TestHashFile')
47
48
    @Timer.decorator_noprint
49
    def test_xxh64(self):
50
        return PoolProcess(hash_file_xxh64, self.paths).map_return()
51
52
    @Timer.decorator_noprint
53
    def test_xxh32(self):
54
        return PoolProcess(hash_file_xxh32, self.paths).map_return()
55
56
    @Timer.decorator_noprint
57
    def test_md5(self):
58
        return PoolProcess(hash_file_md5, self.paths).map_return()
59
60
    @Timer.decorator_noprint
61
    def test_sha256(self):
62
        return PoolProcess(hash_file_sha256, self.paths).map_return()
63
64
65 View Code Duplication
class TestHashFile(unittest.TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
67
    @classmethod
68
    def setUpClass(cls):
69
        with open(FILE, 'rb') as fp:
70
            cls.hash = Hash(fp.read())
71
72
    @classmethod
73
    def tearDownClass(cls):
74
        print('\n\nFile Hashing results\n' + '-' * 68)
75
        Timer().print_times('TestHashFile')
76
77
    @Timer.decorator_noprint
78
    def test_md5(self):
79
        self.hash.md5()
80
81
    @Timer.decorator_noprint
82
    def test_sha1(self):
83
        self.hash.sha1()
84
85
    @Timer.decorator_noprint
86
    def test_sha256(self):
87
        self.hash.sha256()
88
89
    @Timer.decorator_noprint
90
    def test_sha512(self):
91
        self.hash.sha512()
92
93
    @Timer.decorator_noprint
94
    def test_blake2b(self):
95
        self.hash.blake2b()
96
97
    @Timer.decorator_noprint
98
    def test_blake2s(self):
99
        self.hash.blake2s()
100
101
    @Timer.decorator_noprint
102
    def test_shake_128(self):
103
        self.hash.shake_128()
104
105
    @Timer.decorator_noprint
106
    def test_xxh32(self):
107
        self.hash.xxh32()
108
109
    @Timer.decorator_noprint
110
    def test_xxh64(self):
111
        self.hash.xxh64()
112
113
114 View Code Duplication
class TestHashString(unittest.TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
115
116
    @classmethod
117
    def setUpClass(cls):
118
        cls.hash = Hash(b'Nobody inspects the spammish repetition')
119
120
    @classmethod
121
    def tearDownClass(cls):
122
        print('\n\nString Hashing results\n' + '-' * 68)
123
        Timer().print_times('TestHashString')
124
125
    @Timer.decorator_noprint
126
    def test_md5(self):
127
        self.hash.md5()
128
129
    @Timer.decorator_noprint
130
    def test_sha1(self):
131
        self.hash.sha1()
132
133
    @Timer.decorator_noprint
134
    def test_sha256(self):
135
        self.hash.sha256()
136
137
    @Timer.decorator_noprint
138
    def test_sha512(self):
139
        self.hash.sha512()
140
141
    @Timer.decorator_noprint
142
    def test_blake2b(self):
143
        self.hash.blake2b()
144
145
    @Timer.decorator_noprint
146
    def test_blake2s(self):
147
        self.hash.blake2s()
148
149
    @Timer.decorator_noprint
150
    def test_shake_128(self):
151
        self.hash.shake_128()
152
153
    @Timer.decorator_noprint
154
    def test_xxh32(self):
155
        self.hash.xxh32()
156
157
    @Timer.decorator_noprint
158
    def test_xxh64(self):
159
        self.hash.xxh64()
160
161
162
if __name__ == '__main__':
163
    unittest.main()
164