|
| 1 | +from enum import Enum |
1 | 2 | import json |
2 | 3 | import os |
3 | 4 | import random |
4 | | -from typing import Any, Dict, Tuple |
| 5 | +from typing import TYPE_CHECKING, Any, Dict, Tuple, cast |
5 | 6 |
|
6 | 7 | from jsonschema import Draft7Validator |
7 | 8 | import pytest |
|
25 | 26 | skipif_no_test_dandiset_metadata_dir, |
26 | 27 | ) |
27 | 28 |
|
28 | | -from .. import _get_datacite_schema, to_datacite |
| 29 | +from .. import _get_datacite_schema, _licenses_to_rights_list, to_datacite |
| 30 | + |
| 31 | + |
| 32 | +class TestLicensesToRightsList: |
| 33 | + """ |
| 34 | + Tests for the `_licenses_to_rights_list()` helper function. |
| 35 | + """ |
| 36 | + |
| 37 | + @pytest.mark.parametrize( |
| 38 | + "licenses", |
| 39 | + [ |
| 40 | + [" "], |
| 41 | + ["bad_license"], |
| 42 | + [" spdx:CC0-1.0"], |
| 43 | + ["spdx:CC0-1.0 "], |
| 44 | + ["spdx: CC0-1.0"], |
| 45 | + ["spdx:CC0-1.0", "foo-license"], |
| 46 | + ], |
| 47 | + ) |
| 48 | + def test_bad_format(self, licenses: list[str]) -> None: |
| 49 | + """ |
| 50 | + Test handling of licenses with a value of bad format |
| 51 | + """ |
| 52 | + if TYPE_CHECKING: |
| 53 | + |
| 54 | + # noinspection PyUnusedLocal |
| 55 | + class BadLicenseType(Enum): |
| 56 | + ... # fmt: skip |
| 57 | + |
| 58 | + else: |
| 59 | + # noinspection PyPep8Naming |
| 60 | + BadLicenseType = Enum( |
| 61 | + "BadLicenseType", |
| 62 | + [(f"M{idx}", license_) for idx, license_ in enumerate(licenses)], |
| 63 | + ) |
| 64 | + |
| 65 | + with pytest.raises(AssertionError, match="not of the expected format"): |
| 66 | + _licenses_to_rights_list(list(BadLicenseType)) |
| 67 | + |
| 68 | + @pytest.mark.parametrize( |
| 69 | + "licenses", |
| 70 | + [ |
| 71 | + ["foo:license"], |
| 72 | + ["bar:license"], |
| 73 | + ["foo:license", "bar:license"], |
| 74 | + ], |
| 75 | + ) |
| 76 | + def test_non_spdx_license(self, licenses: list[str]) -> None: |
| 77 | + """ |
| 78 | + Test handling of licenses not denoted using the `"spdx"` schema, i.e. |
| 79 | + licenses that are not in the SPDX license list at |
| 80 | + https://spdx.org/licenses/ |
| 81 | + """ |
| 82 | + if TYPE_CHECKING: |
| 83 | + # noinspection PyUnusedLocal |
| 84 | + class NonSpdxLicenseType(Enum): |
| 85 | + ... # fmt: skip |
| 86 | + |
| 87 | + else: |
| 88 | + # noinspection PyPep8Naming |
| 89 | + NonSpdxLicenseType = Enum( |
| 90 | + "NonSpdxLicenseType", |
| 91 | + [(f"M{idx}", license_) for idx, license_ in enumerate(licenses)], |
| 92 | + ) |
| 93 | + |
| 94 | + with pytest.raises( |
| 95 | + NotImplementedError, match="Currently only SPDX licenses are supported" |
| 96 | + ): |
| 97 | + _licenses_to_rights_list(list(NonSpdxLicenseType)) |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "licenses", |
| 101 | + [ |
| 102 | + ["spdx:CC0-1.0"], |
| 103 | + ["spdx:CC-BY-4.0"], |
| 104 | + ["spdx:CC0-1.0", "spdx:CC-BY-4.0"], |
| 105 | + ], |
| 106 | + ) |
| 107 | + def test_valid_input(self, licenses: list[str]) -> None: |
| 108 | + """ |
| 109 | + Test handling of valid input |
| 110 | + """ |
| 111 | + if TYPE_CHECKING: |
| 112 | + # noinspection PyUnusedLocal |
| 113 | + class ValidLicenseType(Enum): |
| 114 | + ... # fmt: skip |
| 115 | + |
| 116 | + else: |
| 117 | + # noinspection PyPep8Naming |
| 118 | + ValidLicenseType = Enum( |
| 119 | + "ValidLicenseType", |
| 120 | + [(license_,) * 2 for license_ in licenses], |
| 121 | + ) |
| 122 | + |
| 123 | + expected_rights_list = [ |
| 124 | + { |
| 125 | + "rightsIdentifier": license_.removeprefix("spdx:"), |
| 126 | + "rightsIdentifierScheme": "SPDX", |
| 127 | + "schemeUri": "https://spdx.org/licenses/", |
| 128 | + } |
| 129 | + for license_ in licenses |
| 130 | + ] |
| 131 | + |
| 132 | + assert ( |
| 133 | + _licenses_to_rights_list( |
| 134 | + cast( |
| 135 | + list[LicenseType], |
| 136 | + [ValidLicenseType(license_) for license_ in licenses], |
| 137 | + ) |
| 138 | + ) |
| 139 | + == expected_rights_list |
| 140 | + ) |
29 | 141 |
|
30 | 142 |
|
31 | 143 | def datacite_post(datacite: dict, doi: str) -> None: |
@@ -171,7 +283,7 @@ def test_datacite(dandi_id: str, schema: Any) -> None: |
171 | 283 | ), |
172 | 284 | "rightsList": ( |
173 | 285 | 1, |
174 | | - {"rightsIdentifierScheme": "SPDX", "rightsIdentifier": "CC_BY_40"}, |
| 286 | + {"rightsIdentifierScheme": "SPDX", "rightsIdentifier": "CC-BY-4.0"}, |
175 | 287 | ), |
176 | 288 | "types": ( |
177 | 289 | None, |
@@ -493,7 +605,7 @@ def test_datacite_publish(metadata_basic: Dict[str, Any]) -> None: |
493 | 605 | }, |
494 | 606 | "rightsList": [ |
495 | 607 | { |
496 | | - "rightsIdentifier": "CC_BY_40", |
| 608 | + "rightsIdentifier": "CC-BY-4.0", |
497 | 609 | "rightsIdentifierScheme": "SPDX", |
498 | 610 | "schemeUri": "https://spdx.org/licenses/", |
499 | 611 | } |
|
0 commit comments