From 657dd39c1b9028299878a880fa44d11990be57f3 Mon Sep 17 00:00:00 2001 From: Saksham Arora Date: Mon, 27 May 2024 16:45:06 +0200 Subject: [PATCH] importer: Fix Audiobook updating test --- cds_ils/importer/documents/importer.py | 2 +- cds_ils/importer/eitems/importer.py | 2 ++ cds_ils/importer/series/importer.py | 2 +- .../data/documents_with_audiobook.json | 18 +++++----------- tests/importer/data/existing_documents.json | 15 +++++++++++++ tests/importer/data/existing_eitems.json | 21 +++++++++++++++++++ tests/importer/test_importer.py | 10 +++++---- 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/cds_ils/importer/documents/importer.py b/cds_ils/importer/documents/importer.py index cf735b862..f823cbd48 100644 --- a/cds_ils/importer/documents/importer.py +++ b/cds_ils/importer/documents/importer.py @@ -202,7 +202,7 @@ def _update_field_alternative_identifiers(self, matched_document): @staticmethod def _normalize_title(title): """Return a normalized title.""" - t = " ".join(title.lower().split()) + t = " ".join((title or "").lower().split()) return t.strip() def update_document(self, matched_document): diff --git a/cds_ils/importer/eitems/importer.py b/cds_ils/importer/eitems/importer.py index 91844a864..d752e872a 100644 --- a/cds_ils/importer/eitems/importer.py +++ b/cds_ils/importer/eitems/importer.py @@ -225,6 +225,7 @@ def _apply_url_login(self, eitem): def eitems_search(self, matched_document): """Search items for given document.""" document_pid = matched_document["pid"] + print(self.eitem_json.get("_type", "E-BOOK").upper()) # get eitems for current provider search = get_eitems_for_document_by_provider( @@ -236,6 +237,7 @@ def import_eitem_action(self, search): """Determine import action.""" # If found more than 0 then update for the same type & provider, if eitem with same type & provider isn't found then do create hits_count = search.count() + print(hits_count) if hits_count == 1: self.action = "update" elif hits_count == 0: diff --git a/cds_ils/importer/series/importer.py b/cds_ils/importer/series/importer.py index f937c1f78..d18706802 100644 --- a/cds_ils/importer/series/importer.py +++ b/cds_ils/importer/series/importer.py @@ -113,7 +113,7 @@ def _update_field_identifiers(self, matched_series, json_series): @staticmethod def _normalize_title(title): """Return a normalized title.""" - t = " ".join(title.lower().split()) + t = " ".join((title or "").lower().split()) # remove `series` or `ser` at the end of the title # `International Series of Numerical Mathematics series` # or `International series of Numerical mathematics ser` diff --git a/tests/importer/data/documents_with_audiobook.json b/tests/importer/data/documents_with_audiobook.json index f09905996..233c369dc 100644 --- a/tests/importer/data/documents_with_audiobook.json +++ b/tests/importer/data/documents_with_audiobook.json @@ -53,23 +53,16 @@ } ] }, - "abstract": "This is an abstract that you understand.", + "abstract": "Check if existing audiobook gets updated if providers are same", "agency_code": "OCoLC", "alternative_identifiers": [ { "scheme": "SAFARI", "value": "on1417409123" } ], - "authors": [ - { - "full_name": "Bros, Warner", - "roles": ["AUTHOR"], - "type": "ORGANISATION" - } - ], + "authors": [{ "full_name": "Doe, John" }], "document_type": "BOOK", "edition": "1st", "identifiers": [ - { "material": "AUDIOBOOK", "scheme": "ISBN", "value": "9781663731913" }, - { "material": "AUDIOBOOK", "scheme": "ISBN", "value": "1663731918" } + { "material": "AUDIOBOOK", "scheme": "ISBN", "value": "9999999999" } ], "imprint": { "place": "Place of publication not known", @@ -77,8 +70,7 @@ }, "keywords": [ { "source": "SAFARI", "value": "Abstracts" }, - { "source": "SAFARI", "value": "Books" }, - { "source": "SAFARI", "value": "Self-care, Health" } + { "source": "SAFARI", "value": "Books" } ], "languages": ["ENG"], "provider_recid": "on1417409123", @@ -87,6 +79,6 @@ { "scheme": "LOC", "value": "GF575.B6" }, { "scheme": "DEWEY", "value": "222.4/6" } ], - "title": "Understanding abstracts" + "title": "New Book: Test Audiobook Updating" } ] diff --git a/tests/importer/data/existing_documents.json b/tests/importer/data/existing_documents.json index 119812a76..8b6b599dc 100644 --- a/tests/importer/data/existing_documents.json +++ b/tests/importer/data/existing_documents.json @@ -194,5 +194,20 @@ "publication_year": "2024", "identifiers": [{ "scheme": "ISBN", "value": "9876543210" }], "title": "Understanding abstracts" + }, + { + "$schema": "https://127.0.0.1:5000/schemas/documents/document-v1.0.0.json", + "created_by": { "type": "script", "value": "test" }, + "pid": "docid-11", + "title": "New Book: Test Audiobook Updating", + "authors": [{ "full_name": "Frank Bidart" }], + "abstract": "This is an abstract", + "edition": "3", + "identifiers": [ + { "material": "AUDIOBOOK", "scheme": "ISBN", "value": "9999999999" } + ], + "keywords": [{ "source": "X", "value": "Patata" }], + "document_type": "BOOK", + "publication_year": "1950" } ] diff --git a/tests/importer/data/existing_eitems.json b/tests/importer/data/existing_eitems.json index 169935d3c..16c2938e7 100644 --- a/tests/importer/data/existing_eitems.json +++ b/tests/importer/data/existing_eitems.json @@ -196,5 +196,26 @@ "login_required": false } ] + }, + { + "pid": "eitemid-11", + "created_by": {"type": "import", "value": "safari"}, + "document_pid": "docid-11", + "eitem_type": "AUDIOBOOK", + "internal_notes": "AUDIOBOOK TO MODIFY", + "description": "Description of the electronic item", + "open_access": false, + "urls": [ + { + "description": "Protected URL", + "value": "http://protected-cds-ils.ch/", + "login_required": true + }, + { + "description": "Another open URL", + "value": "http://cds-ils.ch/", + "login_required": false + } + ] } ] diff --git a/tests/importer/test_importer.py b/tests/importer/test_importer.py index 59caed059..c5db672e5 100644 --- a/tests/importer/test_importer.py +++ b/tests/importer/test_importer.py @@ -100,14 +100,16 @@ def test_import_update_existing_audiobook(importer_test_data): updated_document = document_cls.get_record_by_pid(report["document_json"]["pid"]) time.sleep(1) - search = eitem_search_cls().search_by_document_pid( - document_pid=updated_document["pid"] + search = ( + eitem_search_cls() + .search_by_document_pid(document_pid=updated_document["pid"]) + .filter("term", eitem_type="AUDIOBOOK") ) results = search.execute() assert results.hits.total.value == 1 - created_eitem_pid = results.hits[0].pid - eitem = eitem_cls.get_record_by_pid(created_eitem_pid) + updated_eitem_pid = results.hits[0].pid + eitem = eitem_cls.get_record_by_pid(updated_eitem_pid) assert eitem["document_pid"] == updated_document["pid"] assert eitem["eitem_type"] == "AUDIOBOOK"