forked from m3dev/gokart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_run.py
89 lines (71 loc) · 3.85 KB
/
test_run.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
import os
import unittest
from unittest.mock import MagicMock, patch
import luigi
import luigi.mock
import gokart
from gokart.run import _try_to_send_event_summary_to_slack
from gokart.slack import SlackConfig
class _DummyTask(gokart.TaskOnKart):
task_namespace = __name__
param = luigi.Parameter()
class RunTest(unittest.TestCase):
def setUp(self):
luigi.configuration.LuigiConfigParser._instance = None
luigi.mock.MockFileSystem().clear()
os.environ.clear()
@patch('sys.argv', new=['main', f'{__name__}._DummyTask', '--param', 'test', '--log-level=CRITICAL', '--local-scheduler'])
def test_run(self):
config_file_path = os.path.join(os.path.dirname(__name__), 'config', 'test_config.ini')
luigi.configuration.LuigiConfigParser.add_config_path(config_file_path)
os.environ.setdefault('test_param', 'test')
with self.assertRaises(SystemExit) as exit_code:
gokart.run()
self.assertEqual(exit_code.exception.code, 0)
@patch('sys.argv', new=['main', f'{__name__}._DummyTask', '--log-level=CRITICAL', '--local-scheduler'])
def test_run_with_undefined_environ(self):
config_file_path = os.path.join(os.path.dirname(__name__), 'config', 'test_config.ini')
luigi.configuration.LuigiConfigParser.add_config_path(config_file_path)
with self.assertRaises(luigi.parameter.MissingParameterException) as missing_parameter:
gokart.run()
@patch('sys.argv',
new=[
'main', '--tree-info-mode=simple', '--tree-info-output-path=tree.txt', f'{__name__}._DummyTask', '--param', 'test', '--log-level=CRITICAL',
'--local-scheduler'
])
@patch('luigi.LocalTarget', new=lambda path, **kwargs: luigi.mock.MockTarget(path, **kwargs))
def test_run_tree_info(self):
config_file_path = os.path.join(os.path.dirname(__name__), 'config', 'test_config.ini')
luigi.configuration.LuigiConfigParser.add_config_path(config_file_path)
os.environ.setdefault('test_param', 'test')
tree_info = gokart.tree_info(mode='simple', output_path='tree.txt')
with self.assertRaises(SystemExit):
gokart.run()
self.assertTrue(gokart.make_tree_info(_DummyTask(param='test')), tree_info.output().load())
@patch('gokart.make_tree_info')
def test_try_to_send_event_summary_to_slack(self, make_tree_info_mock: MagicMock):
event_aggregator_mock = MagicMock()
event_aggregator_mock.get_summury.return_value = f'{__name__}._DummyTask'
event_aggregator_mock.get_event_list.return_value = f'{__name__}._DummyTask:[]'
make_tree_info_mock.return_value = 'tree'
def get_content(content: str, **kwargs):
self.output = content
slack_api_mock = MagicMock()
slack_api_mock.send_snippet.side_effect = get_content
cmdline_args = [f'{__name__}._DummyTask', '--param', 'test']
with patch('gokart.slack.SlackConfig.send_tree_info', True):
_try_to_send_event_summary_to_slack(slack_api_mock, event_aggregator_mock, cmdline_args)
expects = os.linesep.join(['===== Event List ====', event_aggregator_mock.get_event_list(), os.linesep, '==== Tree Info ====', 'tree'])
results = self.output
self.assertEqual(expects, results)
cmdline_args = [f'{__name__}._DummyTask', '--param', 'test']
with patch('gokart.slack.SlackConfig.send_tree_info', False):
_try_to_send_event_summary_to_slack(slack_api_mock, event_aggregator_mock, cmdline_args)
expects = os.linesep.join([
'===== Event List ====',
event_aggregator_mock.get_event_list(), os.linesep, '==== Tree Info ====', 'Please add SlackConfig.send_tree_info to include tree-info'
])
results = self.output
self.assertEqual(expects, results)
if __name__ == '__main__':
unittest.main()