From 620c2628bfa4200c813c33bfd8140f2efc952245 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Fri, 29 Nov 2024 07:39:45 +0000 Subject: [PATCH 1/3] feat: add fetch_multiversion_configuration --- sphinx_scylladb_theme/utils.py | 21 ++++++++++++++ tests/test_utils.py | 50 +++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/sphinx_scylladb_theme/utils.py b/sphinx_scylladb_theme/utils.py index c6a38e1f2..e11cb423a 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,21 @@ 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) + 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..ba60afead 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ -from sphinx_scylladb_theme.utils import multiversion_regex_builder +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 +20,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 From 99cef562e24114e8c41c45023ac62c9de3a3009c Mon Sep 17 00:00:00 2001 From: David Garcia Date: Fri, 29 Nov 2024 07:42:31 +0000 Subject: [PATCH 2/3] fix: tests --- tests/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index ba60afead..be3d42db9 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,4 @@ +import requests from sphinx_scylladb_theme.utils import multiversion_regex_builder, fetch_multiversion_configuration from unittest.mock import patch, MagicMock From 8bd64d447d9fcd7d978552a2c2c84e83860286c6 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Fri, 29 Nov 2024 07:45:37 +0000 Subject: [PATCH 3/3] fix: tests --- sphinx_scylladb_theme/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sphinx_scylladb_theme/utils.py b/sphinx_scylladb_theme/utils.py index e11cb423a..264f95d90 100644 --- a/sphinx_scylladb_theme/utils.py +++ b/sphinx_scylladb_theme/utils.py @@ -37,6 +37,7 @@ def fetch_multiversion_configuration(url): """ try: response = requests.get(url) + response.raise_for_status() return response.json() except Exception as e: print(f"Error fetching data from {url}: {e}")