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