Total Complexity | 18 |
Total Lines | 145 |
Duplicated Lines | 27.59 % |
Changes | 0 |
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 | """Tests for MetaStalk. |
||
2 | """ |
||
3 | import unittest |
||
4 | from argparse import Namespace |
||
5 | import os |
||
6 | from distutils.spawn import find_executable |
||
7 | |||
8 | from MetaStalk import main |
||
9 | |||
10 | |||
11 | def check_orca() -> bool: |
||
12 | """checks to see if orca is available |
||
13 | |||
14 | Returns: |
||
15 | bool: If orca executable is found |
||
16 | """ |
||
17 | return find_executable("orca") is not None |
||
18 | |||
19 | |||
20 | class MetaStalkTests(unittest.TestCase): |
||
21 | """MetStalkTests. |
||
22 | |||
23 | Test Suite for MetaStalk |
||
24 | |||
25 | """ |
||
26 | def __init__(self, *args, **kwargs): |
||
27 | super(MetaStalkTests, self).__init__(*args, **kwargs) |
||
28 | self.filenames = ["Focal", "GPS", "Manufacturer", "Model", "Producer", "Stats", "Timestamp"] |
||
29 | |||
30 | def test_empty_path(self): |
||
31 | """Shows result for no path input.""" |
||
32 | with self.assertRaises(FileNotFoundError): |
||
33 | main.start() |
||
34 | |||
35 | def test_directory(self): |
||
36 | """Results for a directory. |
||
37 | """ |
||
38 | arguments = Namespace( |
||
39 | files=['./ExamplePhotos/'], |
||
40 | alphabetic=False, |
||
41 | loglevel=30, |
||
42 | test=True, |
||
43 | export_only=False, |
||
44 | no_open=True, |
||
45 | export=None) |
||
46 | metastalk = main.MetaStalk() |
||
47 | self.assertEqual(metastalk.run(arguments), None) |
||
48 | |||
49 | def test_files(self): |
||
50 | """Results for files.""" |
||
51 | arguments = Namespace( |
||
52 | files=['./ExamplePhotos/22-canon_tags.jpg', './ExamplePhotos/32-lens_data.jpeg'], |
||
53 | alphabetic=True, |
||
54 | no_open=True, |
||
55 | export_only=False, |
||
56 | loglevel=30, |
||
57 | test=True, |
||
58 | export=None) |
||
59 | metastalk = main.MetaStalk() |
||
60 | self.assertEqual(metastalk.run(arguments), None) |
||
61 | |||
62 | def test_html_export(self): |
||
63 | """Test to see html files got exported.""" |
||
64 | arguments = Namespace( |
||
65 | files=['./ExamplePhotos/22-canon_tags.jpg', './ExamplePhotos/32-lens_data.jpeg'], |
||
66 | loglevel=30, |
||
67 | alphabetic=False, |
||
68 | no_open=True, |
||
69 | export_only=False, |
||
70 | test=True, |
||
71 | export="html", |
||
72 | output="metastalk_exports") |
||
73 | metastalk = main.MetaStalk() |
||
74 | metastalk.run(arguments) |
||
75 | test_passed = True |
||
76 | for required_file in self.filenames: |
||
77 | if not os.path.isfile(f"metastalk_exports/{required_file}.html"): |
||
78 | print(f"missing file {required_file}") |
||
79 | test_passed = False |
||
80 | self.assertTrue(test_passed) |
||
81 | |||
82 | def test_html_size(self): |
||
83 | """Test to see html that the offline html files are bigger""" |
||
84 | arguments = Namespace( |
||
85 | files=['./ExamplePhotos/22-canon_tags.jpg', './ExamplePhotos/32-lens_data.jpeg'], |
||
86 | loglevel=20, |
||
87 | alphabetic=False, |
||
88 | no_open=True, |
||
89 | export_only=False, |
||
90 | test=True, |
||
91 | export="html_offline", |
||
92 | output="metastalk_exports_offline") |
||
93 | metastalk = main.MetaStalk() |
||
94 | metastalk.run(arguments) |
||
95 | test_passed = True |
||
96 | for required_file in self.filenames: |
||
97 | file_size_normal = os.stat(f"metastalk_exports/{required_file}.html").st_size |
||
98 | file_size_offline = os.stat(f"metastalk_exports_offline/{required_file}.html").st_size |
||
99 | if file_size_normal > file_size_offline: |
||
100 | print(f"Wrong File Size for: {required_file}") |
||
101 | test_passed = False |
||
102 | self.assertTrue(test_passed) |
||
103 | |||
104 | View Code Duplication | @unittest.skipUnless(check_orca(), "Test not needed if orca executable is missing") |
|
1 ignored issue
–
show
|
|||
105 | def test_orca_export(self): |
||
106 | """Test for export fail.""" |
||
107 | test_passed = True |
||
108 | arguments = Namespace( |
||
109 | files=['./ExamplePhotos/'], |
||
110 | loglevel=30, |
||
111 | test=True, |
||
112 | alphabetic=False, |
||
113 | export_only=False, |
||
114 | no_open=True, |
||
115 | export="pdf", |
||
116 | output="metastalk_exports") |
||
117 | metastalk = main.MetaStalk() |
||
118 | metastalk.run(arguments) |
||
119 | for required_file in self.filenames: |
||
120 | if not os.path.isfile(f"metastalk_exports/{required_file}.pdf"): |
||
121 | print(f"missing file {required_file}") |
||
122 | test_passed = False |
||
123 | self.assertTrue(test_passed) |
||
124 | |||
125 | View Code Duplication | @unittest.skipUnless(check_orca(), "Test not needed if orca executable is missing") |
|
1 ignored issue
–
show
|
|||
126 | def test_export_only(self): |
||
127 | """Test to show that only export option works""" |
||
128 | arguments = Namespace( |
||
129 | files=['./ExamplePhotos/32-lens_data.jpeg'], |
||
130 | loglevel=30, |
||
131 | alphabetic=False, |
||
132 | no_open=True, |
||
133 | export_only=True, |
||
134 | test=True, |
||
135 | export="png", |
||
136 | output="metastalk_exports") |
||
137 | metastalk = main.MetaStalk() |
||
138 | metastalk.run(arguments) |
||
139 | test_passed = True |
||
140 | for required_file in self.filenames: |
||
141 | if not os.path.isfile(f"metastalk_exports/{required_file}.png"): |
||
142 | print(f"missing file {required_file}") |
||
143 | test_passed = False |
||
144 | self.assertTrue(test_passed) |
||
145 |