|
| 1 | +from copy import deepcopy |
1 | 2 | import json |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
@@ -414,7 +415,8 @@ def test_datacite_publish(metadata_basic: Dict[str, Any]) -> None: |
414 | 415 | metadata_basic.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
415 | 416 |
|
416 | 417 | # creating and validating datacite objects |
417 | | - datacite = to_datacite(metadata_basic, publish=True, validate=True) |
| 418 | + with pytest.warns(DeprecationWarning, match="'publish' is deprecated"): |
| 419 | + datacite = to_datacite(metadata_basic, publish=True, validate=True) |
418 | 420 |
|
419 | 421 | assert datacite == { |
420 | 422 | # 'data': {} |
@@ -549,3 +551,174 @@ def test_datacite_related_res_url( |
549 | 551 | relIdent = datacite["data"]["attributes"]["relatedIdentifiers"][0] |
550 | 552 | assert relIdent["relatedIdentifier"] == related_ident_exp[0].lower() |
551 | 553 | assert relIdent["relatedIdentifierType"] == related_ident_exp[1] |
| 554 | + |
| 555 | + |
| 556 | +@pytest.fixture(scope="function") |
| 557 | +def metadata_basic_event() -> Dict[str, Any]: |
| 558 | + """Create a basic metadata dictionary for testing DOI generation""" |
| 559 | + dandi_id_noprefix = f"000{random.randrange(100, 999)}" |
| 560 | + dandi_id = f"DANDI:{dandi_id_noprefix}" |
| 561 | + version = "0.0.0" |
| 562 | + |
| 563 | + # Create metadata similar to what's used in test_datacite.py |
| 564 | + meta_dict = { |
| 565 | + "identifier": dandi_id, |
| 566 | + "id": f"{dandi_id}/{version}", |
| 567 | + "name": "testing dataset", |
| 568 | + "description": "testing", |
| 569 | + "contributor": [ |
| 570 | + { |
| 571 | + "name": "A_last, A_first", |
| 572 | + "email": "nemo@example.com", |
| 573 | + "roleName": [RoleType("dcite:ContactPerson")], |
| 574 | + "schemaKey": "Person", |
| 575 | + } |
| 576 | + ], |
| 577 | + "license": [LicenseType("spdx:CC-BY-4.0")], |
| 578 | + "url": f"https://dandiarchive.org/dandiset/{dandi_id_noprefix}/{version}", |
| 579 | + "version": version, |
| 580 | + "citation": "A_last, A_first 2021", |
| 581 | + "manifestLocation": [ |
| 582 | + f"https://api.dandiarchive.org/api/dandisets/{dandi_id_noprefix}/versions/draft/assets/" |
| 583 | + ], |
| 584 | + "assetsSummary": { |
| 585 | + "schemaKey": "AssetsSummary", |
| 586 | + "numberOfBytes": 10, |
| 587 | + "numberOfFiles": 1, |
| 588 | + "dataStandard": [{"schemaKey": "StandardsType", "name": "NWB"}], |
| 589 | + }, |
| 590 | + } |
| 591 | + |
| 592 | + return meta_dict |
| 593 | + |
| 594 | + |
| 595 | +def test_event_none_draft_doi(metadata_basic_event: Dict[str, Any]) -> None: |
| 596 | + """Test that event=None creates a Draft DOI (no event in payload)""" |
| 597 | + dandi_id = metadata_basic_event["identifier"] |
| 598 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 599 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 600 | + |
| 601 | + datacite = to_datacite(metadata_basic_event, event=None) |
| 602 | + |
| 603 | + # Check that there is no event attribute |
| 604 | + assert "event" not in datacite["data"]["attributes"] |
| 605 | + |
| 606 | + |
| 607 | +def test_no_event_draft_doi(metadata_basic_event: Dict[str, Any]) -> None: |
| 608 | + """Test that event=None creates a Draft DOI (no event in payload)""" |
| 609 | + dandi_id = metadata_basic_event["identifier"] |
| 610 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 611 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 612 | + |
| 613 | + datacite = to_datacite(metadata_basic_event) |
| 614 | + |
| 615 | + # Check that there is no event attribute |
| 616 | + assert "event" not in datacite["data"]["attributes"] |
| 617 | + |
| 618 | + |
| 619 | +def test_event_publish_findable_doi(metadata_basic_event: Dict[str, Any]) -> None: |
| 620 | + """Test that event="publish" creates a Findable DOI""" |
| 621 | + dandi_id = metadata_basic_event["identifier"] |
| 622 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 623 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 624 | + |
| 625 | + datacite = to_datacite(metadata_basic_event, event="publish") |
| 626 | + |
| 627 | + # Check that event is "publish" |
| 628 | + assert datacite["data"]["attributes"]["event"] == "publish" |
| 629 | + |
| 630 | + |
| 631 | +def test_event_hide_registered_doi(metadata_basic_event: Dict[str, Any]) -> None: |
| 632 | + """Test that event="hide" creates a Registered DOI""" |
| 633 | + dandi_id = metadata_basic_event["identifier"] |
| 634 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 635 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 636 | + |
| 637 | + datacite = to_datacite(metadata_basic_event, event="hide") |
| 638 | + |
| 639 | + # Check that event is "hide" |
| 640 | + assert datacite["data"]["attributes"]["event"] == "hide" |
| 641 | + |
| 642 | + |
| 643 | +def test_invalid_event(metadata_basic_event: Dict[str, Any]) -> None: |
| 644 | + """Test that invalid event values raise ValueError""" |
| 645 | + dandi_id = metadata_basic_event["identifier"] |
| 646 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 647 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 648 | + |
| 649 | + with pytest.raises(ValueError, match="Invalid event value"): |
| 650 | + to_datacite(metadata_basic_event, event="invalid") |
| 651 | + |
| 652 | + |
| 653 | +def test_event_and_publish_conflict(metadata_basic_event: Dict[str, Any]) -> None: |
| 654 | + """Test that using both event and publish parameters raises ValueError""" |
| 655 | + dandi_id = metadata_basic_event["identifier"] |
| 656 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 657 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 658 | + |
| 659 | + with pytest.raises( |
| 660 | + ValueError, match="Cannot use both 'event' and deprecated 'publish'" |
| 661 | + ): |
| 662 | + to_datacite(metadata_basic_event, event="publish", publish=True) |
| 663 | + |
| 664 | + |
| 665 | +def test_deprecated_publish_parameter(metadata_basic_event: Dict[str, Any]) -> None: |
| 666 | + """Test the deprecated publish parameter still works but shows warning""" |
| 667 | + dandi_id = metadata_basic_event["identifier"] |
| 668 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 669 | + metadata_basic_event.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 670 | + |
| 671 | + with pytest.warns(DeprecationWarning, match="'publish' is deprecated"): |
| 672 | + datacite = to_datacite(metadata_basic_event, publish=True) |
| 673 | + |
| 674 | + # Check that event is "publish" despite using the deprecated parameter |
| 675 | + assert datacite["data"]["attributes"]["event"] == "publish" |
| 676 | + |
| 677 | + |
| 678 | +def test_dandiset_doi_url_handling(metadata_basic_event: Dict[str, Any]) -> None: |
| 679 | + """Test that a Dandiset DOI points to the DLP (no version in URL)""" |
| 680 | + dandi_id = metadata_basic_event["identifier"] |
| 681 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 682 | + version = metadata_basic_event["version"] |
| 683 | + |
| 684 | + # Create a copy of the metadata |
| 685 | + meta_dict = deepcopy(metadata_basic_event) |
| 686 | + meta_dict.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 687 | + |
| 688 | + # Override with a Dandiset DOI (no version) |
| 689 | + meta_dict["doi"] = f"10.80507/dandi.{dandi_id_noprefix}" |
| 690 | + |
| 691 | + # Process as a Dandiset DOI |
| 692 | + datacite = to_datacite(meta_dict) |
| 693 | + |
| 694 | + # Verify the DOI is correctly reflected in output |
| 695 | + assert datacite["data"]["attributes"]["doi"] == meta_dict["doi"] |
| 696 | + |
| 697 | + # Verify that the URL in the metadata contains the version ID |
| 698 | + assert f"/{version}" in meta_dict["url"] |
| 699 | + |
| 700 | + # And the URL in the datacite output should use the same URL |
| 701 | + assert datacite["data"]["attributes"]["url"] == meta_dict["url"] |
| 702 | + |
| 703 | + |
| 704 | +def test_doi_formats(metadata_basic_event: Dict[str, Any]) -> None: |
| 705 | + """Test both Dandiset DOI and Version DOI format handling""" |
| 706 | + dandi_id = metadata_basic_event["identifier"] |
| 707 | + dandi_id_noprefix = dandi_id.split(":")[1] |
| 708 | + version = metadata_basic_event["version"] |
| 709 | + |
| 710 | + # Test with Dandiset DOI format |
| 711 | + dandiset_meta = deepcopy(metadata_basic_event) |
| 712 | + dandiset_meta.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 713 | + dandiset_meta["doi"] = f"10.80507/dandi.{dandi_id_noprefix}" |
| 714 | + dandiset_datacite = to_datacite(dandiset_meta) |
| 715 | + |
| 716 | + # Test with Version DOI format |
| 717 | + version_meta = deepcopy(metadata_basic_event) |
| 718 | + version_meta.update(_basic_publishmeta(dandi_id=dandi_id_noprefix)) |
| 719 | + version_meta["doi"] = f"10.80507/dandi.{dandi_id_noprefix}/{version}" |
| 720 | + version_datacite = to_datacite(version_meta) |
| 721 | + |
| 722 | + # Verify DOIs are correctly reflected in output |
| 723 | + assert dandiset_datacite["data"]["attributes"]["doi"] == dandiset_meta["doi"] |
| 724 | + assert version_datacite["data"]["attributes"]["doi"] == version_meta["doi"] |
0 commit comments