Commit | Line | Data |
---|---|---|
66b856f5 FC |
1 | from panda3d.core import load_prc_file_data |
2 | load_prc_file_data('', 'window-type none') | |
8c440734 FC |
3 | from pathlib import Path |
4 | import sys | |
5 | if '' in sys.path: sys.path.remove('') | |
6 | sys.path.append(str(Path(__file__).parent.parent.parent)) | |
c991401b | 7 | from os import makedirs |
d6c157a0 | 8 | from shutil import rmtree |
8c440734 | 9 | from unittest import TestCase |
d6c157a0 | 10 | from unittest.mock import patch, MagicMock |
8c440734 FC |
11 | from main import Main |
12 | import logging | |
13 | from ya2.utils.dictfile import DctFile | |
14 | import importlib | |
15 | import main | |
07a1eaf3 | 16 | import pmachines.application.application |
8c440734 FC |
17 | |
18 | ||
19 | class MainTests(TestCase): | |
20 | ||
21 | def setUp(self): | |
22 | self.__optfile = DctFile('options.ini') | |
23 | self.__verbose_val = self.__optfile['development']['verbose_log'] | |
d6c157a0 FC |
24 | makedirs('/tmp/.mount_Pmachines_unit_test/usr/bin', exist_ok=True) |
25 | with open('/tmp/.mount_Pmachines_unit_test/usr/bin/appimage_version.txt', 'w') as f: | |
26 | f.write('version_test') | |
8c440734 FC |
27 | |
28 | def tearDown(self): | |
29 | self.__optfile['development']['verbose_log'] = self.__verbose_val | |
30 | self.__optfile.store() | |
d6c157a0 | 31 | rmtree('/tmp/.mount_Pmachines_unit_test') |
8c440734 FC |
32 | |
33 | def test_logging(self): | |
34 | self.assertEqual(logging.root.handlers[0].formatter._fmt, '%(asctime)s %(message)s') | |
35 | self.assertEqual(logging.root.handlers[0].formatter.datefmt, '%H:%M:%S') | |
36 | self.__optfile['development']['verbose_log'] = 0 | |
37 | self.__optfile.store() | |
38 | importlib.reload(main) | |
39 | self.assertEqual(logging.root.level, logging.INFO) | |
40 | self.__optfile['development']['verbose_log'] = 1 | |
41 | self.__optfile.store() | |
42 | importlib.reload(main) | |
43 | self.assertEqual(logging.root.level, logging.DEBUG) | |
d6c157a0 FC |
44 | |
45 | def test_update(self): | |
807c4e10 | 46 | with (patch.object(main, 'argv', ['python -m unittest']), |
07a1eaf3 | 47 | patch.object(pmachines.application.application, 'argv', ['python -m unittest'])): |
807c4e10 FC |
48 | _main = Main() |
49 | _main._Main__appimage_builder.update = MagicMock() | |
50 | _main._Main__pmachines._fsm.demand = MagicMock() | |
51 | _main._Main__pmachines.run = MagicMock() | |
52 | _main.run() | |
bf77b5d5 | 53 | _main._Main__pmachines.destroy() |
807c4e10 FC |
54 | _main._Main__appimage_builder.update.assert_not_called() |
55 | with (patch.object(sys, 'argv', ['python -m unittest', '--update']), | |
07a1eaf3 | 56 | patch.object(pmachines.application.application, 'argv', ['python -m unittest', '--update'])): |
807c4e10 FC |
57 | _main = Main() |
58 | _main._Main__appimage_builder.update = MagicMock() | |
59 | _main.run() | |
bf77b5d5 | 60 | _main._Main__pmachines.destroy() |
807c4e10 | 61 | _main._Main__appimage_builder.update.assert_called_once() |
66b856f5 FC |
62 | |
63 | def test_version(self): | |
807c4e10 | 64 | with (patch.object(main, 'argv', ['python -m unittest']), |
07a1eaf3 | 65 | patch.object(pmachines.application.application, 'argv', ['python -m unittest'])): |
807c4e10 FC |
66 | _main = Main() |
67 | _main._Main__run_game = MagicMock() | |
68 | _main.run() | |
bf77b5d5 | 69 | _main._Main__pmachines.destroy() |
807c4e10 FC |
70 | _main._Main__run_game.assert_called_once() |
71 | with (patch.object(main, 'argv', ['python -m unittest', '--version']), | |
07a1eaf3 | 72 | patch.object(pmachines.application.application, 'argv', ['python -m unittest', '--version'])): |
807c4e10 FC |
73 | _main = Main() |
74 | _main._Main__run_game = MagicMock() | |
75 | _main.run() | |
bf77b5d5 | 76 | _main._Main__pmachines.destroy() |
807c4e10 | 77 | _main._Main__run_game.assert_not_called() |
66b856f5 | 78 | |
807c4e10 | 79 | @patch.object(main, 'argv', ['python -m unittest']) |
07a1eaf3 | 80 | @patch.object(pmachines.application.application, 'argv', ['python -m unittest']) |
66b856f5 | 81 | def test_run_game(self): |
807c4e10 FC |
82 | _main = Main() |
83 | _main._Main__pmachines.run = MagicMock() | |
84 | _main.run() | |
bf77b5d5 | 85 | _main._Main__pmachines.destroy() |
807c4e10 | 86 | _main._Main__pmachines.run.assert_called_once() |
66b856f5 | 87 | |
07a1eaf3 | 88 | @patch.object(pmachines.application.application, 'argv', ['python -m unittest']) |
807c4e10 FC |
89 | @patch.object(main, 'argv', ['python -m unittest']) |
90 | @patch.object(main, 'print_exc') | |
b1af73f2 | 91 | def test_run_game_exception(self, print_exc_mock): |
807c4e10 FC |
92 | _main = Main() |
93 | _main._Main__pmachines.run = MagicMock(side_effect=Exception) | |
94 | _main.run() | |
bf77b5d5 | 95 | _main._Main__pmachines.destroy() |
807c4e10 | 96 | _main._Main__pmachines.run.trowed_exception() |
b1af73f2 | 97 | print_exc_mock.assert_called_once() |