Skip to content

Commit 48a8379

Browse files
kenlhluiJR-1991
andauthored
Sanitize version num from string to int by removing non-numeric characters (#58)
* Sanitize version string by removing non-numeric characters * test versions are extracted correctly * test borealis compliance --------- Co-authored-by: Jan Range <30547301+JR-1991@users.noreply.github.com>
1 parent f53165c commit 48a8379

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

easyDataverse/dataverse.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def _version_is_compliant(self) -> bool:
226226
f"URL '{self.server_url}' is not a valid Dataverse installation. Couldn't find version info."
227227
)
228228

229-
major, minor, *_ = response.json()["data"]["version"].split(".")
229+
version = response.json()["data"]["version"]
230+
major, minor = self._extract_major_minor(version)
230231

231232
if int(major) >= 6:
232233
return True
@@ -235,6 +236,26 @@ def _version_is_compliant(self) -> bool:
235236

236237
return False
237238

239+
@staticmethod
240+
def _extract_major_minor(version: str) -> Tuple[int, int]:
241+
"""Extracts the major and minor version numbers from a Dataverse version string."""
242+
try:
243+
major, minor, *_ = version.split(".")
244+
major = "".join(filter(str.isdigit, major))
245+
minor = "".join(filter(str.isdigit, minor))
246+
return int(major), int(minor)
247+
except ValueError:
248+
raise ValueError(f"Version '{version}' is not a valid Dataverse version.")
249+
250+
@staticmethod
251+
def _check_version(major: int, minor: int) -> bool:
252+
"""Checks if the version is compliant."""
253+
if int(major) >= 6:
254+
return True
255+
elif int(major) >= 5 and int(minor) >= 13:
256+
return True
257+
return False
258+
238259
def _fetch_licenses(self) -> Dict[str, License]:
239260
"""Fetches the licenses from the Dataverse installation."""
240261
response = httpx.get(parse.urljoin(str(self.server_url), "/api/licenses"))

tests/integration/test_connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ def test_numeric_namespace(self):
7777
Dataverse(server_url="https://dataverse.harvard.edu")
7878
except ValueError as e:
7979
AssertionError("Failed to parse numeric namespace: " + str(e))
80+
81+
@pytest.mark.integration
82+
def test_version_borealis(self):
83+
"""Tests compatibility with BorealisData, which uses a different versioning scheme."""
84+
Dataverse("https://borealisdata.ca/")

tests/unit/test_dataverse.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,40 @@ def test_invalid_api_token(self):
2121
server_url="http://localhost:8080",
2222
api_token="not a uuid",
2323
)
24+
25+
@pytest.mark.unit
26+
def test_valid_versions(self):
27+
"""Test that the version is compliant"""
28+
cases = [
29+
"6.0",
30+
"6.1",
31+
"5.13",
32+
"5.14",
33+
"v6.0",
34+
"v5.14",
35+
"v5.13-beta",
36+
"6.1.0",
37+
]
38+
39+
for version in cases:
40+
major, minor = Dataverse._extract_major_minor(version)
41+
assert Dataverse._check_version(major, minor)
42+
43+
@pytest.mark.unit
44+
def test_invalid_version(self):
45+
"""Test that an invalid version raises a ValueError"""
46+
with pytest.raises(ValueError):
47+
Dataverse._extract_major_minor("not a version")
48+
49+
@pytest.mark.unit
50+
def test_unsupported_version(self):
51+
"""Test that the version is compliant"""
52+
cases = [
53+
"4.0",
54+
"4.1",
55+
"5.12",
56+
"5.11",
57+
]
58+
for version in cases:
59+
major, minor = Dataverse._extract_major_minor(version)
60+
assert not Dataverse._check_version(major, minor)

0 commit comments

Comments
 (0)