Example #1
0
    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 = [
            ('RuleTestNoCloseMethod', RuleTestNoCloseMethod,
             'RuleTestNoCloseMethod'),
            ('RuleTestDangerous', RuleTestDangerous, 'RuleTestDangerous')
        ]
        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', 'socorro.unittest.lib.test_transform_rules.'
            'RuleTestNoCloseMethod')
        config.logger.debug.assert_any_call(
            'trying to close %s', 'socorro.unittest.lib.test_transform_rules.'
            'RuleTestDangerous')
        config.logger.debug.assert_any_call(
            '%s has no close', 'socorro.unittest.lib.test_transform_rules.'
            'RuleTestNoCloseMethod')
Example #2
0
    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',
            'socorro.unittest.lib.test_transform_rules.'
            'TestRuleTestBrokenCloseMethod'
        )
Example #3
0
    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)
Example #4
0
    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))
Example #5
0
    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_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, RuleTestLaughable))
        ok_(isinstance(trs.rules[1], StatsdRuleBenchmarkWrapper))
        ok_(isinstance(trs.rules[1].wrapped_object, RuleTestDangerous))

        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.RuleTestLaughable.act',
                    1000  # 1 second
                ),
                call(
                    'timing.RuleTestDangerous.act',
                    1000  # 1 second
                ),
            ])
    def test_TransformRuleSystem_apply_all_until_action_fails(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_action_fails(s, d)
        assert d == {}
        assert quit_check_mock.call_count == 1
Example #8
0
 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_TransformRuleSystem_load_rules(self):
     rules = transform_rules.TransformRuleSystem()
     some_rules = [(True, '', '', True, '', ''),
                   (False, '', '', False, '', '')]
     rules.load_rules(some_rules)
     expected = [
         transform_rules.TransformRule(*(True, (), {}, True, (), {})),
         transform_rules.TransformRule(*(False, (), {}, False, (), {}))
     ]
     assert rules.rules == expected
    def test_rules_in_config(self):
        config = DotDict()
        config['TestRuleTestLaughable.laughable'] = 'wilma'
        config['TestRuleTestDangerous.dangerous'] = 'dwight'
        config.rules_list = [TestRuleTestLaughable, 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))
Example #11
0
    def test_TransformRuleSystem_apply_all_until_predicate_fails(self):
        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()
        rules.load_rules(some_rules)
        s = {}
        d = {}
        rules.apply_until_predicate_fails(s, d)
        assert_expected(d, {'one': 1})

        some_rules = [
            (True, '', '', True, '', ''),
            (True, '', '', False, '', ''),
        ]
        rules.load_rules(some_rules)
        res = rules.apply_until_predicate_fails()
        assert_expected(res, None)

        some_rules = [
            (True, '', '', True, '', ''),
            (False, '', '', False, '', ''),
        ]
        rules.load_rules(some_rules)
        res = rules.apply_until_predicate_fails()
        assert_expected(res, False)

        some_rules = [
            (True, '', '', True, '', ''),
            (False, '', '', True, '', ''),
        ]
        rules.load_rules(some_rules)
        res = rules.apply_until_predicate_fails()
        assert_expected(res, False)
Example #12
0
    def test_TransformRuleSystem_apply_all_until_action_succeeds(self):
        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()
        rules.load_rules(some_rules)
        s = {}
        d = {}
        rules.apply_until_action_succeeds(s, d)
        assert_expected(d, {'one': 1})
Example #13
0
 def test_TransformRuleSystem_init(self):
     rules = transform_rules.TransformRuleSystem()
     assert_expected(rules.rules, [])
 def test_TransformRuleSystem_init(self):
     rules = transform_rules.TransformRuleSystem()
     assert rules.rules == []