diff --git a/cds_ils/importer/XMLRecordToJson.py b/cds_ils/importer/XMLRecordToJson.py index b4229985a..88720b47d 100644 --- a/cds_ils/importer/XMLRecordToJson.py +++ b/cds_ils/importer/XMLRecordToJson.py @@ -15,7 +15,7 @@ from invenio_records.api import Record from cds_ils.importer import marc21 -from cds_ils.importer.errors import LossyConversion, RecordModelMissing +from cds_ils.importer.errors import LossyConversion, UnrecognisedImportMediaType from cds_ils.importer.handlers import xml_import_handlers @@ -54,14 +54,17 @@ def dump(self): is_deletable = False init_fields = {} - if "im" in marc_record.get("leader", []): + leader_tag = marc_record.get("leader", []) + if "am" in leader_tag: + init_fields.update({"_eitem": {"_type": "e-book"}}) + elif "im" in leader_tag or "jm" in leader_tag: init_fields.update({"_eitem": {"_type": "audiobook"}}) - elif "gm" in marc_record.get("leader", []): + elif "gm" in leader_tag: init_fields.update( {"document_type": "MULTIMEDIA", "_eitem": {"_type": "video"}} ) else: - init_fields.update({"_eitem": {"_type": "e-book"}}) + raise UnrecognisedImportMediaType(leader_tag) # MARCXML -> JSON fields translation val = self.dojson_model.do( marc_record, diff --git a/cds_ils/importer/errors.py b/cds_ils/importer/errors.py index bc38b15d9..780ac1172 100644 --- a/cds_ils/importer/errors.py +++ b/cds_ils/importer/errors.py @@ -81,6 +81,12 @@ class RecordModelMissing(CDSImporterException): message = "[Record did not match any available model]" +class UnrecognisedImportMediaType(CDSImporterException): + """Unrecognised record media type exception.""" + + message = "Record media type is not recognised." + + class UnexpectedValue(CDSImporterException): """The corresponding value is unexpected.""" diff --git a/tests/importer/data/safari_audiobook2.xml b/tests/importer/data/safari_audiobook2.xml new file mode 100644 index 000000000..0a8084200 --- /dev/null +++ b/tests/importer/data/safari_audiobook2.xml @@ -0,0 +1,139 @@ + + + + 00000njm a22000007i 4500 + on1417409648 + OCoLC + 20240125213018.0 + m o h + sz zunnnnnuneu + cr nnannnuuuuu + 240117s2024 xx nnnn o z n eng d + + ORMDA + eng + rda + pn + ORMDA + OCLCO + + + 9781663731913 + (electronic audio bk.) + + + 1663731918 + (electronic audio bk.) + + + (OCoLC)1417409648 + + + 9781663731913 + O'Reilly Media + + + BF575.A6 + + + 152.4/6 + 23/eng/20240117 + + + MAIN + + + Managing your anxiety / + Harvard Business Review. + + + [First edition]. + + + [Place of publication not identified] : + Ascent Audio, + 2024. + + + 1 online resource (1 sound file (1 hr., 27 min.)) + + + 012700 + + + spoken word + spw + rdacontent + + + computer + c + rdamedia + + + online resource + cr + rdacarrier + + + digital + rdatr + + + audio file + rdaft + + + Read by Steve Marvel, Teri Schnaubelt. + + + Make anxiety work for you. Work is stressful: We race to meet deadlines. We extend ourselves to return favors for colleagues. We set ambitious goals for ourselves and our teams. We measure ourselves against metrics, our competitors, and sometimes, our colleagues. Some of us even go beyond tangible metrics to internalize stress and fear of missing the mark-ruminating over presentations that didn't go according to plan, imagining worst-case scenarios, or standing frozen, paralyzed by perfectionism. But hypervigilance, worry, and catastrophizing don't have to hold you back at work. When channeled thoughtfully, anxiety can motivate us to be more resourceful, productive, and creative. It can break down barriers and create new bonds with our colleagues. Managing Your Anxiety will help you distinguish stress from anxiety, learn what anxiety looks like for you, understand it, and respond to it with self-compassion at work. With the latest psychological research and practical advice from leading experts, you'll learn how to recognize how your anxiety manifests itself; manage it in small, day-to-day moments and in more challenging times; experiment and find a mindfulness practice that works for you; and build a support infrastructure to help you manage your anxiety over the long term. + + + Online resource; title from title details screen (O'Reilly, viewed January 17, 2024). + + + O'Reilly + O'Reilly Online Learning Platform: Academic Edition (SAML SSO Access) + + + Anxiety. + + + Job stress. + + + Self-care, Health. + + + AutotheĢrapie. + + + Audiobooks. + lcgft + + + Livres audio. + rvmgf + + + Marvel, Steve, + narrator. + + + Schnaubelt, Teri, + narrator. + + + Harvard Business Review Press, + issuing body. + + + https://learning.oreilly.com/library/view/-/9781663731913/?ar + + + 92 + CHCER + + + diff --git a/tests/importer/data/safari_record_broken_mediatype.xml b/tests/importer/data/safari_record_broken_mediatype.xml new file mode 100644 index 000000000..f20672643 --- /dev/null +++ b/tests/importer/data/safari_record_broken_mediatype.xml @@ -0,0 +1,152 @@ + + +01538nbla a2200397 a 4500 +9780814415467 +OCoLC +20200404234619.1 +m o d +cr zn||||||||| +110609s2009 enka ob 001 0 eng d + + 765144159 + 961649869 + 962617255 + + + 9780814415467 + + + 9781801073141 (pbk.) + electronic publication + + + 9780814415474 + (electronic bk. ; + oBook) + + + (OCoLC)9780814415467 + (OCoLC)765144159 + + + CtWfDGI + eng + pn + STF + + + eng + + + ita + + + HD69.P75 + M868 2009eb + + + HD69.P86 + + + 658.4/04 + 22 + + + Falcone, Paul, + author. + + + Murray, Andy. + + + 101 Sample Write-Ups for Documenting Employee Performance Problems: + A Guide to Progressive Discipline and Termination + [electronic resource] / + Falcone, Paul. + + + 2nd edition + + + London : + TSO (The Stationary Office), + 2009. + + + 1 online resource (399 pages) + + + text + txt + rdacontent + + + computer + c + rdamedia + + + online resource + cr + rdacarrier + + + text file + + + 35.00 + + + --For dummies + 1 + + + A complete tool kit for handling disciplinary problems in a fair, responsible, and legally defensible way. + + + Electronic reproduction. + Boston, MA : + Safari, + Available via World Wide Web. + 2010. + + + Mode of access: World Wide Web. + + + 2010 + + + Made available through: Safari, an O'Reilly Media Company. + + + Online resource; Title from title page (viewed March 31, 2010) + + + Project management. + + + Gestion de projet. + + + Project other. + fast + (OCoLC)fst01078797 + + + Electronic books. + local + + + Bennett, Nigel. + aut + + + Safari, an O'Reilly Media Company. + + + Connect to this resource online + https://learning.oreilly.com/library/view/-/9780814415467/?ar + + + diff --git a/tests/importer/test_safari_dojson.py b/tests/importer/test_safari_dojson.py index 9311839ea..013abdf50 100644 --- a/tests/importer/test_safari_dojson.py +++ b/tests/importer/test_safari_dojson.py @@ -1,7 +1,9 @@ import os +import pytest from cds_dojson.marc21.utils import create_record +from cds_ils.importer.errors import UnrecognisedImportMediaType from cds_ils.importer.providers.safari.safari import model marcxml = ( @@ -14,14 +16,17 @@ def check_transformation(marcxml_body, json_body): """Check transformation.""" blob = create_record(marcxml.format(marcxml_body)) init_fields = {} - if "im" in blob.get("leader", []): + leader_tag = blob.get("leader", []) + if "am" in leader_tag: + init_fields.update({"_eitem": {"_type": "e-book"}}) + elif "im" in leader_tag or "jm" in leader_tag: init_fields.update({"_eitem": {"_type": "audiobook"}}) - elif "gm" in blob.get("leader", []): + elif "gm" in leader_tag: init_fields.update( {"document_type": "MULTIMEDIA", "_eitem": {"_type": "video"}} ) else: - init_fields.update({"_eitem": {"_type": "e-book"}}) + raise UnrecognisedImportMediaType(leader_tag) record = {} record.update(**model.do(blob, ignore_missing=True, init_fields=init_fields)) @@ -462,6 +467,87 @@ def test_safari_audiobook(app): ) +def test_safari_audiobook2(app): + """Test audiobook import.""" + dirname = os.path.join(os.path.dirname(__file__), "data") + with open(os.path.join(dirname, "safari_audiobook2.xml"), "r") as fp: + example = fp.read() + with app.app_context(): + check_transformation( + example, + { + "_eitem": { + "_type": "audiobook", + "urls": [ + { + "description": "audiobook", + "value": "https://learning.oreilly.com/library/view/-/9781663731913/?ar", + } + ], + }, + "abstract": "Make anxiety work for you. Work is stressful: We race to meet deadlines. We extend ourselves to return favors for colleagues. We set ambitious goals for ourselves and our teams. We measure ourselves against metrics, our competitors, and sometimes, our colleagues. Some of us even go beyond tangible metrics to internalize stress and fear of missing the mark-ruminating over presentations that didn't go according to plan, imagining worst-case scenarios, or standing frozen, paralyzed by perfectionism. But hypervigilance, worry, and catastrophizing don't have to hold you back at work. When channeled thoughtfully, anxiety can motivate us to be more resourceful, productive, and creative. It can break down barriers and create new bonds with our colleagues. Managing Your Anxiety will help you distinguish stress from anxiety, learn what anxiety looks like for you, understand it, and respond to it with self-compassion at work. With the latest psychological research and practical advice from leading experts, you'll learn how to recognize how your anxiety manifests itself; manage it in small, day-to-day moments and in more challenging times; experiment and find a mindfulness practice that works for you; and build a support infrastructure to help you manage your anxiety over the long term.", + "agency_code": "OCoLC", + "alternative_identifiers": [ + {"scheme": "SAFARI", "value": "on1417409648"} + ], + "authors": [ + { + "full_name": "Marvel, Steve", + "roles": ["AUTHOR"], + "type": "PERSON", + }, + { + "full_name": "Schnaubelt, Teri", + "roles": ["AUTHOR"], + "type": "PERSON", + }, + ], + "document_type": "BOOK", + "edition": "1st", + "identifiers": [ + { + "material": "AUDIOBOOK", + "scheme": "ISBN", + "value": "9781663731913", + }, + {"material": "AUDIOBOOK", "scheme": "ISBN", "value": "1663731918"}, + ], + "imprint": { + "place": "Place of publication not identified", + "publisher": "Ascent Audio", + }, + "keywords": [ + {"source": "SAFARI", "value": "Anxiety"}, + {"source": "SAFARI", "value": "Job stress"}, + {"source": "SAFARI", "value": "Self-care, Health"}, + {"source": "SAFARI", "value": "AutotheĢrapie"}, + ], + "languages": ["ENG"], + "provider_recid": "on1417409648", + "publication_year": "2024", + "subjects": [ + {"scheme": "LOC", "value": "BF575.A6"}, + {"scheme": "DEWEY", "value": "152.4/6"}, + ], + "title": "Managing your anxiety", + }, + ) + + +def test_safari_record_broken_mediatype(app): + """Test broken mediatype "bla".""" + dirname = os.path.join(os.path.dirname(__file__), "data") + with open(os.path.join(dirname, "safari_record_broken_mediatype.xml"), "r") as fp: + example = fp.read() + + with app.app_context(): + with pytest.raises(UnrecognisedImportMediaType): + check_transformation( + example, + {}, + ) + + def test_safari_video(app): """Test video import.""" dirname = os.path.join(os.path.dirname(__file__), "data")