diff --git a/sphinx_scylladb_theme/utils.py b/sphinx_scylladb_theme/utils.py index c6a38e1f2..264f95d90 100644 --- a/sphinx_scylladb_theme/utils.py +++ b/sphinx_scylladb_theme/utils.py @@ -1,3 +1,6 @@ +import requests +import json + def multiversion_regex_builder(versions): """Generates a regex string from a list of versions. @@ -20,3 +23,22 @@ def multiversion_regex_builder(versions): else: versions = [f"^{version}$" for version in versions] return r"\b(" + "|".join(versions) + r")\b" + + +def fetch_multiversion_configuration(url): + """Fetches JSON content from the specified URL and parses it into a Python object + for defining ScyllaDB multiversion data from a remote source. + + :param url: The URL to fetch the JSON data from. + :type url: str + + :return: A dictionary representing the JSON content. + :rtype: dict + """ + try: + response = requests.get(url) + response.raise_for_status() + return response.json() + except Exception as e: + print(f"Error fetching data from {url}: {e}") + return None diff --git a/tests/test_utils.py b/tests/test_utils.py index 84027cd9b..be3d42db9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,6 @@ -from sphinx_scylladb_theme.utils import multiversion_regex_builder +import requests +from sphinx_scylladb_theme.utils import multiversion_regex_builder, fetch_multiversion_configuration +from unittest.mock import patch, MagicMock def test_multiversion_regex_builder_empty(): @@ -19,3 +21,50 @@ def test_multiversion_regex_builder_one_version(): def test_multiversion_regex_builder_many_version(): versions = ["1.0", "2.0"] assert multiversion_regex_builder(versions) == r"\b(^1.0$|^2.0$)\b" + +def test_fetch_multiversion_configuration_success(): + url = "https://example.com/success" + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "tags": [], + "branches": ["master", "branch-5.1", "branch-5.2"], + "latest": "branch-5.2", + } + with patch("requests.get", return_value=mock_response): + result = fetch_multiversion_configuration(url) + assert result == { + "tags": [], + "branches": ["master", "branch-5.1", "branch-5.2"], + "latest": "branch-5.2", + } + + +def test_fetch_multiversion_configuration_http_error(): + url = "https://example.com/http_error" + + mock_response = MagicMock() + mock_response.raise_for_status.side_effect = requests.HTTPError("HTTP Error") + with patch("requests.get", return_value=mock_response): + result = fetch_multiversion_configuration(url) + assert result is None + + +def test_fetch_multiversion_configuration_invalid_json(): + url = "https://example.com/invalid_json" + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.side_effect = ValueError("Invalid JSON") + with patch("requests.get", return_value=mock_response): + result = fetch_multiversion_configuration(url) + assert result is None + + +def test_fetch_multiversion_configuration_invalid_url(): + result = fetch_multiversion_configuration(None) + assert result is None + + result = fetch_multiversion_configuration("") + assert result is None