File: tests_core.py

package info (click to toggle)
paperwork 2.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 166,660 kB
  • sloc: python: 44,775; makefile: 992; sh: 625; xml: 135
file content (380 lines) | stat: -rw-r--r-- 12,843 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import unittest
import unittest.mock

import openpaperwork_core


class TestLoading(unittest.TestCase):
    @unittest.mock.patch("importlib.import_module")
    def test_simple_loading(self, import_module):
        class TestModule(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called = False
                    self.test_method_called = False

                def init(self, core):
                    self.init_called = True

                def test_method(self):
                    self.test_method_called = True

        core = openpaperwork_core.Core(auto_load_dependencies=True)

        import_module.return_value = TestModule()
        core.load('whatever_module')
        import_module.assert_called_once_with('whatever_module')

        core.init()
        self.assertTrue(core.get_by_name('whatever_module').init_called)

        core.call_all('test_method')
        self.assertTrue(core.get_by_name('whatever_module').test_method_called)


class TestInit(unittest.TestCase):
    @unittest.mock.patch("importlib.import_module")
    def test_init_order(self, import_module):
        global g_idx
        g_idx = 0

        class TestModuleA(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called_a = -1

                def get_interfaces(self):
                    return ['module_a']

                def init(self, core):
                    global g_idx
                    self.init_called_a = g_idx
                    g_idx += 1

        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called_b = -1

                def get_interfaces(self):
                    return ['module_b']

                def get_deps(self):
                    return [
                        {
                            'interface': 'module_a',
                            'defaults': ['module_a'],
                        }
                    ]

                def init(self, core):
                    global g_idx
                    self.init_called_b = g_idx
                    g_idx += 1

        class TestModuleC(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called_c = -1

                def get_deps(self):
                    return [
                        {
                            'interface': 'module_b',
                            'defaults': ['module_b'],
                            'expected_already_satisfied': False,
                        }
                    ]

                def init(self, core):
                    global g_idx
                    self.init_called_c = g_idx
                    g_idx += 1

        core = openpaperwork_core.Core()

        import_module.return_value = TestModuleA()
        core.load('module_a')
        import_module.assert_called_once_with('module_a')

        import_module.reset_mock()
        import_module.return_value = TestModuleC()
        core.load('module_c')
        import_module.assert_called_once_with('module_c')

        import_module.reset_mock()
        import_module.return_value = TestModuleB()
        core.init()  # will load 'module_b' because of dependencies
        import_module.assert_called_once_with('module_b')

        self.assertEqual(core.get_by_name('module_a').init_called_a, 0)
        self.assertEqual(core.get_by_name('module_b').init_called_b, 1)
        self.assertEqual(core.get_by_name('module_c').init_called_c, 2)


class TestCall(unittest.TestCase):
    @unittest.mock.patch("importlib.import_module")
    def test_default_interface(self, import_module):
        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called_b = False
                    self.test_method_called_b = False

                def get_interfaces(self):
                    return ["test_interface"]

                def init(self, core):
                    self.init_called_b = True

                def test_method(self):
                    self.test_method_called_b = True

        class TestModuleC(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called_c = False
                    self.test_method_called_c = False

                def get_deps(self):
                    return [
                        {
                            'interface': 'test_interface',
                            'defaults': ['module_a', 'module_b']
                        },
                    ]

                def init(self, core):
                    self.init_called_c = True

                def test_method(self):
                    self.test_method_called_c = True

        core = openpaperwork_core.Core(auto_load_dependencies=True)

        import_module.return_value = TestModuleC()
        core.load('module_c')
        import_module.assert_called_once_with('module_c')

        import_module.reset_mock()
        import_module.return_value = TestModuleB()
        core.load('module_b')
        import_module.assert_called_once_with('module_b')

        import_module.reset_mock()
        # interface already satisfied --> won't load 'module_a'
        core.init()

        self.assertTrue(core.get_by_name('module_b').init_called_b)
        self.assertTrue(core.get_by_name('module_c').init_called_c)

        core.call_all('test_method')
        self.assertTrue(core.get_by_name('module_b').test_method_called_b)
        self.assertTrue(core.get_by_name('module_c').test_method_called_c)

    @unittest.mock.patch("importlib.import_module")
    def test_call_success_priority(self, import_module):
        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                PRIORITY = 33

                def __init__(self):
                    self.test_method_called_b = False

                def test_method(self):
                    self.test_method_called_b = True
                    return None

        class TestModuleC(object):
            class Plugin(openpaperwork_core.PluginBase):
                PRIORITY = 22

                def __init__(self):
                    self.test_method_called_c = False

                def test_method(self):
                    self.test_method_called_c = True
                    return "value"

        class TestModuleD(object):
            class Plugin(openpaperwork_core.PluginBase):
                PRIORITY = 11

                def __init__(self):
                    self.test_method_called_d = False

                def test_method(self):
                    self.test_method_called_d = True
                    return None

        core = openpaperwork_core.Core(auto_load_dependencies=True)

        import_module.return_value = TestModuleB()
        core.load('module_b')
        import_module.assert_called_once_with('module_b')

        import_module.reset_mock()
        import_module.return_value = TestModuleC()
        core.load('module_c')
        import_module.assert_called_once_with('module_c')

        import_module.reset_mock()
        import_module.return_value = TestModuleD()
        core.load('module_d')
        import_module.assert_called_once_with('module_d')

        import_module.reset_mock()
        # interface already satisfied --> won't load 'module_a'
        core.init()

        r = core.call_success('test_method')
        self.assertEqual(r, "value")
        self.assertTrue(core.get_by_name('module_b').test_method_called_b)
        self.assertTrue(core.get_by_name('module_c').test_method_called_c)
        self.assertFalse(core.get_by_name('module_d').test_method_called_d)

    @unittest.mock.patch("importlib.import_module")
    def test_priority(self, import_module):
        class TestModuleA(object):
            class Plugin(openpaperwork_core.PluginBase):
                PRIORITY = 22

                def test_method(self):
                    return "A"

        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                PRIORITY = 33

                def test_method(self):
                    return "B"

        core = openpaperwork_core.Core(auto_load_dependencies=True)

        import_module.return_value = TestModuleA()
        core.load('module_a')
        import_module.assert_called_once_with('module_a')

        import_module.reset_mock()
        import_module.return_value = TestModuleB()
        core.load('module_b')
        import_module.assert_called_once_with('module_b')

        import_module.reset_mock()
        core.init()

        r = core.call_success('test_method')
        self.assertEqual(r, "B")


class TestDependencies(unittest.TestCase):
    @unittest.mock.patch("importlib.import_module")
    def test_default_interface(self, import_module):
        class TestModuleA(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called = False
                    self.test_method_called = False

                def get_interfaces(self):
                    return [
                        "test_interface",
                        "some_interface",
                    ]

                def init(self, core):
                    self.init_called = True

                def test_method(self):
                    self.test_method_called = True

        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(self):
                    self.init_called = False

                def get_interfaces(self):
                    return ['some_interface']

                def get_deps(self):
                    return [
                        {
                            'interface': 'test_interface',
                            'defaults': ['module_a'],
                        }
                    ]

                def init(self, core):
                    self.init_called = True

        core = openpaperwork_core.Core(auto_load_dependencies=True)

        import_module.return_value = TestModuleB()
        core.load('module_b')
        import_module.assert_called_once_with('module_b')

        import_module.reset_mock()
        import_module.return_value = TestModuleA()
        core.init()  # will load 'module_a' because of dependencies
        import_module.assert_called_once_with('module_a')
        self.assertTrue(core.get_by_name('module_a').init_called)
        self.assertTrue(core.get_by_name('module_b').init_called)

        core.call_all('test_method')
        self.assertTrue(core.get_by_name('module_a').test_method_called)

        self.assertEqual(
            core.get_by_interface('some_interface'),
            [
                core.get_by_name('module_b'),
                core.get_by_name('module_a'),
            ]
        )
        self.assertEqual(core.get_by_interface('unknown_interface'), [])

    @unittest.mock.patch("importlib.import_module")
    def test_no_init_if_dropped(self, import_module):
        self.init_called = False

        class TestModuleA(object):
            class Plugin(openpaperwork_core.PluginBase):
                def get_interfaces(s):
                    return [
                        "test_interface",
                        "some_interface",
                    ]

                def init(s, core):
                    self.init_called = True

        class TestModuleB(object):
            class Plugin(openpaperwork_core.PluginBase):
                def __init__(s):
                    s.init_called = False

                def get_interfaces(s):
                    return ['some_interface']

                def get_deps(s):
                    return [
                        {
                            'interface': 'test_interface',
                            'defaults': ['module_a'],
                        }
                    ]

                def init(s, core):
                    self.init_called = True

        core = openpaperwork_core.Core(auto_load_dependencies=False)

        import_module.return_value = TestModuleB()
        core.load('module_b')
        import_module.assert_called_once_with('module_b')

        import_module.reset_mock()
        import_module.return_value = TestModuleA()
        core.init()  # will NOT load 'module_a' and will drop 'module_b'
        self.assertFalse(self.init_called)
        self.assertRaises(KeyError, core.get_by_name, 'module_a')
        self.assertRaises(KeyError, core.get_by_name, 'module_b')