Example #1
0
        def actual_factory(
            status_code: int,
            page_content: str,
        ) -> "adaptonline.OnlineSourceContentRetriever":
            """
            Create a valid online source object for the running mock websever.

            The mock server's url is available on the returned object in the `url`
            property.

            Parameters
            ----------
            status_code: int
                Status code that the mock web server is responding with to the
                requests.
            page_content: str
                Page content that the mock web server is responding with to the
                requests.

            Returns
            -------
            adaptonline.OnlineSourceContentRetriever
                OnlineSourceContentRetriever instance, configure to retrieve
                content from the mock server.

            """
            request_get_handler = request_get_handler_class_factory(
                status_code,
                page_content,
            )
            mock_server = mock_server_factory(request_get_handler)
            url = "http://{0}:{1}/".format(mock_server[0], mock_server[1])
            validurl = adapturl.ValidSourceURL(url)
            return adaptonline.OnlineSourceContentRetriever(validurl)
Example #2
0
    def test_type_error_if_input_not_specific_enough(self) -> None:
        """
        Type error if input not specific enough.

        If the input is subclass of `ucretrieve.AbstractValidSource` but not of
        `adaptonline.AbstractValidOnlineSource` the type error is also thrown.

        """
        from logtweet.source.usecases import retrieve as ucretrieve

        class NotSpecificEnoughSource(ucretrieve.AbstractValidSource):
            @staticmethod
            def is_valid(_: str) -> bool:
                return True

        not_specific_enough_source = NotSpecificEnoughSource("stuff")
        from logtweet.source.adapters import onlineretriever as adaptonline

        with pytest.raises(
                TypeError,
                match=r"Expected .*, got .*",
        ):
            adaptonline.OnlineSourceContentRetriever(
                not_specific_enough_source,  # type: ignore
            )
