def test_evaluate_rules_with_cookie_when_matched_rules_and_termination(self): production_to_be_called = MagicMock(return_value=True) extra_rules = [ Rule(lambda *args: True, lambda *args: False, Priority.MIN_PRIORITY), Rule(lambda *args: True, production_to_be_called, Priority.get_lower_priority_value(Priority.MAX_PRIORITY)), Rule(lambda *args: False, lambda *args: False, Priority.MAX_PRIORITY) ] self.processor.rules = self.rules + extra_rules halt = self.processor.evaluate_rules_with_cookie(self.cookie) total_call_count = 0 for rule in self.rules: total_call_count += rule._action.call_count self.assertEqual(total_call_count, 0) self.assertEqual(production_to_be_called.call_count, 1) self.assertTrue(halt)
from datetime import datetime from unittest.mock import MagicMock from cookiemonster.common.models import Cookie, Enrichment from cookiemonster.common.context import Context from hgicommon.collections import Metadata from hgicommon.mixable import Priority from hgicommon.data_source import register from cookiemonster.processor.models import EnrichmentLoader NAME_MATCH_LOADER_ENRICHMENT_LOADER_ID = "name_match_loader" NAME_ENRICHMENT_LOADER_MATCH_COOKIE = "/cookie/matches/name/for/enrichment" def _can_enrich(cookie: Cookie, context: Context) -> bool: return cookie.identifier == NAME_ENRICHMENT_LOADER_MATCH_COOKIE \ and NAME_MATCH_LOADER_ENRICHMENT_LOADER_ID not in [enrichment.source for enrichment in cookie.enrichments] def _load_enrichment(cookie: Cookie, context: Context) -> Enrichment: return Enrichment(NAME_MATCH_LOADER_ENRICHMENT_LOADER_ID, datetime.min, Metadata({"matches": True})) _priority = Priority.get_lower_priority_value(Priority.MAX_PRIORITY) _enrichment_loader = EnrichmentLoader(MagicMock(side_effect=_can_enrich), MagicMock(side_effect=_load_enrichment), NAME_MATCH_LOADER_ENRICHMENT_LOADER_ID, _priority) register(_enrichment_loader)
def test_get_higher_priority_value(self): higher = Priority.get_higher_priority_value(Priority.MIN_PRIORITY) self.assertLess(abs(higher - Priority.MIN_PRIORITY), abs(Priority.MAX_PRIORITY - Priority.MIN_PRIORITY))
def setUp(self): self.rules = [] priority = Priority.MAX_PRIORITY for _ in range(10): self.rules.append(create_mock_rule(priority)) priority = Priority.get_lower_priority_value(priority)
def setUp(self): self.low_priority = TestPriority._MockPriority(Priority.MIN_PRIORITY) self.medium_priority = TestPriority._MockPriority(Priority.get_lower_priority_value(Priority.MAX_PRIORITY)) self.high_priority = TestPriority._MockPriority(Priority.MAX_PRIORITY)