From ac261d7b1a641952978855f2717dff4688b3f521 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 27 Dec 2020 14:27:18 -0800 Subject: [PATCH] Send connector with search result also fix typo in get_work_from_edition_data function --- bookwyrm/connectors/abstract_connector.py | 3 +- bookwyrm/connectors/bookwyrm_connector.py | 1 + bookwyrm/connectors/openlibrary.py | 3 +- bookwyrm/connectors/self_connector.py | 9 +--- bookwyrm/models/import_job.py | 4 +- bookwyrm/tests/models/test_import_model.py | 57 +++++++++++++++++++++- 6 files changed, 65 insertions(+), 12 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 86ac74353..ce1184d8c 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -168,7 +168,7 @@ class AbstractConnector(AbstractMinimalConnector): ''' every work needs at least one edition ''' @abstractmethod - def get_work_from_edition_date(self, data): + def get_work_from_edition_data(self, data): ''' every edition needs a work ''' @abstractmethod @@ -228,6 +228,7 @@ class SearchResult: key: str author: str year: str + connector: object confidence: int = 1 def __repr__(self): diff --git a/bookwyrm/connectors/bookwyrm_connector.py b/bookwyrm/connectors/bookwyrm_connector.py index e4d32fd33..3c6f46145 100644 --- a/bookwyrm/connectors/bookwyrm_connector.py +++ b/bookwyrm/connectors/bookwyrm_connector.py @@ -13,4 +13,5 @@ class Connector(AbstractMinimalConnector): return data def format_search_result(self, search_result): + search_result['connector'] = self return SearchResult(**search_result) diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index 3b60c3073..c59829d68 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -85,7 +85,7 @@ class Connector(AbstractConnector): return pick_default_edition(data['entries']) - def get_work_from_edition_date(self, data): + def get_work_from_edition_data(self, data): try: key = data['works'][0]['key'] except (IndexError, KeyError): @@ -123,6 +123,7 @@ class Connector(AbstractConnector): title=search_result.get('title'), key=key, author=', '.join(author), + connector=self, year=search_result.get('first_publish_year'), ) diff --git a/bookwyrm/connectors/self_connector.py b/bookwyrm/connectors/self_connector.py index 8d31c8a1a..cad982493 100644 --- a/bookwyrm/connectors/self_connector.py +++ b/bookwyrm/connectors/self_connector.py @@ -51,28 +51,23 @@ class Connector(AbstractConnector): author=search_result.author_text, year=search_result.published_date.year if \ search_result.published_date else None, + connector=self, confidence=search_result.rank, ) - def get_remote_id_from_data(self, data): - pass - def is_work_data(self, data): pass def get_edition_from_work_data(self, data): pass - def get_work_from_edition_date(self, data): + def get_work_from_edition_data(self, data): pass def get_authors_from_data(self, data): return None - def get_cover_from_data(self, data): - return None - def parse_search_data(self, data): ''' it's already in the right format, don't even worry about it ''' return data diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index 835094cd7..576dd07d1 100644 --- a/bookwyrm/models/import_job.py +++ b/bookwyrm/models/import_job.py @@ -76,7 +76,7 @@ class ImportItem(models.Model): ) if search_result: # raises ConnectorException - return books_manager.get_or_create_book(search_result.key) + return search_result.connector.get_or_create_book(search_result.key) return None @@ -91,7 +91,7 @@ class ImportItem(models.Model): ) if search_result: # raises ConnectorException - return books_manager.get_or_create_book(search_result.key) + return search_result.connector.get_or_create_book(search_result.key) return None diff --git a/bookwyrm/tests/models/test_import_model.py b/bookwyrm/tests/models/test_import_model.py index 4833ed169..d393d968c 100644 --- a/bookwyrm/tests/models/test_import_model.py +++ b/bookwyrm/tests/models/test_import_model.py @@ -1,9 +1,15 @@ ''' testing models ''' import datetime +import json +import pathlib +from unittest.mock import patch + from django.utils import timezone from django.test import TestCase +import responses -from bookwyrm import models +from bookwyrm import books_manager, models +from bookwyrm.connectors.abstract_connector import SearchResult class ImportJob(TestCase): @@ -113,3 +119,52 @@ class ImportJob(TestCase): expected = [] actual = models.ImportItem.objects.get(index=3) self.assertEqual(actual.reads, expected) + + + @responses.activate + def test_get_book_from_isbn(self): + ''' search and load books by isbn (9780356506999) ''' + connector_info = models.Connector.objects.create( + identifier='openlibrary.org', + name='OpenLibrary', + connector_file='openlibrary', + base_url='https://openlibrary.org', + books_url='https://openlibrary.org', + covers_url='https://covers.openlibrary.org', + search_url='https://openlibrary.org/search?q=', + priority=3, + ) + connector = books_manager.load_connector(connector_info) + result = SearchResult( + title='Test Result', + key='https://openlibrary.org/works/OL1234W', + author='An Author', + year='1980', + connector=connector, + ) + + + datafile = pathlib.Path(__file__).parent.joinpath( + '../data/ol_edition.json') + bookdata = json.loads(datafile.read_bytes()) + responses.add( + responses.GET, + 'https://openlibrary.org/works/OL1234W', + json=bookdata, + status=200) + responses.add( + responses.GET, + 'https://openlibrary.org//works/OL15832982W', + json=bookdata, + status=200) + responses.add( + responses.GET, + 'https://openlibrary.org//authors/OL382982A.json', + json={'name': 'test author'}, + status=200) + + with patch('bookwyrm.books_manager.first_search_result') as search: + search.return_value = result + book = self.item_1.get_book_from_isbn() + + self.assertEqual(book.title, 'Sabriel')