Skip to content

Commit 9780ef5

Browse files
committed
fix(documents): fix subject imports routing condition
* Add field_key condition check before get_mef_link() call in both LOC and SLSP models to ensure the check is applied only for non-imported subjects * Ensures GND subjects are always routed to subjects field * Add comprehensive tests for subject field routing with various sources (GND, RERO, IDREF, LCSH, MeSH) Fixes subject routing for GND subjects in MARC21 to JSON conversion. Co-Authored-by: Peter Weber <peter.weber@rero.ch>
1 parent 85f98ce commit 9780ef5

File tree

4 files changed

+116
-12
lines changed

4 files changed

+116
-12
lines changed

rero_ils/modules/documents/dojson/contrib/marc21tojson/loc/model.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,14 @@ def marc21_to_subjects_6XX(self, key, value):
602602
):
603603
subject["authorized_access_point"] = f"{creator}. {subject['authorized_access_point']}"
604604

605-
if ref := get_mef_link(
606-
bibid=marc21.bib_id,
607-
reroid=marc21.bib_id,
608-
entity_type=data_type,
609-
ids=utils.force_list(value.get("0")),
610-
key=key,
605+
if field_key != "subjects_imported" and (
606+
ref := get_mef_link(
607+
bibid=marc21.bib_id,
608+
reroid=marc21.bib_id,
609+
entity_type=data_type,
610+
ids=utils.force_list(value.get("0")),
611+
key=key,
612+
)
611613
):
612614
subject = {"$ref": ref}
613615
else:

rero_ils/modules/documents/dojson/contrib/marc21tojson/slsp/model.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,14 @@ def marc21_to_subjects_6XX(self, key, value):
428428
".",
429429
):
430430
subject["authorized_access_point"] = f"{creator}. {subject['authorized_access_point']}"
431-
if ref := get_mef_link(
432-
bibid=marc21.bib_id,
433-
reroid=marc21.rero_id,
434-
entity_type=data_type,
435-
ids=utils.force_list(value.get("0")),
436-
key=key,
431+
if field_key != "subjects_imported" and (
432+
ref := get_mef_link(
433+
bibid=marc21.bib_id,
434+
reroid=marc21.rero_id,
435+
entity_type=data_type,
436+
ids=utils.force_list(value.get("0")),
437+
key=key,
438+
)
437439
):
438440
subject = {"$ref": ref}
439441
else:

tests/unit/documents/test_documents_dojson_loc.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,53 @@ def test_marc21_to_subjects_gnd_routing(mock_get_mef_link):
256256
data = marc21.do(marc21json)
257257
assert data.get("subjects") == [{"entity": {"$ref": "https://test.rero.ch/api/places/gnd/4005728-8"}}]
258258
assert data.get("subjects_imported") is None
259+
260+
# Test 11: RERO subject with $0 should NOT use $ref, should use
261+
# authorized_access_point + identifiedBy
262+
marc21xml = """
263+
<record>
264+
<datafield tag="650" ind1=" " ind2="7">
265+
<subfield code="a">Physics</subfield>
266+
<subfield code="0">(RERO)A012345678</subfield>
267+
<subfield code="2">rero</subfield>
268+
</datafield>
269+
</record>
270+
"""
271+
marc21json = create_record(marc21xml)
272+
mock_get_mef_link.return_value = "https://mef.rero.ch/api/concepts/rero/A012345678"
273+
data = marc21.do(marc21json)
274+
assert data.get("subjects_imported") == [
275+
{
276+
"entity": {
277+
"type": "bf:Topic",
278+
"authorized_access_point": "Physics",
279+
"identifiedBy": {"type": "RERO", "value": "A012345678"},
280+
}
281+
}
282+
]
283+
assert data.get("subjects") is None
284+
285+
# Test 12: IDREF subject with $0 should NOT use $ref, should use
286+
# authorized_access_point + identifiedBy
287+
marc21xml = """
288+
<record>
289+
<datafield tag="650" ind1=" " ind2="7">
290+
<subfield code="a">Chemistry</subfield>
291+
<subfield code="0">(IDREF)027390548</subfield>
292+
<subfield code="2">idref</subfield>
293+
</datafield>
294+
</record>
295+
"""
296+
marc21json = create_record(marc21xml)
297+
mock_get_mef_link.return_value = "https://mef.rero.ch/api/concepts/idref/027390548"
298+
data = marc21.do(marc21json)
299+
assert data.get("subjects_imported") == [
300+
{
301+
"entity": {
302+
"type": "bf:Topic",
303+
"authorized_access_point": "Chemistry",
304+
"identifiedBy": {"type": "IdRef", "value": "027390548"},
305+
}
306+
}
307+
]
308+
assert data.get("subjects") is None

tests/unit/documents/test_documents_dojson_slsp.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,53 @@ def test_marc21_to_subjects_gnd_routing(mock_get_mef_link):
603603
data = marc21.do(marc21json)
604604
assert data.get("subjects") == [{"entity": {"$ref": "https://test.rero.ch/api/places/gnd/4005728-8"}}]
605605
assert data.get("subjects_imported") is None
606+
607+
# Test 11: RERO subject with $0 should NOT use $ref, should use
608+
# authorized_access_point + identifiedBy
609+
marc21xml = """
610+
<record>
611+
<datafield tag="650" ind1=" " ind2="7">
612+
<subfield code="a">Physics</subfield>
613+
<subfield code="0">(RERO)A012345678</subfield>
614+
<subfield code="2">rero</subfield>
615+
</datafield>
616+
</record>
617+
"""
618+
marc21json = create_record(marc21xml)
619+
mock_get_mef_link.return_value = "https://mef.rero.ch/api/concepts/rero/A012345678"
620+
data = marc21.do(marc21json)
621+
assert data.get("subjects_imported") == [
622+
{
623+
"entity": {
624+
"type": "bf:Topic",
625+
"authorized_access_point": "Physics",
626+
"identifiedBy": {"type": "RERO", "value": "A012345678"},
627+
}
628+
}
629+
]
630+
assert data.get("subjects") is None
631+
632+
# Test 12: IDREF subject with $0 should NOT use $ref, should use
633+
# authorized_access_point + identifiedBy
634+
marc21xml = """
635+
<record>
636+
<datafield tag="650" ind1=" " ind2="7">
637+
<subfield code="a">Chemistry</subfield>
638+
<subfield code="0">(IDREF)027390548</subfield>
639+
<subfield code="2">idref</subfield>
640+
</datafield>
641+
</record>
642+
"""
643+
marc21json = create_record(marc21xml)
644+
mock_get_mef_link.return_value = "https://mef.rero.ch/api/concepts/idref/027390548"
645+
data = marc21.do(marc21json)
646+
assert data.get("subjects_imported") == [
647+
{
648+
"entity": {
649+
"type": "bf:Topic",
650+
"authorized_access_point": "Chemistry",
651+
"identifiedBy": {"type": "IdRef", "value": "027390548"},
652+
}
653+
}
654+
]
655+
assert data.get("subjects") is None

0 commit comments

Comments
 (0)