Tests for fedireads connector

This commit is contained in:
Mouse Reeve 2020-05-10 14:53:45 -07:00
parent d5f46a1c6f
commit 7493abeb53
8 changed files with 1179 additions and 2 deletions

View file

@ -284,8 +284,14 @@ def get_date(date_string):
''' helper function to try to interpret dates '''
if not date_string:
return None
try:
return pytz.utc.localize(parser.parse(date_string))
except ValueError:
pass
try:
return parser.parse(date_string)
except ValueError:
return None

View file

@ -0,0 +1,42 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Document",
"book_type": "Edition",
"name": "Jonathan Strange and Mr Norrell",
"url": "https://example.com/book/122",
"authors": [
"https://example.com/author/25"
],
"published_date": "2017-05-10T00:00:00+00:00",
"work": {
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Document",
"book_type": "Work",
"name": "Jonathan Strange and Mr Norrell",
"url": "https://example.com/book/121",
"authors": [
"https://example.com/author/25"
],
"title": "Jonathan Strange and Mr Norrell",
"attachment": [
{
"type": "Document",
"mediaType": "image/jpg",
"url": "https://example.com/images/covers/8775540-M.jpg",
"name": "Cover of \"Jonathan Strange and Mr Norrell\""
}
]
},
"title": "Jonathan Strange and Mr Norrell",
"subtitle": "Bloomsbury Modern Classics",
"isbn_13": "9781408891469",
"physical_format": "paperback",
"attachment": [
{
"type": "Document",
"mediaType": "image/jpg",
"url": "https://example.com/images/covers/9155821-M.jpg",
"name": "Cover of \"Jonathan Strange and Mr Norrell\""
}
]
}

View file

@ -0,0 +1 @@
[{"title": "Jonathan Strange and Mr Norrell", "key": "https://example.com/book/122", "author": "Susanna Clarke", "year": 2017}]

View file

@ -0,0 +1,44 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Document",
"book_type": "Work",
"name": "Jonathan Strange and Mr Norrell",
"url": "https://example.com/book/121",
"authors": [
"https://example.com/author/25"
],
"editions": [
{
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Document",
"book_type": "Edition",
"name": "Jonathan Strange and Mr Norrell",
"url": "https://example.com/book/122",
"authors": [
"https://example.com/author/25"
],
"published_date": "2017-05-10T00:00:00+00:00",
"title": "Jonathan Strange and Mr Norrell",
"subtitle": "Bloomsbury Modern Classics",
"isbn_13": "9781408891469",
"physical_format": "paperback",
"attachment": [
{
"type": "Document",
"mediaType": "image/jpg",
"url": "https://example.com/images/covers/9155821-M.jpg",
"name": "Cover of \"Jonathan Strange and Mr Norrell\""
}
]
}
],
"title": "Jonathan Strange and Mr Norrell",
"attachment": [
{
"type": "Document",
"mediaType": "image/jpg",
"url": "https://example.com/images/covers/8775540-M.jpg",
"name": "Cover of \"Jonathan Strange and Mr Norrell\""
}
]
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,66 @@
''' 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.fedireads_connector import Connector
from fedireads.connectors.abstract_connector import SearchResult, get_date
class FedireadsConnector(TestCase):
def setUp(self):
models.Connector.objects.create(
identifier='example.com',
connector_file='fedireads_connector',
base_url='https://example.com',
books_url='https://example.com',
covers_url='https://example.com/images/covers',
search_url='https://example.com/search?q=',
key_name='remote_id',
)
self.connector = Connector('example.com')
work_file = pathlib.Path(__file__).parent.joinpath(
'data/fr_work.json')
edition_file = pathlib.Path(__file__).parent.joinpath(
'data/fr_edition.json')
self.work_data = json.loads(work_file.read_bytes())
self.edition_data = json.loads(edition_file.read_bytes())
def test_is_work_data(self):
self.assertEqual(self.connector.is_work_data(self.work_data), True)
self.assertEqual(self.connector.is_work_data(self.edition_data), False)
def test_get_edition_from_work_data(self):
edition = self.connector.get_edition_from_work_data(self.work_data)
self.assertEqual(edition['url'], 'https://example.com/book/122')
def test_get_work_from_edition_data(self):
work = self.connector.get_work_from_edition_date(self.edition_data)
self.assertEqual(work['url'], 'https://example.com/book/121')
def test_format_search_result(self):
datafile = pathlib.Path(__file__).parent.joinpath('data/fr_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, 'Jonathan Strange and Mr Norrell')
self.assertEqual(result.key, 'https://example.com/book/122')
self.assertEqual(result.author, 'Susanna Clarke')
self.assertEqual(result.year, 2017)
def test_get_date(self):
date = get_date(self.edition_data['published_date'])
expected = parser.parse("2017-05-10T00:00:00+00:00")
self.assertEqual(date, expected)

View file

@ -13,6 +13,7 @@ from fedireads import forms, models, outgoing
from fedireads import goodreads_import
from fedireads.settings import DOMAIN
from fedireads.views import get_user_from_username
from fedireads.books_manager import get_or_create_book
def user_login(request):

View file

@ -6,7 +6,6 @@ from django.db.models import Avg, Q
from django.http import HttpResponseBadRequest, HttpResponseNotFound,\
JsonResponse
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.views.decorators.csrf import csrf_exempt
@ -390,7 +389,7 @@ def edit_profile_page(request):
def book_page(request, book_id, tab='friends'):
''' info about a book '''
book = get_or_create_book(book_id)
book = models.Book.objects.select_subclasses().get(id=book_id)
if is_api_request(request):
return JsonResponse(activitypub.get_book(book))