From 470c5d4acb085a70548497e2381d9f76073de9e1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 10 May 2020 09:11:01 -0700 Subject: [PATCH] Adds cursory openlibrary connector tests --- fedireads/connectors/openlibrary.py | 2 +- fedireads/tests/data/ol_edition.json | 83 +++++++++++ fedireads/tests/data/ol_search.json | 132 ++++++++++++++++++ fedireads/tests/data/ol_work.json | 63 +++++++++ fedireads/tests/test_connector_openlibrary.py | 65 +++++++++ 5 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 fedireads/tests/data/ol_edition.json create mode 100644 fedireads/tests/data/ol_search.json create mode 100644 fedireads/tests/data/ol_work.json create mode 100644 fedireads/tests/test_connector_openlibrary.py diff --git a/fedireads/connectors/openlibrary.py b/fedireads/connectors/openlibrary.py index ee73f47d..6da608ef 100644 --- a/fedireads/connectors/openlibrary.py +++ b/fedireads/connectors/openlibrary.py @@ -42,7 +42,7 @@ class Connector(AbstractConnector): return SearchResult( doc.get('title'), key, - author[0], + ', '.join(author), doc.get('first_publish_year'), ) diff --git a/fedireads/tests/data/ol_edition.json b/fedireads/tests/data/ol_edition.json new file mode 100644 index 00000000..459e9dff --- /dev/null +++ b/fedireads/tests/data/ol_edition.json @@ -0,0 +1,83 @@ +{ + "identifiers": { + "librarything": [ + "10014" + ], + "goodreads": [ + "535197", + "1102517", + "518848" + ] + }, + "lc_classifications": [ + "PZ7.N647 Sab 1995" + ], + "latest_revision": 7, + "ocaid": "sabriel00nixg", + "ia_box_id": [ + "IA107202" + ], + "edition_name": "1st American ed.", + "title": "Sabriel", + "languages": [ + { + "key": "/languages/eng" + } + ], + "subjects": [ + "Fantasy." + ], + "publish_country": "nyu", + "by_statement": "Garth Nix.", + "type": { + "key": "/type/edition" + }, + "revision": 7, + "publishers": [ + "Harper Trophy" + ], + "description": { + "type": "/type/text", + "value": "Sabriel, daughter of the necromancer Abhorsen, must journey into the mysterious and magical Old Kingdom to rescue her father from the Land of the Dead." + }, + "last_modified": { + "type": "/type/datetime", + "value": "2017-10-08T21:20:07.665236" + }, + "key": "/books/OL22951843M", + "authors": [ + { + "key": "/authors/OL382982A" + } + ], + "publish_places": [ + "New York" + ], + "pagination": "491 p. :", + "created": { + "type": "/type/datetime", + "value": "2009-02-12T16:29:58.929717" + }, + "dewey_decimal_class": [ + "[Fic]" + ], + "notes": { + "type": "/type/text", + "value": "Originally published: Australia : HarperCollins, 1995." + }, + "number_of_pages": 491, + "lccn": [ + "96001295" + ], + "isbn_10": [ + "0060273224", + "0060273232", + "0064471837" + ], + "publish_date": "1996", + "works": [ + { + "key": "/works/OL15832982W" + } + ] +} diff --git a/fedireads/tests/data/ol_search.json b/fedireads/tests/data/ol_search.json new file mode 100644 index 00000000..4ac51bac --- /dev/null +++ b/fedireads/tests/data/ol_search.json @@ -0,0 +1,132 @@ +{ + "start": 0, + "num_found": 2, + "numFound": 2, + "docs": [ + { + "title_suggest": "This Is How You Lose the Time War", + "edition_key": [ + "OL27901088M" + ], + "isbn": [ + "9781534431003", + "1534431004" + ], + "has_fulltext": false, + "text": [ + "OL27901088M", + "9781534431003", + "1534431004", + "Amal El-Mohtar", + "Max Gladstone", + "OL7313207A", + "OL7129451A", + "epistolary", + "science fiction", + "time-traveling", + "LGBT", + "This Is How You Lose the Time War", + "/works/OL20639540W", + "Simon and Schuster", + "Atlantis", + "London", + "The whole of time and space" + ], + "author_name": [ + "Amal El-Mohtar", + "Max Gladstone" + ], + "seed": [ + "/books/OL27901088M", + "/works/OL20639540W", + "/subjects/science_fiction", + "/subjects/time-traveling", + "/subjects/epistolary", + "/subjects/lgbt", + "/subjects/place:london", + "/subjects/place:atlantis", + "/subjects/time:the_whole_of_time_and_space", + "/authors/OL7313207A", + "/authors/OL7129451A" + ], + "author_key": [ + "OL7313207A", + "OL7129451A" + ], + "availability": { + "status": "error" + }, + "subject": [ + "epistolary", + "science fiction", + "time-traveling", + "LGBT" + ], + "title": "This Is How You Lose the Time War", + "publish_date": [ + "July 16, 2019" + ], + "type": "work", + "ebook_count_i": 0, + "publish_place": [ + "New York, USA" + ], + "edition_count": 1, + "key": "/works/OL20639540W", + "publisher": [ + "Simon and Schuster" + ], + "language": [ + "eng" + ], + "last_modified_i": 1579909341, + "cover_edition_key": "OL27901088M", + "publish_year": [ + 2019 + ], + "first_publish_year": 2019, + "place": [ + "Atlantis", + "London" + ], + "time": [ + "The whole of time and space" + ] + }, + { + "title_suggest": "This is How You Lose the Time War", + "cover_i": 8665647, + "has_fulltext": false, + "title": "This is How You Lose the Time War", + "last_modified_i": 1561998020, + "edition_count": 0, + "author_name": [ + "Amal El-Mohtar", + "Max Gladstone" + ], + "seed": [ + "/works/OL19859295W", + "/authors/OL7313207A", + "/authors/OL7129451A" + ], + "key": "/works/OL19859295W", + "text": [ + "Amal El-Mohtar", + "Max Gladstone", + "OL7313207A", + "OL7129451A", + "This is How You Lose the Time War", + "/works/OL19859295W" + ], + "author_key": [ + "OL7313207A", + "OL7129451A" + ], + "type": "work", + "availability": { + "status": "error" + }, + "ebook_count_i": 0 + } + ] +} diff --git a/fedireads/tests/data/ol_work.json b/fedireads/tests/data/ol_work.json new file mode 100644 index 00000000..8587845b --- /dev/null +++ b/fedireads/tests/data/ol_work.json @@ -0,0 +1,63 @@ +{ + "first_publish_date": "1995", + "key": "/works/OL15832982W", + "description": { + "type": "/type/text", + "value": "First in the Old Kingdom/Abhorsen series." + }, + "created": { + "type": "/type/datetime", + "value": "2011-07-07T16:30:28.384311" + }, + "title": "Sabriel", + "covers": [ + 6796986, + 3843137 + ], + "first_sentence": { + "type": "/type/text", + "value": "THE RABBIT HAD been run over minutes before." + }, + "excerpts": [ + { + "excerpt": "THE RABBIT HAD been run over minutes before." + } + ], + "lc_classifications": [ + "PZ7.N647 Sab 1995" + ], + "latest_revision": 5, + "last_modified": { + "type": "/type/datetime", + "value": "2019-07-22T13:57:34.579651" + }, + "authors": [ + { + "type": { + "key": "/type/author_role" + }, + "author": { + "key": "/authors/OL382982A" + } + } + ], + "dewey_number": [ + "[Fic]" + ], + "subjects": [ + "Fantasy", + "Science Fiction & Fantasy", + "Fantasy fiction", + "Fiction", + "Juvenile Fiction", + "Juvenile fiction", + "Magical thinking" + ], + "type": { + "key": "/type/work" + }, + "subject_times": [ + "Life and Death." + ], + "revision": 5 +} diff --git a/fedireads/tests/test_connector_openlibrary.py b/fedireads/tests/test_connector_openlibrary.py new file mode 100644 index 00000000..eb2e6b23 --- /dev/null +++ b/fedireads/tests/test_connector_openlibrary.py @@ -0,0 +1,65 @@ +''' testing book data connectors ''' +from dateutil import parser +from django.test import TestCase +import json +import pathlib +import pytz + +from fedireads import models +from fedireads.connectors.openlibrary import Connector +from fedireads.connectors.openlibrary import get_languages, get_description +from fedireads.connectors.abstract_connector import SearchResult, get_date + + +class Openlibrary(TestCase): + def setUp(self): + 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=', + key_name='openlibrary_key', + ) + self.connector = Connector('openlibrary.org') + + work_file = pathlib.Path(__file__).parent.joinpath( + 'data/ol_work.json') + edition_file = pathlib.Path(__file__).parent.joinpath( + 'data/ol_edition.json') + self.work_data = json.loads(work_file.read_bytes()) + self.edition_data = json.loads(edition_file.read_bytes()) + + + def test_format_search_result(self): + ''' translate json from openlibrary into SearchResult ''' + datafile = pathlib.Path(__file__).parent.joinpath('data/ol_search.json') + search_data = json.loads(datafile.read_bytes()) + results = self.connector.parse_search_data(search_data) + self.assertIsInstance(results, list) + + result = self.connector.format_search_result(results[0]) + self.assertIsInstance(result, SearchResult) + self.assertEqual(result.title, 'This Is How You Lose the Time War') + self.assertEqual(result.key, 'OL20639540W') + self.assertEqual(result.author, 'Amal El-Mohtar, Max Gladstone') + self.assertEqual(result.year, 2019) + + + def test_get_description(self): + description = get_description(self.work_data['description']) + expected = 'First in the Old Kingdom/Abhorsen series.' + self.assertEqual(description, expected) + + + def test_get_date(self): + date = get_date(self.work_data['first_publish_date']) + expected = pytz.utc.localize(parser.parse('1995')) + self.assertEqual(date, expected) + + + def test_get_languages(self): + languages = get_languages(self.edition_data['languages']) + self.assertEqual(languages, ['English'])