def test_rules_close_bubble_close_errors(self):
        config = DotDict()
        config.logger = Mock()
        config.tag = 'test.rule'
        config.action = 'apply_all_rules'
        config.rules_list = DotDict()
        config.rules_list.class_list = [
            (
                'TestRuleTestBrokenCloseMethod',
                TestRuleTestBrokenCloseMethod,
                'TestRuleTestBrokenCloseMethod'
            ),
        ]
        trs = transform_rules.TransformRuleSystem(config)
        assert_raises(
            AttributeError,
            trs.close
        )

        assert len(config.logger.debug.mock_calls) == 1
        config.logger.debug.assert_any_call(
            'trying to close %s',
            'socorrolib.unittest.lib.test_transform_rules.'
            'TestRuleTestBrokenCloseMethod'
        )
    def test_rules_close(self):
        config = DotDict()
        config.logger = Mock().s
        config.chatty_rules = False
        config.chatty = False
        config.tag = 'test.rule'
        config.action = 'apply_all_rules'
        config['TestRuleTestLaughable.laughable'] = 'wilma'
        config['TestRuleTestDangerous.dangerous'] = 'dwight'
        config.rules_list = DotDict()
        config.rules_list.class_list = [
            (
                'TestRuleTestLaughable',
                TestRuleTestLaughable,
                'TestRuleTestLaughable'
            ),
            (
                'TestRuleTestDangerous',
                TestRuleTestDangerous,
                'TestRuleTestDangerous'
            )
        ]
        trs = transform_rules.TransformRuleSystem(config)

        trs.close()

        eq_(trs.rules[0].close_counter, 1)
        eq_(trs.rules[1].close_counter, 1)
    def test_rules_in_config(self):
        config = DotDict()
        config.chatty_rules = False
        config.chatty = False
        config.tag = 'test.rule'
        config.action = 'apply_all_rules'
        config['TestRuleTestLaughable.laughable'] = 'wilma'
        config['TestRuleTestDangerous.dangerous'] = 'dwight'
        config.rules_list = DotDict()
        config.rules_list.class_list = [
            (
                'TestRuleTestLaughable',
                TestRuleTestLaughable,
                'TestRuleTestLaughable'
            ),
            (
                'TestRuleTestDangerous',
                TestRuleTestDangerous,
                'TestRuleTestDangerous'
            )
        ]
        trs = transform_rules.TransformRuleSystem(config)

        ok_(isinstance(trs.rules[0], TestRuleTestLaughable))
        ok_(isinstance(trs.rules[1], TestRuleTestDangerous))
        ok_(trs.rules[0].predicate(None))
        ok_(trs.rules[1].action(None))
    def test_TransformRuleSystem_apply_all_until_predicate_succeeds(self):

        quit_check_mock = Mock()

        def assign_1(s, d):
            d['one'] = 1
            return True

        def increment_1(s, d):
            try:
                d['one'] += 1
                return True
            except KeyError:
                return False

        some_rules = [(True, '', '', increment_1, '', ''),
                      (True, '', '', assign_1, '', ''),
                      (False, '', '', increment_1, '', ''),
                      (True, '', '', increment_1, '', ''),
                     ]
        rules = transform_rules.TransformRuleSystem(quit_check=quit_check_mock)
        rules.load_rules(some_rules)
        s = {}
        d = {}
        rules.apply_until_predicate_succeeds(s, d)
        assert_expected(d, {})
        assert_expected(quit_check_mock.call_count, 1)
Exemple #5
0
    def test_apply_all(self, statsd_obj):
        config = self.setup_config('processor')
        trs = transform_rules.TransformRuleSystem(config)

        ok_(isinstance(trs.rules[0], StatsdRuleBenchmarkWrapper))
        ok_(isinstance(trs.rules[0].wrapped_object, TestRuleTestLaughable))
        ok_(isinstance(trs.rules[1], StatsdRuleBenchmarkWrapper))
        ok_(isinstance(trs.rules[1].wrapped_object, TestRuleTestDangerous))

        now_str = 'socorro.external.statsd.statsd_base.datetime'
        with patch(now_str) as now_mock:
            times =  [
                datetime(2015, 5, 4, 15, 10, 3),
                datetime(2015, 5, 4, 15, 10, 2),
                datetime(2015, 5, 4, 15, 10, 1),
                datetime(2015, 5, 4, 15, 10, 0),
            ]
            ok_(trs.rules[0].predicate(None))
            statsd_obj.timing.has_calls([])
            ok_(trs.rules[1].action(None))
            statsd_obj.timing.has_calls([])

            trs.apply_all_rules()
            statsd_obj.timing.has_calls([
                call(
                    'timing.TestRuleTestLaughable.act',
                    1000  # 1 second
                ),
                call(
                    'timing.TestRuleTestDangerous.act',
                    1000  # 1 second
                ),
            ])
 def test_TransformRuleSystem_append_rules(self):
     rules = transform_rules.TransformRuleSystem()
     some_rules = [(True, '', '', True, '', ''),
                   (False, '', '', False, '', '')]
     rules.append_rules(some_rules)
     expected = [transform_rules.TransformRule(*(True, (), {},
                                                 True, (), {})),
                 transform_rules.TransformRule(*(False, (), {},
                                                 False, (), {}))]
     assert_expected_same(rules.rules, expected)
    def test_rules_close_if_close_method_available(self):
        config = DotDict()
        config.logger = Mock()
        config.chatty_rules = False
        config.chatty = False
        config.tag = 'test.rule'
        config.action = 'apply_all_rules'
        config.rules_list = DotDict()
        config.rules_list.class_list = [
            (
                'TestRuleTestNoCloseMethod',
                TestRuleTestNoCloseMethod,
                'TestRuleTestNoCloseMethod'
            ),
            (
                'TestRuleTestDangerous',
                TestRuleTestDangerous,
                'TestRuleTestDangerous'
            )
        ]
        trs = transform_rules.TransformRuleSystem(config)
        trs.close()

        assert len(config.logger.debug.mock_calls) == 3
        config.logger.debug.assert_any_call(
            'trying to close %s',
            'socorrolib.unittest.lib.test_transform_rules.'
            'TestRuleTestNoCloseMethod'
        )
        config.logger.debug.assert_any_call(
            'trying to close %s',
            'socorrolib.unittest.lib.test_transform_rules.'
            'TestRuleTestDangerous'
        )
        config.logger.debug.assert_any_call(
            '%s has no close',
            'socorrolib.unittest.lib.test_transform_rules.'
            'TestRuleTestNoCloseMethod'
        )
 def test_TransformRuleSystem_init(self):
     rules = transform_rules.TransformRuleSystem()
     assert_expected(rules.rules, [])