Example #3
0
    def online_source_content_retriever(
        self,
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> "adaptonline.OnlineSourceContentRetriever":
        """
        Create an `adaptonline.OnlineSourceContentRetriever` object.

        This is a convenience fixture to instantiate an
        `adaptonline.OnlineSourceContentRetriever` object from a meaningless valid online
        source. The source is not actually validated.

        Parameters
        ----------
        valid_online_source_factory : typing.Callable[[str], "adaptonline.AbstractValidOnlineSource"]
            Factory function fixture to create a valid online source instance
            from a given source string.

        Returns
        -------
        adaptonline.OnlineSourceContentRetriever
            The `adaptonline.OnlineSourceContentRetriever` object is instantiated with a
            valid online source, that is created with the
            `valid_online_source_factory`. The valid source is created with a
            meaningless string passed to it.

        """
        from logtweet.source.adapters import onlineretriever as adaptonline

        valid_online_source = valid_online_source_factory("not important")
        online_source_content_retirever = adaptonline.OnlineSourceContentRetriever(
            valid_online_source, )

        return online_source_content_retirever
Example #4
0
    def test_exception_if_404_response(
        self,
        request_get_handler_class_factory: typing.Callable[
            [int, str], typing.Type["httpserver.BaseHTTPRequestHandler"]],
        mock_server_factory: typing.Callable[
            [typing.Type["httpserver.BaseHTTPRequestHandler"]],
            typing.Tuple[str, int]],
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """Raises exception when server responds with 404 status."""
        defined_status_code = 404
        request_handler = request_get_handler_class_factory(  # type: ignore
            defined_status_code, )
        mock_server = mock_server_factory(request_handler)
        source_string = "http://{0}:{1}/".format(mock_server[0],
                                                 mock_server[1])
        # Create a valid online source without actually validating
        valid_online_source = valid_online_source_factory(source_string)
        from logtweet.source.adapters import onlineretriever as adaptonline
        online_source_content_retriever = adaptonline.OnlineSourceContentRetriever(
            valid_online_source, )
        from logtweet.source.adapters import onlineretriever as adaptonline

        with pytest.raises(adaptonline.HTTPStatusError):
            online_source_content_retriever.get_content()
Example #5
0
    def test_sucessful_content_retrieval(
        self,
        request_get_handler_class_factory: typing.Callable[
            [int, str], typing.Type["httpserver.BaseHTTPRequestHandler"]],
        mock_server_factory: typing.Callable[
            [typing.Type["httpserver.BaseHTTPRequestHandler"]],
            typing.Tuple[str, int]],
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """Content from mock server is returned."""
        defined_content = "The content"
        defined_status_code = 200
        request_handler = request_get_handler_class_factory(
            defined_status_code,
            defined_content,
        )
        mock_server = mock_server_factory(request_handler)
        source_string = "http://{0}:{1}/".format(mock_server[0],
                                                 mock_server[1])
        # Create a valid online source without actually validating
        valid_online_source = valid_online_source_factory(source_string)
        from logtweet.source.adapters import onlineretriever as adaptonline
        online_source_content_retriever = adaptonline.OnlineSourceContentRetriever(
            valid_online_source, )

        actual_content = online_source_content_retriever.get_content()

        assert actual_content == defined_content
Example #6
0
    def test_valid_source_avaliable_on_instance(
        self,
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """Passed valid source object is available on the instance."""
        valid_online_source = valid_online_source_factory("not important")
        from logtweet.source.adapters import onlineretriever as adaptonline

        instance = adaptonline.OnlineSourceContentRetriever(
            valid_online_source)

        assert instance.valid_source == valid_online_source
Example #7
0
    def test_type_error_if_init_input_wrong_type(self) -> None:
        """Raise TypeError if init input parameter not right type."""
        class NotTheRightType(object):
            pass

        wrong_type_input = NotTheRightType()
        from logtweet.source.adapters import onlineretriever as adaptonline

        with pytest.raises(
                TypeError,
                match=r"Expected .*, got .*",
        ):
            adaptonline.OnlineSourceContentRetriever(
                wrong_type_input)  # type: ignore
Example #8
0
    def test_exception_when_server_not_available(
        self,
        free_port: int,
    ) -> None:
        """Raises exception when online source not available."""
        url = "http://localhost:{0}".format(free_port)
        from logtweet.source.adapters import validurl as adapturl
        from logtweet.source.adapters import onlineretriever as adaptonline
        validurl = adapturl.ValidSourceURL(url)
        online_source_retriever = adaptonline.OnlineSourceContentRetriever(
            validurl, )
        from logtweet.source.usecases import retrieve as ucretrieve

        with pytest.raises(ucretrieve.SourceContentRetrievalError):
            ucretrieve.get_log_content_from_source(online_source_retriever)
Example #9
0
    def test_valid_source_has_url_property(
        self,
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """Valid source on instance has `url` property."""
        source_string = "not important"
        valid_online_source = valid_online_source_factory(source_string)
        from logtweet.source.adapters import onlineretriever as adaptonline
        instance = adaptonline.OnlineSourceContentRetriever(
            valid_online_source)

        url = instance.valid_source.url

        assert url == source_string
Example #10
0
    def test_init_success(
        self,
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """
        Successful init without error.

        Passing an instance of a `adaptonline.AbstractValidOnlineSource` subclass leads
        to successful init of `adaptonline.OnlineSourceContentRetriever`.

        """
        valid_online_source = valid_online_source_factory("not important")
        from logtweet.source.adapters import onlineretriever as adaptonline

        adaptonline.OnlineSourceContentRetriever(valid_online_source)
Example #11
0
    def test_expection_if_server_not_avilable(
        self,
        free_port: int,
        valid_online_source_factory: typing.Callable[
            [str], "adaptonline.AbstractValidOnlineSource"],
    ) -> None:
        """Raises exception if server is not available."""
        source_string = "http://localhost:{0}/".format(free_port)
        # Create a valid online source without actually validating
        valid_online_source = valid_online_source_factory(source_string)
        from logtweet.source.adapters import onlineretriever as adaptonline
        online_source_content_retriever = adaptonline.OnlineSourceContentRetriever(
            valid_online_source, )
        from logtweet.source.adapters import onlineretriever as adaptonline

        with pytest.raises(adaptonline.RequestError):
            online_source_content_retriever.get_content()
Example #12
0
def get_log_content_from_source(source_string: str) -> str:
    """
    Return log content from the source identified by the source string.

    Parameters
    ----------
    source_string : str
        String defining the source from which to retrieve the content.
        Currently, the source string has to be a valid url. But, this will
        be extended to allow local file paths in the future.

    Returns
    -------
    str
        Content string retrieved from the source.

    """
    # TODO: Allow source to be local file. Handle the two possible types.
    #       To allow the two source types, detect source type, then create the
    #       appropriate retriever object and call the use case with the
    #       created retriever.
    validurl = adapturl.ValidSourceURL(source_string)
    retriever = adaptonline.OnlineSourceContentRetriever(validurl)
    return ucretrieve.get_log_content_from_source(retriever)