From 3d4bdd3dc95ac70f993f7d5a753d83192ca4564e Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Thu, 14 Mar 2024 13:20:11 +0100 Subject: [PATCH] fix missing `extra.profile_id` after unpublish it was overriding extra dict with doi TGA-82 --- server/tests/test_signal_hooks.py | 27 +++++++++++++++++++++++++++ server/tga/signal_hooks.py | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 server/tests/test_signal_hooks.py diff --git a/server/tests/test_signal_hooks.py b/server/tests/test_signal_hooks.py new file mode 100644 index 0000000..ff831ed --- /dev/null +++ b/server/tests/test_signal_hooks.py @@ -0,0 +1,27 @@ +from unittest.mock import patch + +from tga.signal_hooks import generate_doi + + +@patch("tga.signal_hooks._generate_short_unique_id") +def test_generate_doi_existing_doi(mock_generate_short_unique_id): + item = {"extra": {"doi": "existing_doi"}} + updates = {} + generate_doi(None, item, updates) + assert item["extra"]["doi"] == "existing_doi" + assert "extra" not in updates + + item = {"extra": {}} + updates = {} + generate_doi(None, item, updates) + assert item["extra"]["doi"] is not None + assert item["extra"]["doi"] == updates["extra"]["doi"] + + +@patch("tga.signal_hooks._generate_short_unique_id") +def test_generate_doi_new_doi(mock_generate_short_unique_id): + item = {"extra": {}} + updates = {} + generate_doi(None, item, updates) + assert item["extra"]["doi"] is not None + assert item["extra"]["doi"] == updates["extra"]["doi"] diff --git a/server/tga/signal_hooks.py b/server/tga/signal_hooks.py index 9d064f8..a8c0ed9 100644 --- a/server/tga/signal_hooks.py +++ b/server/tga/signal_hooks.py @@ -13,8 +13,10 @@ def generate_doi(_sender, item, updates): """Assign a DOI to this item if one does not already exist""" - item.setdefault("extra", {}) + if item["extra"].get("doi"): + return + updates.setdefault("extra", {}) updates["extra"]["doi"] = updates["extra"].get("doi") or item["extra"].get("doi") or _generate_short_unique_id() item["extra"]["doi"] = updates["extra"]["doi"]