Skip to content

Commit

Permalink
places, concepts: correct MEF creations/updates
Browse files Browse the repository at this point in the history
Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Nov 10, 2024
1 parent ad36b25 commit f14bdac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
30 changes: 22 additions & 8 deletions rero_mef/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/places/test_places_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

0 comments on commit f14bdac

Please sign in to comment.