Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fetch_multiversion_configuration #1303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sphinx_scylladb_theme/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import requests
import json

def multiversion_regex_builder(versions):
"""Generates a regex string from a list of versions.

Expand All @@ -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
51 changes: 50 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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