From aa6eaccfbbd8641379b5c5c2d3b3aacaa2841c53 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Thu, 26 Jan 2023 16:22:50 +1100 Subject: [PATCH 1/3] 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 From 1fe6892d0a56c869e0a2ca5811647e0e387ff25c Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Thu, 26 Jan 2023 16:54:14 +1100 Subject: [PATCH 2/3] pylint --- bookwyrm/tests/views/books/test_edit_book.py | 3 ++- bookwyrm/utils/isni.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/views/books/test_edit_book.py b/bookwyrm/tests/views/books/test_edit_book.py index 2c3254df0..2dc25095f 100644 --- a/bookwyrm/tests/views/books/test_edit_book.py +++ b/bookwyrm/tests/views/books/test_edit_book.py @@ -46,9 +46,10 @@ class EditBookViews(TestCase): remote_id="https://example.com/book/1", parent_work=self.work, ) - + # pylint: disable=line-too-long self.authors_body = "1.10000000084510024" + # pylint: disable=line-too-long 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( diff --git a/bookwyrm/utils/isni.py b/bookwyrm/utils/isni.py index a6b0060fc..bcff3810f 100644 --- a/bookwyrm/utils/isni.py +++ b/bookwyrm/utils/isni.py @@ -86,7 +86,8 @@ def find_authors_by_name(name_string, description=False): 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? + # 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 From c86fdfbd40f6bc2b3913abb4e6ce3377ba3d027d Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Thu, 26 Jan 2023 16:56:11 +1100 Subject: [PATCH 3/3] black --- bookwyrm/utils/isni.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/utils/isni.py b/bookwyrm/utils/isni.py index bcff3810f..318de30ef 100644 --- a/bookwyrm/utils/isni.py +++ b/bookwyrm/utils/isni.py @@ -86,7 +86,7 @@ def find_authors_by_name(name_string, description=False): possible_authors = [] for element in root.iter("responseRecord"): - # TODO: we don't seem to do anything with the + # 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: