diff --git a/easyDataverse/dataverse.py b/easyDataverse/dataverse.py index 8d81bf5..8775f0a 100644 --- a/easyDataverse/dataverse.py +++ b/easyDataverse/dataverse.py @@ -226,7 +226,8 @@ def _version_is_compliant(self) -> bool: f"URL '{self.server_url}' is not a valid Dataverse installation. Couldn't find version info." ) - major, minor, *_ = response.json()["data"]["version"].split(".") + version = response.json()["data"]["version"] + major, minor = self._extract_major_minor(version) if int(major) >= 6: return True @@ -235,6 +236,26 @@ def _version_is_compliant(self) -> bool: return False + @staticmethod + def _extract_major_minor(version: str) -> Tuple[int, int]: + """Extracts the major and minor version numbers from a Dataverse version string.""" + try: + major, minor, *_ = version.split(".") + major = "".join(filter(str.isdigit, major)) + minor = "".join(filter(str.isdigit, minor)) + return int(major), int(minor) + except ValueError: + raise ValueError(f"Version '{version}' is not a valid Dataverse version.") + + @staticmethod + def _check_version(major: int, minor: int) -> bool: + """Checks if the version is compliant.""" + if int(major) >= 6: + return True + elif int(major) >= 5 and int(minor) >= 13: + return True + return False + def _fetch_licenses(self) -> Dict[str, License]: """Fetches the licenses from the Dataverse installation.""" response = httpx.get(parse.urljoin(str(self.server_url), "/api/licenses")) diff --git a/tests/integration/test_connection.py b/tests/integration/test_connection.py index dbd1b58..1b623d9 100644 --- a/tests/integration/test_connection.py +++ b/tests/integration/test_connection.py @@ -77,3 +77,8 @@ def test_numeric_namespace(self): Dataverse(server_url="https://dataverse.harvard.edu") except ValueError as e: AssertionError("Failed to parse numeric namespace: " + str(e)) + + @pytest.mark.integration + def test_version_borealis(self): + """Tests compatibility with BorealisData, which uses a different versioning scheme.""" + Dataverse("https://borealisdata.ca/") diff --git a/tests/unit/test_dataverse.py b/tests/unit/test_dataverse.py index f5f2bb0..6074a98 100644 --- a/tests/unit/test_dataverse.py +++ b/tests/unit/test_dataverse.py @@ -21,3 +21,40 @@ def test_invalid_api_token(self): server_url="http://localhost:8080", api_token="not a uuid", ) + + @pytest.mark.unit + def test_valid_versions(self): + """Test that the version is compliant""" + cases = [ + "6.0", + "6.1", + "5.13", + "5.14", + "v6.0", + "v5.14", + "v5.13-beta", + "6.1.0", + ] + + for version in cases: + major, minor = Dataverse._extract_major_minor(version) + assert Dataverse._check_version(major, minor) + + @pytest.mark.unit + def test_invalid_version(self): + """Test that an invalid version raises a ValueError""" + with pytest.raises(ValueError): + Dataverse._extract_major_minor("not a version") + + @pytest.mark.unit + def test_unsupported_version(self): + """Test that the version is compliant""" + cases = [ + "4.0", + "4.1", + "5.12", + "5.11", + ] + for version in cases: + major, minor = Dataverse._extract_major_minor(version) + assert not Dataverse._check_version(major, minor)