Total Complexity | 46 |
Total Lines | 256 |
Duplicated Lines | 39.84 % |
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:
Complex classes like elodie.tests.plugins_test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from __future__ import absolute_import |
||
2 | # Project imports |
||
3 | import mock |
||
4 | import os |
||
5 | import sys |
||
6 | from tempfile import gettempdir |
||
7 | |||
8 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
||
9 | |||
10 | from . import helper |
||
11 | from elodie.config import load_config |
||
12 | from elodie.plugins.plugins import Plugins, PluginBase, PluginDb |
||
13 | |||
14 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir()) |
||
15 | def test_load_plugins_unset_backwards_compat(): |
||
16 | with open('%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir(), 'w') as f: |
||
17 | f.write(""" |
||
18 | """) |
||
19 | if hasattr(load_config, 'config'): |
||
20 | del load_config.config |
||
21 | |||
22 | plugins = Plugins() |
||
23 | plugins.load() |
||
24 | |||
25 | if hasattr(load_config, 'config'): |
||
26 | del load_config.config |
||
27 | |||
28 | assert plugins.plugins == [], plugins.plugins |
||
29 | |||
30 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-exists-not-set' % gettempdir()) |
||
31 | def test_load_plugins_exists_not_set(): |
||
32 | with open('%s/config.ini-load-plugins-exists-not-set' % gettempdir(), 'w') as f: |
||
33 | f.write(""" |
||
34 | [Plugins] |
||
35 | """) |
||
36 | if hasattr(load_config, 'config'): |
||
37 | del load_config.config |
||
38 | |||
39 | plugins = Plugins() |
||
40 | plugins.load() |
||
41 | |||
42 | if hasattr(load_config, 'config'): |
||
43 | del load_config.config |
||
44 | |||
45 | assert plugins.plugins == [], plugins.plugins |
||
46 | |||
47 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one' % gettempdir()) |
|
|
|||
48 | def test_load_plugins_one(): |
||
49 | with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
||
50 | f.write(""" |
||
51 | [Plugins] |
||
52 | plugins=Dummy |
||
53 | """) |
||
54 | if hasattr(load_config, 'config'): |
||
55 | del load_config.config |
||
56 | |||
57 | plugins = Plugins() |
||
58 | plugins.load() |
||
59 | |||
60 | if hasattr(load_config, 'config'): |
||
61 | del load_config.config |
||
62 | |||
63 | assert plugins.plugins == ['Dummy'], plugins.plugins |
||
64 | assert len(plugins.classes) == 1, len(plugins.classes) |
||
65 | |||
66 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one-with-invalid' % gettempdir()) |
|
67 | def test_load_plugins_one_with_invalid(): |
||
68 | with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
||
69 | f.write(""" |
||
70 | [Plugins] |
||
71 | plugins=DNE |
||
72 | """) |
||
73 | if hasattr(load_config, 'config'): |
||
74 | del load_config.config |
||
75 | |||
76 | plugins = Plugins() |
||
77 | plugins.load() |
||
78 | |||
79 | if hasattr(load_config, 'config'): |
||
80 | del load_config.config |
||
81 | |||
82 | assert plugins.plugins == [], plugins.plugins |
||
83 | assert len(plugins.classes) == 0, len(plugins.classes) |
||
84 | |||
85 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many' % gettempdir()) |
||
86 | def test_load_plugins_many(): |
||
87 | with open('%s/config.ini-load-plugins-many' % gettempdir(), 'w') as f: |
||
88 | f.write(""" |
||
89 | [Plugins] |
||
90 | plugins=ThrowError,Dummy |
||
91 | """) |
||
92 | if hasattr(load_config, 'config'): |
||
93 | del load_config.config |
||
94 | |||
95 | plugins = Plugins() |
||
96 | plugins.load() |
||
97 | |||
98 | if hasattr(load_config, 'config'): |
||
99 | del load_config.config |
||
100 | |||
101 | assert plugins.plugins == ['ThrowError','Dummy'], plugins.plugins |
||
102 | assert plugins.classes['ThrowError'].__name__ == 'ThrowError', plugins.classes['ThrowError'].__name__ |
||
103 | assert plugins.classes['Dummy'].__name__ == 'Dummy', plugins.classes['Dummy'].__name__ |
||
104 | assert len(plugins.classes) == 2, len(plugins.classes) |
||
105 | |||
106 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many-with-invalid' % gettempdir()) |
||
107 | def test_load_plugins_set_many_with_invalid(): |
||
108 | with open('%s/config.ini-load-plugins-many-with-invalid' % gettempdir(), 'w') as f: |
||
109 | f.write(""" |
||
110 | [Plugins] |
||
111 | plugins=ThrowError,Dummy,DNE |
||
112 | """) |
||
113 | if hasattr(load_config, 'config'): |
||
114 | del load_config.config |
||
115 | |||
116 | plugins = Plugins() |
||
117 | plugins.load() |
||
118 | |||
119 | if hasattr(load_config, 'config'): |
||
120 | del load_config.config |
||
121 | |||
122 | assert plugins.plugins == ['ThrowError','Dummy'], plugins.plugins |
||
123 | |||
124 | @mock.patch('elodie.config.config_file', '%s/config.ini-run-before' % gettempdir()) |
||
125 | def test_run_before(): |
||
126 | with open('%s/config.ini-run-before' % gettempdir(), 'w') as f: |
||
127 | f.write(""" |
||
128 | [Plugins] |
||
129 | plugins=Dummy |
||
130 | """) |
||
131 | if hasattr(load_config, 'config'): |
||
132 | del load_config.config |
||
133 | |||
134 | plugins = Plugins() |
||
135 | plugins.load() |
||
136 | before_ran_1 = plugins.classes['Dummy'].before_ran |
||
137 | plugins.run_all_before('', '') |
||
138 | before_ran_2 = plugins.classes['Dummy'].before_ran |
||
139 | |||
140 | if hasattr(load_config, 'config'): |
||
141 | del load_config.config |
||
142 | |||
143 | assert before_ran_1 == False, before_ran_1 |
||
144 | assert before_ran_2 == True, before_ran_2 |
||
145 | |||
146 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-error' % gettempdir()) |
|
147 | def test_throw_error(): |
||
148 | with open('%s/config.ini-throw-error' % gettempdir(), 'w') as f: |
||
149 | f.write(""" |
||
150 | [Plugins] |
||
151 | plugins=ThrowError |
||
152 | """) |
||
153 | if hasattr(load_config, 'config'): |
||
154 | del load_config.config |
||
155 | |||
156 | plugins = Plugins() |
||
157 | plugins.load() |
||
158 | status_after = plugins.run_all_after('', '', '', '') |
||
159 | status_batch = plugins.run_batch() |
||
160 | status_before = plugins.run_all_before('', '') |
||
161 | |||
162 | if hasattr(load_config, 'config'): |
||
163 | del load_config.config |
||
164 | |||
165 | assert status_after == False, status_after |
||
166 | assert status_batch == False, status_batch |
||
167 | assert status_before == False, status_before |
||
168 | |||
169 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-error-one-of-many' % gettempdir()) |
|
170 | def test_throw_error_one_of_many(): |
||
171 | with open('%s/config.ini-throw-error-one-of-many' % gettempdir(), 'w') as f: |
||
172 | f.write(""" |
||
173 | [Plugins] |
||
174 | plugins=Dummy,ThrowError |
||
175 | """) |
||
176 | if hasattr(load_config, 'config'): |
||
177 | del load_config.config |
||
178 | |||
179 | plugins = Plugins() |
||
180 | plugins.load() |
||
181 | status_after = plugins.run_all_after('', '', '', '') |
||
182 | status_batch = plugins.run_batch() |
||
183 | status_before = plugins.run_all_before('', '') |
||
184 | |||
185 | if hasattr(load_config, 'config'): |
||
186 | del load_config.config |
||
187 | |||
188 | assert status_after == False, status_after |
||
189 | assert status_batch == False, status_batch |
||
190 | assert status_before == False, status_before |
||
191 | |||
192 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-runtime-error' % gettempdir()) |
|
193 | def test_throw_error_runtime_error(): |
||
194 | with open('%s/config.ini-throw-runtime-error' % gettempdir(), 'w') as f: |
||
195 | f.write(""" |
||
196 | [Plugins] |
||
197 | plugins=RuntimeError |
||
198 | """) |
||
199 | if hasattr(load_config, 'config'): |
||
200 | del load_config.config |
||
201 | |||
202 | plugins = Plugins() |
||
203 | plugins.load() |
||
204 | status_after = plugins.run_all_after('', '', '', '') |
||
205 | status_batch = plugins.run_batch() |
||
206 | status_before = plugins.run_all_before('', '') |
||
207 | |||
208 | if hasattr(load_config, 'config'): |
||
209 | del load_config.config |
||
210 | |||
211 | assert status_after == True, status_after |
||
212 | assert status_batch == True, status_batch |
||
213 | assert status_before == True, status_before |
||
214 | |||
215 | def test_plugin_base_inherits_db(): |
||
216 | plugin_base = PluginBase() |
||
217 | assert hasattr(plugin_base.db, 'get') |
||
218 | assert hasattr(plugin_base.db, 'set') |
||
219 | assert hasattr(plugin_base.db, 'get_all') |
||
220 | assert hasattr(plugin_base.db, 'delete') |
||
221 | |||
222 | def test_db_initialize_file(): |
||
223 | db = PluginDb('foobar') |
||
224 | try: |
||
225 | os.remove(db.db_file) |
||
226 | except OSError: |
||
227 | pass |
||
228 | db = PluginDb('foobar') |
||
229 | |||
230 | def test_db_get_then_set_then_get_then_delete(): |
||
231 | db = PluginDb('foobar') |
||
232 | foo = db.get('foo') |
||
233 | assert foo is None, foo |
||
234 | db.set('foo', 'bar') |
||
235 | foo = db.get('foo') |
||
236 | assert foo == 'bar', foo |
||
237 | db.delete('foo') |
||
238 | foo = db.get('foo') |
||
239 | assert foo is None, foo |
||
240 | |||
241 | def test_db_get_all(): |
||
242 | # we initialize the db to get the file path to delete then reinitialize |
||
243 | db = PluginDb('foobar') |
||
244 | try: |
||
245 | os.remove(db.db_file) |
||
246 | except OSError: |
||
247 | pass |
||
248 | db = PluginDb('foobar') |
||
249 | db.set('a', '1') |
||
250 | db.set('b', '2') |
||
251 | db.set('c', '3') |
||
252 | db.set('d', '4') |
||
253 | all_rows = db.get_all() |
||
254 | |||
255 | assert all_rows == {'a': '1', 'b': '2', 'c': '3', 'd': '4'}, all_rows |
||
256 |