Skip to content

Commit

Permalink
feat: add fetch_multiversion_configuration (#1303)
Browse files Browse the repository at this point in the history
* feat: add fetch_multiversion_configuration

* fix: tests

* fix: tests
  • Loading branch information
dgarcia360 authored Dec 6, 2024
1 parent 03ad082 commit 2027062
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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

0 comments on commit 2027062

Please sign in to comment.