2021-03-08 16:49:10 +00:00
|
|
|
""" testing models """
|
2020-05-09 23:16:28 +00:00
|
|
|
import datetime
|
2020-12-27 22:27:18 +00:00
|
|
|
import json
|
|
|
|
import pathlib
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-11-28 00:24:53 +00:00
|
|
|
from django.utils import timezone
|
2020-05-09 23:16:28 +00:00
|
|
|
from django.test import TestCase
|
2020-12-27 22:27:18 +00:00
|
|
|
import responses
|
2020-05-09 23:16:28 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
2021-09-30 18:46:17 +00:00
|
|
|
from bookwyrm.book_search import SearchResult
|
2021-01-02 16:14:28 +00:00
|
|
|
from bookwyrm.connectors import connector_manager
|
2020-05-09 23:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImportJob(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""this is a fancy one!!!"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-05-09 23:16:28 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""data is from a goodreads export of The Raven Tower"""
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
2021-11-11 22:08:00 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.mouse", "password", local=True
|
2021-08-03 17:25:53 +00:00
|
|
|
)
|
2021-11-11 22:08:00 +00:00
|
|
|
self.job = models.ImportJob.objects.create(user=self.local_user, mappings={})
|
2020-05-09 23:16:28 +00:00
|
|
|
|
|
|
|
def test_isbn(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""it unquotes the isbn13 field from data"""
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(item.isbn, "9780356506999")
|
2020-05-09 23:16:28 +00:00
|
|
|
|
|
|
|
def test_shelf(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""converts to the local shelf typology"""
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(item.shelf, "reading")
|
2020-05-09 23:16:28 +00:00
|
|
|
|
|
|
|
def test_date_added(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""converts to the local shelf typology"""
|
2020-11-28 00:24:53 +00:00
|
|
|
expected = datetime.datetime(2019, 4, 9, 0, 0, tzinfo=timezone.utc)
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
"date_added": "2019/04/09",
|
|
|
|
},
|
|
|
|
)
|
2020-05-09 23:16:28 +00:00
|
|
|
self.assertEqual(item.date_added, expected)
|
|
|
|
|
|
|
|
def test_date_read(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""converts to the local shelf typology"""
|
2020-11-28 00:24:53 +00:00
|
|
|
expected = datetime.datetime(2019, 4, 12, 0, 0, tzinfo=timezone.utc)
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
"date_added": "2019/04/09",
|
|
|
|
"date_finished": "2019/04/12",
|
|
|
|
},
|
|
|
|
)
|
2020-05-09 23:16:28 +00:00
|
|
|
self.assertEqual(item.date_read, expected)
|
|
|
|
|
2020-05-10 05:13:44 +00:00
|
|
|
def test_currently_reading_reads(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""infer currently reading dates where available"""
|
2021-03-08 16:49:10 +00:00
|
|
|
expected = [
|
|
|
|
models.ReadThrough(
|
|
|
|
start_date=datetime.datetime(2019, 4, 9, 0, 0, tzinfo=timezone.utc)
|
|
|
|
)
|
|
|
|
]
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
"date_added": "2019/04/09",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(item.reads[0].start_date, expected[0].start_date)
|
|
|
|
self.assertIsNone(item.reads[0].finish_date)
|
2020-05-09 23:38:05 +00:00
|
|
|
|
2020-05-10 05:13:44 +00:00
|
|
|
def test_read_reads(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""infer read dates where available"""
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
"date_added": "2019/04/09",
|
|
|
|
"date_finished": "2019/04/12",
|
|
|
|
},
|
|
|
|
)
|
2020-12-27 21:32:27 +00:00
|
|
|
self.assertEqual(
|
2021-11-11 22:08:00 +00:00
|
|
|
item.reads[0].start_date,
|
2021-03-08 16:49:10 +00:00
|
|
|
datetime.datetime(2019, 4, 9, 0, 0, tzinfo=timezone.utc),
|
|
|
|
)
|
2020-12-27 21:32:27 +00:00
|
|
|
self.assertEqual(
|
2021-11-11 22:08:00 +00:00
|
|
|
item.reads[0].finish_date,
|
2021-03-08 16:49:10 +00:00
|
|
|
datetime.datetime(2019, 4, 12, 0, 0, tzinfo=timezone.utc),
|
|
|
|
)
|
2020-05-09 23:38:05 +00:00
|
|
|
|
2020-05-10 05:13:44 +00:00
|
|
|
def test_unread_reads(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""handle books with no read dates"""
|
2020-05-09 23:38:05 +00:00
|
|
|
expected = []
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
"shelf": "reading",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(item.reads, expected)
|
2020-05-09 23:38:05 +00:00
|
|
|
|
2020-12-27 22:27:18 +00:00
|
|
|
@responses.activate
|
2021-12-14 22:19:27 +00:00
|
|
|
def test_get_book_from_identifier(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""search and load books by isbn (9780356506999)"""
|
2021-11-11 22:08:00 +00:00
|
|
|
item = models.ImportItem.objects.create(
|
|
|
|
index=1,
|
|
|
|
job=self.job,
|
|
|
|
data={},
|
|
|
|
normalized_data={
|
|
|
|
"isbn_13": '="9780356506999"',
|
|
|
|
},
|
|
|
|
)
|
2020-12-27 22:27:18 +00:00
|
|
|
connector_info = models.Connector.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
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=",
|
2020-12-27 22:27:18 +00:00
|
|
|
priority=3,
|
|
|
|
)
|
2021-01-02 16:14:28 +00:00
|
|
|
connector = connector_manager.load_connector(connector_info)
|
2020-12-27 22:27:18 +00:00
|
|
|
result = SearchResult(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test Result",
|
|
|
|
key="https://openlibrary.org/works/OL1234W",
|
|
|
|
author="An Author",
|
|
|
|
year="1980",
|
2020-12-27 22:27:18 +00:00
|
|
|
connector=connector,
|
|
|
|
)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_edition.json")
|
2020-12-27 22:27:18 +00:00
|
|
|
bookdata = json.loads(datafile.read_bytes())
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
2021-03-08 16:49:10 +00:00
|
|
|
"https://openlibrary.org/works/OL1234W",
|
2020-12-27 22:27:18 +00:00
|
|
|
json=bookdata,
|
2021-03-08 16:49:10 +00:00
|
|
|
status=200,
|
|
|
|
)
|
2020-12-27 22:27:18 +00:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
2021-03-08 16:49:10 +00:00
|
|
|
"https://openlibrary.org/works/OL15832982W",
|
2020-12-27 22:27:18 +00:00
|
|
|
json=bookdata,
|
2021-03-08 16:49:10 +00:00
|
|
|
status=200,
|
|
|
|
)
|
2020-12-27 22:27:18 +00:00
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
2021-03-08 16:49:10 +00:00
|
|
|
"https://openlibrary.org/authors/OL382982A",
|
|
|
|
json={"name": "test author"},
|
|
|
|
status=200,
|
|
|
|
)
|
2020-12-27 22:27:18 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"):
|
2021-01-02 17:08:37 +00:00
|
|
|
with patch(
|
2021-03-08 16:49:10 +00:00
|
|
|
"bookwyrm.connectors.connector_manager.first_search_result"
|
|
|
|
) as search:
|
2021-01-02 17:08:37 +00:00
|
|
|
search.return_value = result
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
|
|
|
|
):
|
2021-12-14 22:19:27 +00:00
|
|
|
book = item.get_book_from_identifier()
|
2020-12-27 22:27:18 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(book.title, "Sabriel")
|