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)
def test_valid_object_has_url_property( self, valid_url_string: str, ) -> None: """Initialized object has `url` property.""" from logtweet.source.adapters import validurl as adapturl url_obj = adapturl.ValidSourceURL(valid_url_string) assert url_obj.url == valid_url_string
def test_valid_url_string_creates_object( self, valid_url_string: str, ) -> None: """Init succeeds with valid url.""" from logtweet.source.adapters import validurl as adapturl url_obj = adapturl.ValidSourceURL(valid_url_string) assert isinstance(url_obj, adapturl.ValidSourceURL)
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)
def test_invalid_url_raises_exception( self, invalid_url_string: str, ) -> None: """ Init fails with exception on invalid URL. Exception contains passed string that is not a URL. """ from logtweet.source.usecases import retrieve as ucretrieve from logtweet.source.adapters import validurl as adapturl with pytest.raises( ucretrieve.SourceValidationError, match=r".*{0}.*".format(invalid_url_string), ): adapturl.ValidSourceURL(invalid_url_string)
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)