Exemple #1
0
    def test_container_get_commands_empty(self):

        reg = Registry()
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertFalse(len(commands) > 0)
Exemple #2
0
    def test_container_get_commands_empty(self):

        reg = Registry()
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertFalse(len(commands) > 0)
Exemple #3
0
    def test_container_get_commands(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertTrue(len(commands) > 0)
Exemple #4
0
    def test_container_get_commands(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())

        commands = reg.get_commands()
        self.assertTrue(len(commands) > 0)
Exemple #5
0
    def test_container_error(self):

        # test if application not implement container abstract class
        with self.assertRaises(ContainerError) as container_error:
            reg = Registry()
            reg.register(FakeApp3())

        exception = container_error.exception
        self.assertEqual('FakeApp3', exception.app_name)
        self.assertEqual('ContainerError: Cannot use FakeApp3 as container application', exception.message)
Exemple #6
0
    def test_build_registry(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())
        reg.register(FakeApp4())

        # check if list of apps is not empty list
        self.assertIsNot(reg.get_apps(), [])
        self.assertEqual(3, len(reg.get_apps()))
        self.assertEqual(3, len(reg.get_routes()))
Exemple #7
0
    def test_build_registry(self):

        reg = Registry()
        reg.register(FakeApp1())
        reg.register(FakeApp2())
        reg.register(FakeApp4())

        # check if list of apps is not empty list
        self.assertIsNot(reg.get_apps(), [])
        self.assertEqual(3, len(reg.get_apps()))
        self.assertEqual(3, len(reg.get_routes()))
Exemple #8
0
    def test_container_error(self):

        # test if application not implement container abstract class
        with self.assertRaises(ContainerError) as container_error:
            reg = Registry()
            reg.register(FakeApp3())

        exception = container_error.exception
        self.assertEqual('FakeApp3', exception.app_name)
        self.assertEqual(
            'ContainerError: Cannot use FakeApp3 as container application',
            exception.message)
Exemple #9
0
class CommandProcessorFixture(unittest.TestCase):

    """ Command Processor tests """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_command(self):
        """ given that we have a handler registered for a command, when we send a command, it should call the handler"""
        self._handler = MyCommandHandler()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")

    def test_handle_event(self):
        """ Given that we have many handlers registered of an event, when we raise an event, it should call all the handlers"""

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()
        self._subscriber_registry.register(MyEvent, lambda: self._handler)
        self._subscriber_registry.register(MyEvent, lambda: self._other_handler)
        self._commandProcessor.publish(self._request)

        self.assertTrue(self._handler.called, "The first handler should be called with the message")
        self.assertTrue((self._other_handler, "The second handler should also be called with the message"))

    def test_missing_command_handler_registration(self):
        """Given that we are missing a handler for a command, when we send a command, it should throw an exception"""

        self._handler = MyCommandHandler()
        self._request = MyCommand()

        exception_thrown = False
        try:
            self._commandProcessor.send(self._request)
        except:
            exception_thrown = True

        self.assertTrue(exception_thrown, "Expected an exception to be thrown when no handler is registered for a command")

    def test_missing_event_handler_registration(self):
        """Given that we have no handlers register for an event, when we raise an event, it should not error """

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()

        exception_thrown = False
        try:
            self._commandProcessor.publish(self._request)
        except:
            exception_thrown = True

        self.assertFalse(exception_thrown, "Did not expect an exception to be thrown where there are no handlers for an event")
class PipelineTests(unittest.TestCase):
    """Tests of decorators on handlers.
        It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python
        already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators
        used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over
        rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable
        to Pythoh via its decorator syntax
        This test suite is really about proving we can provide equivalent functionality
        We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter
    """
    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(
            registry=self._subscriber_registry)

    def test_handle_retry_on_command(self):
        """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """
        self._handler = MyHandlerSupportingRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(
            self._handler.called,
            "Expected the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")

    def test_exceed_retry_on_command(self):
        """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries"""
        self._handler = MyHandlerBreakingAfterRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        self.assertTrue(
            exception_raised,
            "Exepcted the exception to bubble out, when we run out of retries")
        self.assertFalse(
            self._handler.called,
            "Did not expect the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")

    def test_handle_circuit_breaker_on_command(self):
        """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries"""
        self._handler = MyHandlerBreakingCircuitAfterThreeFailures()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        # Now see if the circuit is broken following the failed call
        circuit_broken = False
        try:
            self._commandProcessor.send(self._request)
        except CircuitBrokenError:
            circuit_broken = True

        self.assertTrue(
            exception_raised,
            "Exepcted an exception to be raised, when we run out of retries")
        self.assertTrue(circuit_broken,
                        "Expected the circuit to be broken to further calls")
        self.assertFalse(
            self._handler.called,
            "Did not expect the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")
Exemple #11
0
class EUNlgService:
    def __init__(
        self,
        random_seed: Optional[int] = None,
        force_cache_refresh: bool = False,
        nomorphi: bool = False,
        planner: str = "full",
    ) -> None:
        """
        :param random_seed: seed for random number generation, for repeatability
        :param force_cache_refresh:
        :param nomorphi: don't load Omorphi for morphological generation. This removes the dependency on Omorphi,
            so allows easier setup, but means that no morphological inflection will be performed on the output,
            which is generally a very bad thing for the full pipeline
        """

        # New registry and result importer
        self.registry = Registry()

        # DataSets
        DATA_ROOT = Path(__file__).parent.absolute() / ".." / "data"

        self.datasets = [
            "cphi",
            "health_cost",
            "health_funding",
        ]
        for dataset in self.datasets:
            cache_path: Path = (DATA_ROOT / "{}.cache".format(dataset)).absolute()
            if not cache_path.exists():
                raise IOError("No cached dataset found at {}. Datasets must be generated before startup.")
            self.registry.register("{}-data".format(dataset), DataFrameStore(str(cache_path)))

        # Resources
        self.resources = [
            CPHIEnglishResource(),
            CPHIFinnishResource(),
            CPHICroatianResource(),
            CPHIRussianResource(),
            CPHIEstonianResource(),
            CPHISlovenianResource(),
            ENVEnglishResource(),
            ENVFinnishResource(),
            HealthCostEnglishResource(),
            HealthCostFinnishResource(),
            HealthFundingEnglishResource(),
            HealthFundingFinnishResource(),
        ]

        # Templates
        self.registry.register("templates", self._load_templates())

        # Slot Realizers:
        realizers: List[SlotRealizerComponent] = []
        for resource in self.resources:
            for realizer in resource.slot_realizer_components():
                realizers.append(realizer(self.registry))
        self.registry.register("slot-realizers", realizers)

        # Language metadata
        self.registry.register("conjunctions", CONJUNCTIONS)
        self.registry.register("errors", ERRORS)

        # PRNG seed
        self._set_seed(seed_val=random_seed)

        def _get_components(headline=False, planner="full"):
            # Put together the list of components
            # This varies depending on whether it's for headlines and which language we are doing stuff in
            yield EUMessageGenerator(expand=True)
            yield EUImportanceSelector()
            if planner == "random":
                yield EURandomHeadlineDocumentPlanner() if headline else EURandomBodyDocumentPlanner()
            elif planner == "score":
                yield EUScoreHeadlineDocumentPlanner() if headline else EUScoreBodyDocumentPlanner()
            elif planner == "earlystop":
                yield EUEarlyStopHeadlineDocumentPlanner() if headline else EUEarlyStopBodyDocumentPlanner()
            elif planner == "topicsim":
                yield EUTopicSimHeadlineDocumentPlanner() if headline else EUTopicSimBodyDocumentPlanner()
            elif planner == "contextsim":
                yield EUContextSimHeadlineDocumentPlanner() if headline else EUContextSimBodyDocumentPlanner()
            elif planner == "neuralsim":
                if headline:
                    yield EUHeadlineDocumentPlanner()
                else:
                    yield TemplateAttacher()
                    yield EUNeuralSimBodyDocumentPlanner()
                    yield EmbeddingRemover()

            elif planner == "full":
                yield EUHeadlineDocumentPlanner() if headline else EUBodyDocumentPlanner()
            else:
                raise ValueError("INCORRECT PLANNER SETTING")
            yield TemplateSelector()
            yield Aggregator()
            yield SlotRealizer()
            yield LanguageSplitComponent(
                {
                    "en": EnglishEUDateRealizer(),
                    "fi": FinnishEUDateRealizer(),
                    "hr": CroatianEUDateRealizer(),
                    "de": GermanEUDateRealizer(),
                    "ru": RussianEUDateRealizer(),
                    "ee": EstonianEUDateRealizer(),
                    "sl": SlovenianEUDateRealizer(),
                }
            )
            yield EUEntityNameResolver()
            yield EUNumberRealizer()
            yield MorphologicalRealizer(
                {
                    "en": EnglishUralicNLPMorphologicalRealizer(),
                    "fi": FinnishUralicNLPMorphologicalRealizer(),
                    "hr": CroatianSimpleMorphologicalRealizer(),
                    "ru": RussianMorphologicalRealizer(),
                    "ee": EstonianUralicNLPMorphologicalRealizer(),
                    "sl": SlovenianSimpleMorphologicalRealizer(),
                }
            )
            yield HeadlineHTMLSurfaceRealizer() if headline else BodyHTMLSurfaceRealizer()

        log.info("Configuring Body NLG Pipeline (planner = {})".format(planner))
        self.body_pipeline = NLGPipeline(self.registry, *_get_components(planner=planner))
        self.headline_pipeline = NLGPipeline(self.registry, *_get_components(headline=True, planner=planner))

    T = TypeVar("T")

    def _get_cached_or_compute(
        self, cache: str, compute: Callable[..., T], force_cache_refresh: bool = False, relative_path: bool = True
    ) -> T:  # noqa: F821 -- Needed until https://github.com/PyCQA/pyflakes/issues/427 reaches a release
        if relative_path:
            cache = os.path.abspath(os.path.join(os.path.dirname(__file__), cache))
        if force_cache_refresh:
            log.info("force_cache_refresh is True, deleting previous cache from {}".format(cache))
            if os.path.exists(cache):
                os.remove(cache)
        if not os.path.exists(cache):
            log.info("No cache at {}, computing".format(cache))
            result = compute()
            with gzip.open(cache, "wb") as f:
                pickle.dump(result, f)
            return result
        else:
            log.info("Found cache at {}, decompressing and loading".format(cache))
            with gzip.open(cache, "rb") as f:
                return pickle.load(f)

    def _load_templates(self) -> Dict[str, List[Template]]:
        log.info("Loading templates")
        templates: Dict[str, List[Template]] = defaultdict(list)
        for resource in self.resources:
            for language, new_templates in read_templates(resource.templates)[0].items():
                templates[language].extend(new_templates)

        log.debug("Templates:")
        for lang, lang_templates in templates.items():
            log.debug("\t{}".format(lang))
            for templ in lang_templates:
                log.debug("\t\t{}".format(templ))
        return templates

    def get_locations(self, dataset: str) -> List[str]:
        return list(self.registry.get("{}-data".format(dataset)).all()["location"].unique()) + ["all"]

    def get_datasets(self, language: Optional[str] = None) -> List[str]:
        return list(
            {
                dataset
                for resource in self.resources
                for dataset in resource.supported_data
                if (language is None or resource.supports(language, dataset)) and dataset in self.datasets
            }
        )

    def get_languages(self):
        return list({language for resource in self.resources for language in resource.supported_languages})

    def run_pipeline(
        self, language: str, dataset: str, location: str, location_type: str, previous_location: Optional[str]
    ) -> Tuple[str, str]:
        log.info("Running headline NLG pipeline")
        try:
            headline_lang = "{}-head".format(language)
            headline = self.headline_pipeline.run((location, location_type, dataset, previous_location), headline_lang,)
            log.info("Headline pipeline complete")
        except Exception as ex:
            headline = location
            log.error("%s", ex)

        # TODO: Figure out what DATA is supposed to be here?!
        log.info(
            "Running Body NLG pipeline: "
            "language={}, dataset={}, location={}, location_type={}, previous_location={}".format(
                language, dataset, location, location_type, previous_location
            )
        )
        try:
            body = self.body_pipeline.run((location, location_type, dataset, previous_location), language)
            log.info("Body pipeline complete")
        except NoMessagesForSelectionException:
            log.error("User selection returned no messages")
            body = ERRORS.get(language, {}).get(
                "no-messages-for-selection", "Something went wrong. Please try again later",
            )
        except Exception as ex:
            log.error("%s", ex)
            body = ERRORS.get(language, {}).get("general-error", "Something went wrong. Please try again later")

        return headline, body

    def _set_seed(self, seed_val: Optional[int] = None) -> None:
        log.info("Selecting seed for NLG pipeline")
        if not seed_val:
            seed_val = randint(1, 10000000)
            log.info("No preset seed, using random seed {}".format(seed_val))
        else:
            log.info("Using preset seed {}".format(seed_val))
        self.registry.register("seed", seed_val)
class PipelineTests(unittest.TestCase):

    """Tests of decorators on handlers.
        It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python
        already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators
        used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over
        rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable
        to Pythoh via its decorator syntax
        This test suite is really about proving we can provide equivalent functionality
        We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter
    """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_retry_on_command(self):
        """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """
        self._handler = MyHandlerSupportingRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_exceed_retry_on_command(self):
        """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries"""
        self._handler = MyHandlerBreakingAfterRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        self.assertTrue(exception_raised, "Exepcted the exception to bubble out, when we run out of retries")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_handle_circuit_breaker_on_command(self):
        """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries"""
        self._handler = MyHandlerBreakingCircuitAfterThreeFailures()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        # Now see if the circuit is broken following the failed call
        circuit_broken = False
        try:
            self._commandProcessor.send(self._request)
        except CircuitBrokenError:
            circuit_broken = True

        self.assertTrue(exception_raised, "Exepcted an exception to be raised, when we run out of retries")
        self.assertTrue(circuit_broken, "Expected the circuit to be broken to further calls")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")
Exemple #13
0
from apps.hello.routes import app as HelloApp
from apps.ping.routes import app as PingApp
from apps.mongo.routes import app as MongoApp
from core.registry import Registry
from core.exceptions.application import ContainerError

try:
    """
    You should register all of your apps here.
    """
    apps = Registry()
    apps.register(HelloApp)
    apps.register(PingApp)
    apps.register(MongoApp)

except ContainerError, e:
    """
    I cannot use logging here.  If i use default logging here,
    it will reset all tornado's logging configurations, so i just
    use simple print statement.
    """
    print e.message