Example #1
0
    def test_missing_channel_settings(self, slack):
        logging.warning = MagicMock()
        ret = slack.generate_schedule_job(
            ScheduledCommand("name", lambda: "dummy", "module_name", {}, {}))

        assert_that(logging.warning.call_count).is_equal_to(1)
        assert_that(ret).is_none()
Example #2
0
        def wrapper(func: ScheduledFunction) -> ScheduledFunction:
            @wraps(func)
            def wrapped_function(given_config: Dict[str, Any]) \
                    -> Union[str, RichMessage]:
                return func(given_config)

            module = inspect.getmodule(func)
            self = cls.__instances.get(cls.__name__, None)
            # Register only if bot is instantiated.
            if self and module:
                module_name = module.__name__
                config = self.plugin_config.get(module_name, {})
                schedule_config = config.get('schedule', {})
                if schedule_config:
                    # If command name duplicates, update with the later one.
                    # The order stays.
                    command = ScheduledCommand(name, wrapped_function,
                                               module_name, config,
                                               schedule_config)
                    try:
                        # If command is already registered, updated it.
                        idx = [c.name for c in cls.__schedules[cls.__name__]] \
                            .index(command.name)
                        cls.__schedules[cls.__name__][idx] = command
                    except ValueError:
                        # Not registered, just append it.
                        cls.__schedules[cls.__name__].append(command)
                else:
                    logging.warning(
                        'Missing configuration for schedule job. %s. '
                        'Skipping.' % module_name)

            # To ease plugin's unit test
            return wrapped_function
Example #3
0
    def test_valid(self):
        scheduled_command = ScheduledCommand(DummyClass.scheduled_job.__name__,
                                             DummyClass.scheduled_job,
                                             DummyClass.__name__,
                                             {'spam': "ham"}, {
                                                 'trigger': "cron",
                                                 'hour': 10,
                                                 'minute': 30
                                             })

        assert_that(scheduled_command.name) \
            .is_equal_to(DummyClass.scheduled_job.__name__)
        assert_that(scheduled_command.function) \
            .is_equal_to(DummyClass.scheduled_job)
        assert_that(scheduled_command.module_name) \
            .is_equal_to(DummyClass.__name__)
        assert_that(scheduled_command.config).is_equal_to({'spam': "ham"})
        assert_that(scheduled_command.schedule_config) \
            .is_equal_to({'trigger': "cron",
                          'hour': 10,
                          'minute': 30})
        assert_that(scheduled_command.job_id) \
            .is_equal_to("%s.%s" % (DummyClass.__name__,
                                    DummyClass.scheduled_job.__name__))

        # is callable
        assert_that(scheduled_command()).is_equal_to("ham")
Example #4
0
    def test_valid_settings_with_rich_message(self, slack):
        ret = slack.generate_schedule_job(
            ScheduledCommand("name", lambda _: SlackMessage(), "module_name",
                             {}, {'channels': ("channel1", )}))

        with patch.object(slack.client, "post", return_value=dict()):
            ret()
            assert_that(slack.client.post.call_count) \
                .is_equal_to(1)
Example #5
0
    def test_valid_settings(self, slack):
        ret = slack.generate_schedule_job(
            ScheduledCommand("name", lambda _: "dummy", "module_name", {},
                             {'channels': ("channel1", )}))
        assert_that(inspect.isfunction(ret)).is_true()

        with patch.object(slack,
                          "enqueue_sending_message",
                          return_value=Future()):
            ret()
            assert_that(slack.enqueue_sending_message.call_count) \
                .is_equal_to(1)
Example #6
0
    def test_missing_returning_function(self):
        class BaseImpl(Base):
            def connect(self) -> None:
                pass

            def generate_schedule_job(self,
                                      command: ScheduledCommand) \
                    -> Optional[Callable[..., None]]:
                return None

        command = ScheduledCommand('spam', lambda config: "ham",
                                   'dummy_module_name', {'egg': "spam"},
                                   {'spam': "egg"})
        base_impl = BaseImpl()
        base_impl.add_schedule_jobs([command])

        assert_that(base_impl.scheduler.get_jobs()).is_empty()
Example #7
0
    def test_valid_configuration(self):
        class BaseImpl(Base):
            def connect(self) -> None:
                pass

            def generate_schedule_job(self,
                                      command: ScheduledCommand) \
                    -> Optional[Callable[..., None]]:
                def job_function() -> None:
                    command()

                return job_function

        command = ScheduledCommand('spam', lambda config: "ham",
                                   'dummy_module_name', {'egg': "spam"},
                                   {'spam': "egg"})
        base_impl = BaseImpl()
        base_impl.add_schedule_jobs([command])

        assert_that(base_impl.scheduler.get_jobs()).is_length(1)
        registered_job = base_impl.scheduler.get_job(command.job_id)
        assert_that(registered_job).is_not_none()