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"]