Passed
Pull Request — master (#319)
by Jaisen
02:25 queued 12s
created

elodie.tests.plugins_test.test_throw_error()   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nop 0
dl 18
loc 18
rs 9.8
c 0
b 0
f 0
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())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 = plugins.run_all_before('', '')
159
160
    if hasattr(load_config, 'config'):
161
        del load_config.config
162
163
    assert status == False, status
164
165 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-throw-error-one-of-many' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
166
def test_throw_error_one_of_many():
167
    with open('%s/config.ini-throw-error-one-of-many' % gettempdir(), 'w') as f:
168
        f.write("""
169
[Plugins]
170
plugins=Dummy,ThrowError
171
        """)
172
    if hasattr(load_config, 'config'):
173
        del load_config.config
174
175
    plugins = Plugins()
176
    plugins.load()
177
    status = plugins.run_all_before('', '')
178
179
    if hasattr(load_config, 'config'):
180
        del load_config.config
181
182
    assert status == False, status
183
184 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-throw-runtime-error' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
185
def test_throw_error_runtime_error():
186
    with open('%s/config.ini-throw-runtime-error' % gettempdir(), 'w') as f:
187
        f.write("""
188
[Plugins]
189
plugins=RuntimeError
190
        """)
191
    if hasattr(load_config, 'config'):
192
        del load_config.config
193
194
    plugins = Plugins()
195
    plugins.load()
196
    status = plugins.run_all_before('', '')
197
198
    if hasattr(load_config, 'config'):
199
        del load_config.config
200
201
    assert status == True, status
202
203
def test_plugin_base_inherits_db():
204
    plugin_base = PluginBase()
205
    assert hasattr(plugin_base.db, 'get')
206
    assert hasattr(plugin_base.db, 'set')
207
    assert hasattr(plugin_base.db, 'get_all')
208
    assert hasattr(plugin_base.db, 'delete')
209
210
def test_db_initialize_file():
211
    db = PluginDb('foobar')
212
    try:
213
        os.remove(db.db_file)
214
    except OSError:
215
        pass
216
    db = PluginDb('foobar')
217
218
def test_db_get_then_set_then_get_then_delete():
219
    db = PluginDb('foobar')
220
    foo = db.get('foo')
221
    assert foo is None, foo
222
    db.set('foo', 'bar')
223
    foo = db.get('foo')
224
    assert foo == 'bar', foo
225
    db.delete('foo')
226
    foo = db.get('foo')
227
    assert foo is None, foo
228
229
def test_db_get_all():
230
    # we initialize the db to get the file path to delete then reinitialize
231
    db = PluginDb('foobar')
232
    try:
233
        os.remove(db.db_file)
234
    except OSError:
235
        pass
236
    db = PluginDb('foobar')
237
    db.set('a', '1')
238
    db.set('b', '2')
239
    db.set('c', '3')
240
    db.set('d', '4')
241
    all_rows = db.get_all()
242
243
    assert all_rows == {'a': '1', 'b': '2', 'c': '3', 'd': '4'}, all_rows
244