-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
652 lines (570 loc) · 26.8 KB
/
test.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""
Module for testing qiwis module.
"""
import collections.abc
import dataclasses
import sys
import json
import unittest
from unittest import mock
from types import MappingProxyType
from typing import Any, Optional, Mapping, Iterable
from PyQt5.QtCore import QObject
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget
import qiwis
APP_INFOS = {
"app1": qiwis.AppInfo(
module="module1",
cls="cls1",
path="path1",
pos="left",
channel=["ch1", "ch2"],
args={"arg1": "value1"}
),
"app2": qiwis.AppInfo(
module="module2",
cls="cls2"
)
}
APP_DICTS = {
"app1": {
"module": "module1",
"cls": "cls1",
"path": "path1",
"pos": "left",
"channel": ["ch1", "ch2"],
"args": {"arg1": "value1"}
},
"app2": {
"module": "module2",
"cls": "cls2"
}
}
APP_JSONS = {
"app1": ('{"module": "module1", "cls": "cls1", "path": "path1", "pos": "left", '
'"channel": ["ch1", "ch2"], "trust": false, "args": {"arg1": "value1"}}'),
"app2": ('{"module": "module2", "cls": "cls2", "path": ".", "pos": "", '
'"channel": [], "trust": false, "args": null}'),
"app2_default": '{"module": "module2", "cls": "cls2"}'
}
qapp = QApplication(sys.argv)
class QiwisTestWithApps(unittest.TestCase):
"""Unit test for Qiwis class with creating apps."""
def setUp(self):
self.import_module_patcher = mock.patch("importlib.import_module")
self.mocked_import_module = self.import_module_patcher.start()
for appInfo in APP_INFOS.values():
app = mock.MagicMock()
app.cls = appInfo.cls
app.frames.return_value = (("title", QWidget()),)
cls = mock.MagicMock(return_value=app)
setattr(self.mocked_import_module.return_value, appInfo.cls, cls)
self.channels = set()
for appInfo in APP_INFOS.values():
self.channels.update(appInfo.channel)
self.qiwis = qiwis.Qiwis(APP_INFOS)
def doCleanups(self):
self.import_module_patcher.stop()
def test_init(self):
self.assertEqual(self.qiwis.appInfos, APP_INFOS)
for name, info in APP_INFOS.items():
self.mocked_import_module.assert_any_call(info.module)
self.assertEqual(self.qiwis._apps[name].cls, info.cls)
self.assertIn(name, self.qiwis._wrapperWidgets)
for channel in self.channels:
self.assertIn(channel, self.qiwis._subscribers)
def test_app_names(self):
appNamesSet = set(self.qiwis.appNames())
self.assertEqual(appNamesSet, set(APP_INFOS))
def test_create_app(self):
app = mock.MagicMock()
app.cls = "cls3"
app.frames.return_value = (("title", QWidget()),)
cls = mock.MagicMock(return_value=app)
setattr(self.mocked_import_module.return_value, "cls3", cls)
self.qiwis.createApp(
"app3",
qiwis.AppInfo(module="module3", cls="cls3", channel=["ch1"])
)
self.mocked_import_module.assert_called_with("module3")
self.assertEqual(self.qiwis._apps["app3"].cls, "cls3")
self.assertIn("app3", self.qiwis._wrapperWidgets)
self.assertIn("app3", self.qiwis._subscribers["ch1"])
def test_create_existing_app(self):
"""Tests for the case where trying to create an existing app."""
orgApp = self.qiwis._apps["app2"]
app = mock.MagicMock()
app.cls = "cls2"
app.frames.return_value = (("title", QWidget()),)
cls = mock.MagicMock(return_value=app)
setattr(self.mocked_import_module.return_value, "cls2", cls)
appInfo = qiwis.AppInfo(module="module2", cls="cls2")
with mock.patch.object(self.qiwis, "destroyApp") as mocked_destroy_app:
# The original app will not be replaced.
self.qiwis.createApp("app2", appInfo)
mocked_destroy_app.assert_not_called()
self.assertEqual(self.qiwis._apps["app2"], orgApp)
# The original app will be replaced.
self.qiwis.createApp("app2", appInfo, True)
mocked_destroy_app.assert_called_once_with("app2")
self.assertNotEqual(self.qiwis._apps["app2"], orgApp)
def test_destroy_app(self):
for name, info in APP_INFOS.items():
self.qiwis.destroyApp(name)
self.assertNotIn(name, self.qiwis._apps)
self.assertNotIn(name, self.qiwis._wrapperWidgets)
for channel in info.channel:
self.assertNotIn(name, self.qiwis._subscribers[channel])
def test_update_frames_inclusive(self):
"""Tests for the case where a new frame is added in the return of frames()."""
orgFramesSet = {wrapper.widget() for wrapper in self.qiwis._wrapperWidgets["app1"]}
newFramesSet = orgFramesSet | {QWidget()}
self.qiwis._apps["app1"].frames.return_value = tuple(("title", frame)
for frame in newFramesSet)
self.qiwis.updateFrames("app1")
finalFramesSet = {wrapper.widget() for wrapper in self.qiwis._wrapperWidgets["app1"]}
self.assertEqual(finalFramesSet, newFramesSet)
def test_update_frames_exclusive(self):
"""Tests for the case where a new frame replaced the return of frames()."""
orgFramesSet = {wrapper.widget() for wrapper in self.qiwis._wrapperWidgets["app1"]}
newFramesSet = {QWidget()}
self.qiwis._apps["app1"].frames.return_value = tuple(("title", frame)
for frame in newFramesSet)
self.qiwis.updateFrames("app1")
finalFramesSet = {wrapper.widget() for wrapper in self.qiwis._wrapperWidgets["app1"]}
self.assertFalse(finalFramesSet & orgFramesSet)
self.assertEqual(finalFramesSet, newFramesSet)
def test_channel_names(self):
channelNamesSet = set(self.qiwis.channelNames())
self.assertEqual(channelNamesSet, self.channels)
def test_subscriber_names(self):
for channel in self.channels:
subscriberNamesSet = set(self.qiwis.subscriberNames(channel))
self.assertEqual(
subscriberNamesSet,
{name for name, info in APP_INFOS.items() if channel in info.channel}
)
def test_subscribe(self):
self.assertNotIn("app1", self.qiwis._subscribers["ch3"])
self.qiwis.subscribe("app1", "ch3")
self.assertIn("app1", self.qiwis._subscribers["ch3"])
def test_subscribe_duplicate(self):
"""Tests for the case where trying to subscribe to a channel already subscribed to."""
self.qiwis.subscribe("app1", "ch3")
orgSubscribers = self.qiwis._subscribers["ch3"]
self.qiwis.subscribe("app1", "ch3") # Try to subscribe to the channel again.
self.assertEqual(self.qiwis._subscribers["ch3"], orgSubscribers)
def test_unsubcribe(self):
self.assertEqual(self.qiwis.unsubscribe("app1", "ch1"), True)
self.assertNotIn("app1", self.qiwis._subscribers["ch1"])
self.assertEqual(self.qiwis.unsubscribe("app2", "ch1"), False)
def test_broadcast(self):
for channelName in self.channels:
self.qiwis._broadcast(channelName, "test_msg")
for name, app in self.qiwis._apps.items():
self.assertEqual(len(APP_INFOS[name].channel), app.received.emit.call_count)
class QiwisTestWithoutApps(unittest.TestCase):
"""Unit test for Qiwis class without apps."""
def setUp(self):
self.qiwis = qiwis.Qiwis()
def help_qiwiscall(
self,
value: Any,
result_string: str,
error: Optional[Exception] = None,
dumps: Iterable = (),
):
"""Helper method for testing _qiwiscall().
Args:
value: The actual return value of the qiwiscall.
result_string: The qiwis.dumps()-ed string of the result object that should be
generated after the qiwiscall.
error: The Exception instance that should have occurred during the qiwiscall.
None if no exception is expected.
dumps: A sequence of return values of the mocked qiwis.dumps().
It will be given as side_effect. Moreover, the number of calls of
qiwis.dumps() should be the same as the lenght of the given iterable.
"""
msg = json.dumps({"call": "callForTest", "args": {}})
with mock.patch.multiple(self.qiwis, _handleQiwiscall=mock.DEFAULT, _apps=mock.DEFAULT):
if error is None:
self.qiwis._handleQiwiscall.return_value = value
else:
self.qiwis._handleQiwiscall.side_effect = error
with mock.patch("qiwis.dumps") as mocked_dumps:
mocked_dumps.side_effect = dumps
self.qiwis._qiwiscall(sender="sender", msg=msg)
self.assertEqual(len(mocked_dumps.mock_calls), len(dumps))
mocked_signal = self.qiwis._apps["sender"].qiwiscallReturned
mocked_signal.emit.assert_called_once_with(msg, result_string)
def test_qiwiscall_primitive(self):
"""The qiwiscall returns a primitive type value, which can be JSONified."""
value = [1.5, True, None, "abc"]
result_string = json.dumps({"done": True, "success": True, "value": value, "error": None})
dumps = (result_string,)
self.help_qiwiscall(value, result_string, dumps=dumps)
def test_qiwiscall_serializable(self):
"""The qiwiscall returns a Serializable type value."""
@dataclasses.dataclass
class ClassForTest(qiwis.Serializable):
a: str
value = ClassForTest(a="abc")
value_string = json.dumps({"a": "abc"})
result_string = json.dumps({
"done": True,
"success": True,
"value": value_string,
"error": None,
})
dumps = (value_string, result_string)
self.help_qiwiscall(value, result_string, dumps=dumps)
def test_qiwiscall_exception(self):
"""The qiwiscall raises an exception."""
class ExceptionForTest(Exception):
"""Temporary exception only for this test."""
error = ExceptionForTest("test")
result_string = json.dumps({
"done": True,
"success": False,
"value": None,
"error": repr(error),
})
dumps = (result_string,)
self.help_qiwiscall(None, result_string, error, dumps=dumps)
def test_parse_args_primitive(self):
def call_for_test(number: float, boolean: bool, string: str): # pylint: disable=unused-argument
"""A dummy function for testing, which has only primitive type arguments."""
args = {"number": 1.5, "boolean": True, "string": "abc"}
parsed_args = self.qiwis._parseArgs(call_for_test, args)
self.assertEqual(args, parsed_args)
def test_parse_args_serializable(self):
@dataclasses.dataclass
class ClassForTest(qiwis.Serializable):
number: float
boolean: bool
string: str
def call_for_test(arg1: ClassForTest, arg2: ClassForTest): # pylint: disable=unused-argument
"""A dummy function for testing, which has only Serializable type arguments."""
fields1 = {
"number": 1.5,
"boolean": True,
"string": "abc",
}
fields2 = {
"number": 0,
"boolean": False,
"string": "",
}
args = {"arg1": ClassForTest(**fields1), "arg2": ClassForTest(**fields2)}
json_args = {"arg1": json.dumps(fields1), "arg2": json.dumps(fields2)}
parsed_args = self.qiwis._parseArgs(call_for_test, json_args)
self.assertEqual(args, parsed_args)
@mock.patch("qiwis.loads")
@mock.patch("qiwis.QMessageBox.warning")
class HandleQiwiscallTest(unittest.TestCase):
"""Unit test for Qiwis._handleQiwiscall()."""
def setUp(self):
self.qiwis = qiwis.Qiwis()
def test_ok(self, mocked_warning, mocked_loads):
args = {"a": 123, "b": "ABC"}
info = qiwis.QiwiscallInfo(call="callForTest", args=args)
msg = json.dumps({"call": "callForTest", "args": args})
mocked_loads.return_value = info
mocked_warning.return_value = QMessageBox.Ok
app_infos = {"sender": qiwis.AppInfo(module="module", cls="cls")}
with mock.patch.multiple(self.qiwis, create=True, appInfos=app_infos,
callForTest=mock.DEFAULT, _parseArgs=mock.DEFAULT):
self.qiwis._parseArgs.return_value = args
self.qiwis._handleQiwiscall(sender="sender", msg=msg)
self.qiwis.callForTest.assert_called_once_with(**args)
self.qiwis._parseArgs.assert_called_once_with(self.qiwis.callForTest, args)
mocked_loads.assert_called_once()
mocked_warning.assert_called_once()
def test_cancel(self, mocked_warning, mocked_loads):
args = {"a": 123, "b": "ABC"}
info = qiwis.QiwiscallInfo(call="callForTest", args=args)
msg = json.dumps({"call": "callForTest", "args": args})
mocked_loads.return_value = info
mocked_warning.return_value = QMessageBox.Cancel
app_infos = {"sender": qiwis.AppInfo(module="module", cls="cls")}
with mock.patch.multiple(self.qiwis, create=True, appInfos=app_infos,
callForTest=mock.DEFAULT, _parseArgs=mock.DEFAULT):
self.qiwis._parseArgs.return_value = args
with self.assertRaises(RuntimeError):
self.qiwis._handleQiwiscall(sender="sender", msg=msg)
self.qiwis.callForTest.assert_not_called()
self.qiwis._parseArgs.assert_called_once_with(self.qiwis.callForTest, args)
mocked_loads.assert_called_once()
mocked_warning.assert_called_once()
def test_non_public(self, mocked_warning, mocked_loads):
args = {"a": 123, "b": "ABC"}
info = qiwis.QiwiscallInfo(call="_callForTest", args=args)
msg = json.dumps({"call": "_callForTest", "args": args})
mocked_loads.return_value = info
with mock.patch.multiple(self.qiwis, create=True,
_callForTest=mock.DEFAULT, _parseArgs=mock.DEFAULT):
with self.assertRaises(ValueError):
self.qiwis._handleQiwiscall(sender="sender", msg=msg)
self.qiwis._callForTest.assert_not_called()
self.qiwis._parseArgs.assert_not_called()
mocked_loads.assert_called_once()
mocked_warning.assert_not_called()
def test_not_existing_method(self, mocked_warning, mocked_loads):
args = {"a": 123, "b": "ABC"}
info = qiwis.QiwiscallInfo(call="callForTest", args=args)
msg = json.dumps({"call": "callForTest", "args": args})
mocked_loads.return_value = info
with mock.patch.multiple(self.qiwis, create=True, _parseArgs=mock.DEFAULT):
with self.assertRaises(AttributeError):
self.qiwis._handleQiwiscall(sender="sender", msg=msg)
self.qiwis._parseArgs.assert_not_called()
mocked_loads.assert_called_once()
mocked_warning.assert_not_called()
class BaseAppTest(unittest.TestCase):
"""Unit test for BaseApp class."""
def setUp(self):
self.app = qiwis.BaseApp("name")
def test_init(self):
self.assertEqual(self.app.name, "name")
def test_set_parent(self):
qiwis.BaseApp("name", QObject())
def test_constants_default(self):
self.assertFalse(self.app.constants._fields)
@mock.patch("qiwis.BaseApp._constants")
def test_constants(self, mocked_constants):
self.assertIs(self.app.constants, mocked_constants)
def test_frames(self):
self.assertIsInstance(self.app.frames(), collections.abc.Iterable)
def test_broadcast(self):
self.app.broadcastRequested = mock.MagicMock()
self.app.broadcast("ch1", "msg")
self.app.broadcastRequested.emit.assert_called_once_with("ch1", '"msg"')
def test_broadcast_exception(self):
self.app.broadcastRequested = mock.MagicMock()
self.app.broadcast("ch1", lambda: None)
self.app.broadcastRequested.emit.assert_not_called()
def test_received_message(self):
self.app.receivedSlot = mock.MagicMock()
self.app._receivedMessage("ch1", '"msg"')
self.app.receivedSlot.assert_called_once_with("ch1", "msg")
def test_received_message_exception(self):
self.app.receivedSlot = mock.MagicMock()
self.app._receivedMessage("ch1", '"msg1" "msg2"')
self.app.receivedSlot.assert_not_called()
def test_received_qiwiscall_result(self):
self.app.qiwiscall.update_result = mock.MagicMock()
self.app._receivedQiwiscallResult(
"request", '{"done": true, "success": true, "value": null, "error": null}'
)
self.app.qiwiscall.update_result.assert_called_once_with(
"request",
qiwis.QiwiscallResult(done=True, success=True)
)
def test_received_qiwiscall_result_exception(self):
self.app.qiwiscall.update_result = mock.MagicMock()
self.app._receivedQiwiscallResult(
"request", '{"done": "tr" "ue", "success": true, "value": null, "error": null}'
)
self.app.qiwiscall.update_result.assert_not_called()
class QiwiscallProxyTest(unittest.TestCase):
"""Unit test for QiwiscallProxy class."""
def setUp(self):
self.qiwiscall = qiwis.QiwiscallProxy(mock.MagicMock())
def help_proxy(self, msg: str, args: Mapping[str, Any], dumps: Iterable):
"""Helper method for testing proxy.
Args:
msg: The qiwiscall request message.
args: A keyword argument mapping for calling the proxy.
dumps: Expected return values of qiwis.dumps() during the proxied qiwiscall.
It will be given as side_effect. The number of calls should be the same as
the length of the given iterable.
"""
with mock.patch.object(self.qiwiscall, "results", {}):
with mock.patch("qiwis.dumps") as mocked_dumps:
mocked_dumps.side_effect = dumps
result = self.qiwiscall.callForTest(**args)
self.assertEqual(len(mocked_dumps.mock_calls), len(dumps))
self.qiwiscall.requested.emit.assert_called_once_with(msg)
self.assertIs(result, self.qiwiscall.results[msg])
self.assertEqual(result, qiwis.QiwiscallResult(done=False, success=False))
def test_proxy_primitive(self):
"""Tests a proxied qiwiscall with primitive type arguments."""
args = {"number": 1.5, "boolean": True, "string": "abc"}
msg = json.dumps({"call": "callForTest", "args": args})
dumps = (msg,)
self.help_proxy(msg, args, dumps)
def test_proxy_serializable(self):
"""Tests a proxied qiwiscall with Serializable type arguments."""
@dataclasses.dataclass
class ClassForTest(qiwis.Serializable):
number: float
boolean: bool
string: str
args = {
"arg1": ClassForTest(number=1.5, boolean=True, string="abc"),
"arg2": ClassForTest(number=0, boolean=False, string=""),
}
json_args = {
"arg1": json.dumps({"number": 1.5, "boolean": True, "string": "abc"}),
"arg2": json.dumps({"number": 0, "boolean": False, "string": ""}),
}
msg = json.dumps({"call": "callForTest", "args": json_args})
dumps = (json_args["arg1"], json_args["arg2"], msg)
self.help_proxy(msg, args, dumps)
def test_proxy_duplicate(self):
"""Tests a duplicate proxied qiwiscall.
The new one should be accepted and the previous one should be discarded.
"""
args = {"a": 123}
msg = json.dumps({"call": "callForTest", "args": args})
with mock.patch.object(self.qiwiscall, "results", {}):
with mock.patch("qiwis.dumps") as mocked_dumps:
mocked_dumps.side_effect = (msg, msg)
result1 = self.qiwiscall.callForTest(**args)
result2 = self.qiwiscall.callForTest(**args)
self.assertEqual(len(mocked_dumps.mock_calls), 2)
self.assertSequenceEqual(
self.qiwiscall.requested.emit.mock_calls,
(mock.call(msg), mock.call(msg)),
)
self.assertIs(result2, self.qiwiscall.results[msg])
self.assertEqual(result2, qiwis.QiwiscallResult(done=False, success=False))
self.assertEqual(result1, result2)
def test_update_result_success(self):
old_result = qiwis.QiwiscallResult(done=False, success=False)
new_result = qiwis.QiwiscallResult(done=True, success=True, value=0)
with mock.patch.object(self.qiwiscall, "results", {"request": old_result}):
self.qiwiscall.update_result("request", new_result)
self.assertEqual(old_result, new_result)
self.assertNotIn("request", self.qiwiscall.results)
def test_update_result_error(self):
old_result = qiwis.QiwiscallResult(done=False, success=False)
new_result = qiwis.QiwiscallResult(done=True, success=False, error=RuntimeError("test"))
with mock.patch.object(self.qiwiscall, "results", {"request": old_result}):
self.qiwiscall.update_result("request", new_result)
self.assertEqual(old_result, new_result)
self.assertNotIn("request", self.qiwiscall.results)
def test_update_result_no_discard(self):
old_result = qiwis.QiwiscallResult(done=False, success=False)
new_result = qiwis.QiwiscallResult(done=True, success=True, value=0)
with mock.patch.object(self.qiwiscall, "results", {"request": old_result}):
self.qiwiscall.update_result("request", new_result, discard=False)
self.assertEqual(old_result, new_result)
self.assertIs(old_result, self.qiwiscall.results["request"])
def test_update_result_not_exist(self):
"""When the request is not in the results dictionary, it is ignored."""
new_result = qiwis.QiwiscallResult(done=True, success=True, value=0)
with mock.patch.object(self.qiwiscall, "results", {}):
self.qiwiscall.update_result("request", new_result)
self.assertNotIn("request", self.qiwiscall.results)
class QiwisFunctionTest(unittest.TestCase):
"""Unit test for functions."""
def test_loads(self):
self.assertEqual(qiwis.loads(qiwis.AppInfo, APP_JSONS["app1"]), APP_INFOS["app1"])
self.assertEqual(qiwis.loads(qiwis.AppInfo, APP_JSONS["app2_default"]), APP_INFOS["app2"])
def test_dumps(self):
self.assertEqual(qiwis.dumps(APP_INFOS["app1"]), APP_JSONS["app1"])
self.assertEqual(qiwis.dumps(APP_INFOS["app2"]), APP_JSONS["app2"])
@mock.patch("qiwis.namedtuple")
@mock.patch("qiwis._immutable")
@mock.patch("qiwis.BaseApp")
def test_set_global_constant_namespace(
self,
mocked_base_app_cls,
mocked_immutable,
mocked_namedtuple
):
source = {"C0": 0, "C1": True, "C2": "str"}
mocked_immutable_values = ("M0", "M1", "M2")
mocked_namespace = mocked_namedtuple.return_value
mocked_constants = mocked_namespace.return_value
mocked_immutable.side_effect = mocked_immutable_values
constants = qiwis.set_global_constant_namespace(source)
self.assertIs(constants, mocked_constants)
self.assertIs(mocked_base_app_cls._constants, mocked_constants)
mocked_namedtuple.assert_called_once_with("ConstantNamespace", source.keys())
_args, _kwargs = mocked_namespace.call_args
self.assertSequenceEqual(_args, mocked_immutable_values)
mocked_immutable.assert_has_calls((mock.call(value) for value in source.values()))
def test_immutable(self):
sources = (
None,
0,
True,
"str",
[None, 1.2, False, "test"],
{"k1": 0, "k2": True},
)
results = (
None,
0,
True,
"str",
(None, 1.2, False, "test"),
MappingProxyType({"k1": 0, "k2": True}),
)
for source, result in zip(sources, results):
self.assertEqual(qiwis._immutable(source), result)
def test_immutable_recursive(self):
source = {
"LIST": [None, 0, True, "list"],
"LIST_LIST": [0, [1, 2, [3, 4, 5]]],
"DICT": {"A": True, "B": False},
"DICT_DICT": {"A": 0, "B": {"C": 1, "D": 2}},
"LIST_DICT": [0, {"A": 1, "B": [2, 3]}],
}
result = MappingProxyType({
"LIST": (None, 0, True, "list"),
"LIST_LIST": (0, (1, 2, (3, 4, 5))),
"DICT": MappingProxyType({"A": True, "B": False}),
"DICT_DICT": MappingProxyType({"A": 0, "B": MappingProxyType({"C": 1, "D": 2})}),
"LIST_DICT": (0, MappingProxyType({"A": 1, "B": (2, 3)})),
})
self.assertEqual(qiwis._immutable(source), result)
# when the root-type is list
self.assertEqual(qiwis._immutable(source["LIST_DICT"]), result["LIST_DICT"])
def test_add_to_path(self):
test_dir = "/test_dir"
old_path = sys.path.copy()
with qiwis._add_to_path(test_dir):
self.assertNotEqual(old_path, sys.path)
self.assertIn(test_dir, sys.path)
self.assertEqual(old_path, sys.path)
@mock.patch.object(sys, "argv", ["", "-c", "test_config.json"])
def test_get_argparser(self):
parser = qiwis._get_argparser()
args = parser.parse_args()
self.assertEqual(args.config_path, "test_config.json")
@mock.patch.object(sys, "argv", [""])
def test_get_argparser_default(self):
args = qiwis._get_argparser().parse_args()
self.assertEqual(args.config_path, "./config.json")
@mock.patch("builtins.open")
@mock.patch("json.load", return_value={"app": APP_DICTS, "constant": {"C0": 0}})
def test_read_config_file(self, mock_load, mock_open):
app_infos, constants = qiwis._read_config_file("")
self.assertEqual(constants, {"C0": 0})
self.assertEqual(app_infos, APP_INFOS)
mock_open.assert_called_once()
mock_load.assert_called_once()
@mock.patch("qiwis.set_global_constant_namespace")
@mock.patch("qiwis._get_argparser")
@mock.patch("qiwis._read_config_file", return_value=({}, {}))
@mock.patch("qiwis.Qiwis")
@mock.patch("qiwis.QApplication")
def test_main(
self,
mock_qapp,
mock_qiwis,
mock_read_config_file,
mock_get_argparser,
mock_set_global_constant_namespace,
):
qiwis.main()
mock_set_global_constant_namespace.assert_called_once()
mock_get_argparser.assert_called_once()
mock_read_config_file.assert_called_once()
mock_qiwis.assert_called_once()
mock_qapp.return_value.exec_.assert_called_once()
if __name__ == "__main__":
unittest.main()