コード例 #1
0
class SlackNotificationsSetupEventHandlerTestCase(unittest.TestCase):
    def tearDown(self):
        SlackNotifyHandling._is_set_handlers = False

    @with_config(BASIC_CONFIG)
    @mock.patch('luigi.Task.event_handler', side_effect=mocked_event_handler)
    def test_set_default_handlers(self, mock_event_handler):
        self.slack_notify = SlackNotifyHandling(slack())
        self.slack_notify.set_handlers()
        calls = [
            mock.call(luigi.Event.START),
            mock.call(luigi.Event.SUCCESS),
            mock.call(luigi.Event.FAILURE)
        ]
        mock_event_handler.assert_has_calls(calls, any_order=True)

    @with_config(BASIC_CONFIG)
    @mock.patch('luigi.Task.event_handler', side_effect=mocked_event_handler)
    def test_set_handlers_only_once(self, mock_event_handler):
        self.slack_notify = SlackNotifyHandling(slack())
        self.slack_notify.set_handlers()
        mock_event_handler.reset_mock()
        self.slack_notify.set_handlers()
        self.assertFalse(mock_event_handler.called)

    @with_config({
        'slack': {
            'webhook': DEMO_WEBHOOK,
            'events': '["START", "custom.event"]'
        }
    })
    @mock.patch('luigi.Task.event_handler', side_effect=mocked_event_handler)
    def test_custom_handlers(self, mock_event_handler):
        self.slack_notify = SlackNotifyHandling(slack())
        self.slack_notify.set_handlers()
        calls = [mock.call(luigi.Event.START), mock.call('custom.event')]
        mock_event_handler.assert_has_calls(calls, any_order=True)

    @with_config(BASIC_CONFIG)
    @mock.patch('luigi.Task.event_handler', side_effect=mocked_event_handler)
    def test_configure_handlers_slack(self, mock_event_handler):
        configure_slack()
        calls = [
            mock.call(luigi.Event.START),
            mock.call(luigi.Event.SUCCESS),
            mock.call(luigi.Event.FAILURE)
        ]
        mock_event_handler.assert_has_calls(calls, any_order=True)
コード例 #2
0
class SlackNotificationsTriggerEventTestCase(unittest.TestCase):
    def setUp(self):
        self.demo_task = TestSlackTask()

    def tearDown(self):
        SlackNotifyHandling._is_set_handlers = False
        luigi.Task._event_callbacks = {}

    @with_config(BASIC_CONFIG)
    def test_send_message_on_trigger_start(self):
        self.slack_notify = SlackNotifyHandling(slack())
        mocked = self.slack_notify.send_slack_message = mock.Mock()
        self.slack_notify.set_handlers()
        self.demo_task.trigger_event(luigi.Event.START, self.demo_task)
        mocked.assert_called_once_with(
            'Luigi task TestSlackTask__99914b932b event START: () {}')

    @with_config({
        'slack': {
            'webhook':
            DEMO_WEBHOOK,
            'template':
            'Custom template {task_id} event {event} - {task.custom_property}'
        }
    })
    def test_send_message_with_custom_template(self):
        self.slack_notify = SlackNotifyHandling(slack())
        mocked = self.slack_notify.send_slack_message = mock.Mock()
        self.slack_notify.set_handlers()
        self.demo_task.trigger_event(luigi.Event.SUCCESS, self.demo_task)
        mocked.assert_called_once_with(
            'Custom template TestSlackTask__99914b932b event SUCCESS - custom-value'
        )

    @with_config({
        'slack': {
            'webhook': DEMO_WEBHOOK,
            'event_templates':
            '{"START": "custom template for start {task_id}"}'
        }
    })
    def test_send_message_with_custom_event_template(self):
        self.slack_notify = SlackNotifyHandling(slack())
        mocked = self.slack_notify.send_slack_message = mock.Mock()
        self.slack_notify.set_handlers()
        self.demo_task.trigger_event(luigi.Event.START, self.demo_task)
        self.demo_task.trigger_event(luigi.Event.SUCCESS, self.demo_task)
        calls = [
            mock.call('custom template for start TestSlackTask__99914b932b'),
            mock.call(
                'Luigi task TestSlackTask__99914b932b event SUCCESS: () {}')
        ]
        mocked.assert_has_calls(calls)