def _test_loaded_in_correct_order(
        self, enrichment_manager: EnrichmentManager, enrichment_loaders: Iterable[EnrichmentLoader]
    ):
        """
        Tests that the given enrichment manager applies enrichments defined be the given loaders in the correct order.
        :param enrichment_manager: enrichment manager
        :param enrichment_loaders: enrichment loaders
        """
        logging.root.setLevel(logging.CRITICAL)
        cookie = Cookie("the_identifier")

        enrichment_loaders_priority_queue = PriorityQueue()
        for enrichment_loader in enrichment_loaders:
            if enrichment_loader.can_enrich(cookie):
                enrichment_loaders_priority_queue.put(enrichment_loader)

        enrichment = enrichment_manager.next_enrichment(cookie)
        while enrichment is not None:
            expected_enrichment_loader = enrichment_loaders_priority_queue.get()  # type: EnrichmentLoader
            expected_enrichment = expected_enrichment_loader.load_enrichment(cookie)
            self.assertEqual(enrichment, expected_enrichment)
            cookie.enrich(enrichment)
            expected_enrichment_loader.can_enrich = MagicMock(return_value=False)
            enrichment = enrichment_manager.next_enrichment(cookie)
        self.assertTrue(enrichment_loaders_priority_queue.empty())
    def handle_cookie_enrichment(self, cookie: Cookie):
        logging.info("Checking if any of the %d enrichment loader(s) can load enrichment for cookie with identifier "
                     "\"%s\"" % (len(self.enrichment_loaders), cookie.identifier))
        enrichment_manager = EnrichmentManager(self.enrichment_loaders)
        enrichment = enrichment_manager.next_enrichment(cookie)

        if enrichment is None:
            logging.info("Cannot enrich cookie with identifier \"%s\" any further" % cookie.identifier)
        else:
            logging.info("Applying enrichment from source \"%s\" to cookie with identifier \"%s\""
                         % (enrichment.source, cookie.identifier))
            self.cookie_jar.enrich_cookie(cookie.identifier, enrichment)