From aa6eaccfbbd8641379b5c5c2d3b3aacaa2841c53 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Thu, 26 Jan 2023 16:22:50 +1100 Subject: [PATCH] use mocks for isni API calls - fixes a couple of tests making author ISNI calls - notes a future TODO to remove some possibly useless code --- bookwyrm/tests/views/books/test_edit_book.py | 40 ++++++++++++++++++++ bookwyrm/utils/isni.py | 2 + 2 files changed, 42 insertions(+) diff --git a/bookwyrm/tests/views/books/test_edit_book.py b/bookwyrm/tests/views/books/test_edit_book.py index 9e3f84a1b..2c3254df0 100644 --- a/bookwyrm/tests/views/books/test_edit_book.py +++ b/bookwyrm/tests/views/books/test_edit_book.py @@ -1,6 +1,7 @@ """ test for app action functionality """ from unittest.mock import patch import responses +from responses import matchers from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType @@ -46,6 +47,43 @@ class EditBookViews(TestCase): parent_work=self.work, ) + self.authors_body = "1.10000000084510024" + + self.author_body = "0000000084510024https://isni.org/isni/000000008451002460Catherine Amy Dawson Scottpoet and novelistpublicVIAFWKPQ544961C. A.Dawson Scott1865-1934publicVIAFNLPa28927850VIAF45886165ALLCREhttp://viaf.org/viaf/45886165Wikipediahttps://en.wikipedia.org/wiki/Catherine_Amy_Dawson_Scott" + + responses.get( + "http://isni.oclc.org/sru/", + content_type="text/xml", + match=[ + matchers.query_param_matcher( + {"query": 'pica.na="Sappho"'}, strict_match=False + ) + ], + body=self.authors_body, + ) + + responses.get( + "http://isni.oclc.org/sru/", + content_type="text/xml", + match=[ + matchers.query_param_matcher( + {"query": 'pica.na="Some Guy"'}, strict_match=False + ) + ], + body=self.authors_body, + ) + + responses.get( + "http://isni.oclc.org/sru/", + content_type="text/xml", + match=[ + matchers.query_param_matcher( + {"query": 'pica.isn="0000000084510024"'}, strict_match=False + ) + ], + body=self.author_body, + ) + models.SiteSettings.objects.create() def test_edit_book_get(self): @@ -97,6 +135,7 @@ class EditBookViews(TestCase): result.context_data["cover_url"], "http://local.host/cover.jpg" ) + @responses.activate def test_edit_book_add_author(self): """lets a user edit a book with new authors""" view = views.EditBook.as_view() @@ -227,6 +266,7 @@ class EditBookViews(TestCase): self.book.refresh_from_db() self.assertTrue(self.book.cover) + @responses.activate def test_add_authors_helper(self): """converts form input into author matches""" form = forms.EditionForm(instance=self.book) diff --git a/bookwyrm/utils/isni.py b/bookwyrm/utils/isni.py index ea0364e55..a6b0060fc 100644 --- a/bookwyrm/utils/isni.py +++ b/bookwyrm/utils/isni.py @@ -85,6 +85,8 @@ def find_authors_by_name(name_string, description=False): # build list of possible authors possible_authors = [] for element in root.iter("responseRecord"): + + # TODO: we don't seem to do anything with the personal_name variable - is this code block needed? personal_name = element.find(".//forename/..") if not personal_name: continue