From d95c063ea45769fd716899d051adcb0b38dbdaf6 Mon Sep 17 00:00:00 2001 From: Peter Weber Date: Thu, 7 Nov 2024 13:04:13 +0100 Subject: [PATCH] places, concepts: correct MEF creations/updates Co-Authored-by: Peter Weber --- rero_mef/api.py | 30 +++++++++++++++------ tests/ui/places/test_places_api.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/rero_mef/api.py b/rero_mef/api.py index 2540698f..8795a096 100644 --- a/rero_mef/api.py +++ b/rero_mef/api.py @@ -613,11 +613,11 @@ def get_mef_record(mef_cls, name, pid): ) } if ( - not mef_self_association_pid - and not mef_other_association_pid - and mef_other_pid + not bool(mef_self_association_pid) + and not bool(mef_other_association_pid) + and bool(mef_other_pid) ): - # Delete associated ref from MEF an create a new one + # Delete associated ref from MEF and create a new one new_mef_record.pop(association_name) if association_record := association_info[ "record_cls" @@ -631,10 +631,10 @@ def get_mef_record(mef_cls, name, pid): ) actions |= action if ( - mef_self_pid - and not mef_self_association_pid - and not mef_other_pid - and mef_other_association_pid + bool(mef_self_pid) + and not bool(mef_self_association_pid) + and not bool(mef_other_pid) + and bool(mef_other_association_pid) ): # Delete entity from old MEF and add it to new MEF ref = mef_associated_record.pop(association_name) @@ -643,6 +643,20 @@ def get_mef_record(mef_cls, name, pid): ) actions[associated_mef_record.pid] = Action.DELETE_ENTITY new_mef_record[association_name] = ref + if ( + bool(mef_self_pid) + and not bool(mef_self_association_pid) + and bool(mef_other_pid) + and bool(mef_other_association_pid) + ): + # Delete entity from new MEF and add it to old MEF + ref = new_mef_record.pop(self.name) + new_mef_record.replace( + data=new_mef_record, dbcommit=dbcommit, reindex=reindex + ) + actions[new_mef_record.pid] = Action.DELETE_ENTITY + mef_associated_record[self.name] = ref + new_mef_record = mef_associated_record mef_record = new_mef_record.replace( data=new_mef_record, dbcommit=dbcommit, reindex=reindex diff --git a/tests/ui/places/test_places_api.py b/tests/ui/places/test_places_api.py index fc288a85..73e0baef 100644 --- a/tests/ui/places/test_places_api.py +++ b/tests/ui/places/test_places_api.py @@ -205,3 +205,45 @@ def test_create_place_record(app, place_idref_data, place_gnd_data, tmpdir): "pid": "1", "type": "bf:Place", } + + # test idref changes to other gnd + place_gnd_data["pid"] = "TEST2" + gnd_record_2 = PlaceGndRecord.create( + data=place_gnd_data, dbcommit=True, reindex=True, delete_pid=False + ) + assert gnd_record_2.pid == "TEST2" + m_gnd_record_2, m_action = gnd_record_2.create_or_update_mef( + dbcommit=True, reindex=True + ) + assert m_action == {"4": Action.CREATE} + assert m_gnd_record_2 == { + "$schema": "https://mef.rero.ch/schemas/places_mef/mef-place-v0.0.1.json", + "gnd": {"$ref": "https://mef.rero.ch/api/places/gnd/TEST2"}, + "pid": "4", + "type": "bf:Place", + } + + for identified_by in idref_record["identifiedBy"]: + if identified_by.get("source") == "GND": + identified_by["value"] = "(DE-101)TEST2" + idref_record = idref_record.update(data=idref_record, dbcommit=True, reindex=True) + + PlaceMefRecord.flush_indexes() + m_idref_record, m_action = idref_record.create_or_update_mef( + dbcommit=True, reindex=True + ) + assert m_action == {"1": Action.DELETE_ENTITY, "4": Action.UPDATE} + assert m_idref_record == { + "$schema": f"{SCHEMA_URL}/mef-place-v0.0.1.json", + "idref": {"$ref": "https://mef.rero.ch/api/places/idref/271330163"}, + "gnd": {"$ref": "https://mef.rero.ch/api/places/gnd/TEST2"}, + "pid": "4", + "type": "bf:Place", + } + m_gnd_record = PlaceMefRecord.get_record_by_pid(m_gnd_record.pid) + assert m_gnd_record == { + "$schema": f"{SCHEMA_URL}/mef-place-v0.0.1.json", + "gnd": {"$ref": "https://mef.rero.ch/api/places/gnd/TEST"}, + "pid": "1", + "type": "bf:Place", + }