<commit_before>import mock
import unittest2

from st2common.persistence.reactor import Trigger, TriggerType
from st2common.models.db.reactor import TriggerDB, TriggerTypeDB
import st2reactor.container.utils as container_utils

MOCK_TRIGGER_TYPE = TriggerTypeDB()
MOCK_TRIGGER_TYPE.id = 'trigger-type-test.id'
MOCK_TRIGGER_TYPE.name = 'trigger-type-test.name'
MOCK_TRIGGER_TYPE.parameters_schema = {}
MOCK_TRIGGER_TYPE.payload_info = {}

MOCK_TRIGGER = TriggerDB()
MOCK_TRIGGER.id = 'trigger-test.id'
MOCK_TRIGGER.name = 'trigger-test.name'
MOCK_TRIGGER.parameters = {}
MOCK_TRIGGER.type = {'name': MOCK_TRIGGER_TYPE.name, 'id': MOCK_TRIGGER_TYPE.id}


class ContainerUtilsTest(unittest2.TestCase):
    @mock.patch.object(TriggerType, 'query', mock.MagicMock(
        return_value=[MOCK_TRIGGER_TYPE]))
    @mock.patch.object(Trigger, 'get_by_name', mock.MagicMock(return_value=MOCK_TRIGGER))
    @mock.patch.object(TriggerType, 'add_or_update')
    def test_add_trigger(self, mock_add_handler):
        mock_add_handler.return_value = MOCK_TRIGGER_TYPE
        container_utils.add_trigger_models([MOCK_TRIGGER_TYPE])
        self.assertTrue(mock_add_handler.called, 'trigger not added.')

    def test_add_trigger_type(self):
        """
        This sensor has misconfigured trigger type. We shouldn't explode.
        """
        class FailTestSensor(object):
            started = False

            def setup(self):
                pass

            def start(self):
                FailTestSensor.started = True

            def stop(self):
                pass

            def get_trigger_types(self):
                return [
                    {'description': 'Ain\'t got no name'}
                ]

        try:
            container_utils.add_trigger_models(FailTestSensor().get_trigger_types())
            self.assertTrue(False, 'Trigger type doesn\'t have \'name\' field. Should have thrown.')
        except Exception:
            self.assertTrue(True)<commit_msg>Add: Test cases for reactor container utils<commit_after>import mock

from st2common.persistence.reactor import Trigger, TriggerType
from st2common.models.db.reactor import TriggerDB, TriggerTypeDB
from st2common.transport.publishers import PoolPublisher
import st2reactor.container.utils as container_utils
from st2tests.base import DbTestCase

MOCK_TRIGGER_TYPE = TriggerTypeDB()
MOCK_TRIGGER_TYPE.id = 'trigger-type-test.id'
MOCK_TRIGGER_TYPE.name = 'trigger-type-test.name'
MOCK_TRIGGER_TYPE.parameters_schema = {}
MOCK_TRIGGER_TYPE.payload_info = {}

MOCK_TRIGGER = TriggerDB()
MOCK_TRIGGER.id = 'trigger-test.id'
MOCK_TRIGGER.name = 'trigger-test.name'
MOCK_TRIGGER.parameters = {}
MOCK_TRIGGER.type = {'name': MOCK_TRIGGER_TYPE.name, 'id': MOCK_TRIGGER_TYPE.id}


@mock.patch.object(PoolPublisher, 'publish', mock.MagicMock())
class ContainerUtilsTest(DbTestCase):
    @mock.patch.object(TriggerType, 'query', mock.MagicMock(
        return_value=[MOCK_TRIGGER_TYPE]))
    @mock.patch.object(Trigger, 'get_by_name', mock.MagicMock(return_value=MOCK_TRIGGER))
    @mock.patch.object(TriggerType, 'add_or_update')
    def test_add_trigger(self, mock_add_handler):
        mock_add_handler.return_value = MOCK_TRIGGER_TYPE
        container_utils.add_trigger_models([MOCK_TRIGGER_TYPE])
        self.assertTrue(mock_add_handler.called, 'trigger not added.')

    def test_add_trigger_type(self):
        """
        This sensor has misconfigured trigger type. We shouldn't explode.
        """
        class FailTestSensor(object):
            started = False

            def setup(self):
                pass

            def start(self):
                FailTestSensor.started = True

            def stop(self):
                pass

            def get_trigger_types(self):
                return [
                    {'description': 'Ain\'t got no name'}
                ]

        try:
            container_utils.add_trigger_models(FailTestSensor().get_trigger_types())
            self.assertTrue(False, 'Trigger type doesn\'t have \'name\' field. Should have thrown.')
        except Exception:
            self.assertTrue(True)

    def test_create_trigger_instance_invalid_trigger(self):
        instance = container_utils.create_trigger_instance({'name': 'footrigger'}, {}, None)
        self.assertTrue(instance is None)

    def test_add_trigger_type_no_params(self):
        # Trigger type with no params should create a trigger with same name as trigger type.
        trig_type = {
            'name': 'myawesometriggertype',
            'description': 'Words cannot describe how awesome I am.',
            'parameters_schema': {},
            'payload_schema': {}
        }
        trigtype_dbs = container_utils.add_trigger_models([trig_type])
        trigger_type, trigger = trigtype_dbs[0]
        trigtype_db = TriggerType.get_by_id(trigger_type.id)
        self.assertEqual(trigtype_db.name, trig_type.get('name'))
        self.assertTrue(trigger is not None)
        self.assertEqual(trigger.name, trigtype_db.name)

    def test_add_trigger_type_with_params(self):
        # Trigger type with params should not create a trigger.
        PARAMETERS_SCHEMA = {
            "type": "object",
            "properties": {
                "url": {"type": "string"}
            },
            "required": ['url'],
            "additionalProperties": False
        }
        trig_type = {
            'name': 'myawesometriggertype2',
            'description': 'Words cannot describe how awesome I am.',
            'parameters_schema': PARAMETERS_SCHEMA,
            'payload_schema': {}
        }
        trigtype_dbs = container_utils.add_trigger_models([trig_type])
        trigger_type, trigger = trigtype_dbs[0]
        trigtype_db = TriggerType.get_by_id(trigger_type.id)
        self.assertEqual(trigtype_db.name, trig_type.get('name'))
        self.assertEqual(trigger, None)
{'fraction_non_alphanumeric': 0.10003396739130435, 'fraction_numerical': 0.0022078804347826085, 'mean_word_length': 2.931241655540721, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5988551', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1781, 'n_words': 331}