moviewyrm/bookwyrm/tests/importers/test_librarything_import.py

164 lines
6.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing import """
2021-02-20 16:02:36 +00:00
import pathlib
from unittest.mock import patch
import datetime
import pytz
2021-02-20 16:02:36 +00:00
from django.test import TestCase
2021-03-30 15:56:25 +00:00
from bookwyrm import models
2021-03-30 16:13:41 +00:00
from bookwyrm.importers import LibrarythingImporter
2021-11-13 17:50:56 +00:00
from bookwyrm.importers.importer import handle_imported_book
2021-02-20 16:02:36 +00:00
2021-03-08 16:49:10 +00:00
def make_date(*args):
2021-08-02 23:05:40 +00:00
"""helper function to easily generate a date obj"""
return datetime.datetime(*args, tzinfo=pytz.UTC)
# pylint: disable=consider-using-with
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 21:50:33 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
2021-02-20 16:02:36 +00:00
class LibrarythingImport(TestCase):
2021-04-26 16:15:42 +00:00
"""importing from librarything tsv"""
2021-03-08 16:49:10 +00:00
2021-02-20 16:02:36 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""use a test tsv"""
2021-03-23 16:19:11 +00:00
self.importer = LibrarythingImporter()
2021-03-30 16:13:41 +00:00
datafile = pathlib.Path(__file__).parent.joinpath("../data/librarything.tsv")
2021-02-20 16:02:36 +00:00
# Librarything generates latin encoded exports...
2021-03-08 16:49:10 +00:00
self.csv = open(datafile, "r", encoding=self.importer.encoding)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-11-11 17:21:28 +00:00
self.local_user = models.User.objects.create_user(
2021-08-03 17:25:53 +00:00
"mmai", "mmai@mmai.mmai", "password", local=True
)
2021-08-02 23:05:40 +00:00
work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=work,
)
2021-02-20 16:02:36 +00:00
2021-09-06 22:09:04 +00:00
def test_create_job(self, *_):
2021-04-26 16:15:42 +00:00
"""creates the import job entry and checks csv"""
2021-11-11 17:21:28 +00:00
import_job = self.importer.create_job(
self.local_user, self.csv, False, "public"
)
self.assertEqual(import_job.user, self.local_user)
2021-02-20 16:02:36 +00:00
self.assertEqual(import_job.include_reviews, False)
2021-03-08 16:49:10 +00:00
self.assertEqual(import_job.privacy, "public")
2021-02-20 16:02:36 +00:00
import_items = models.ImportItem.objects.filter(job=import_job).all()
self.assertEqual(len(import_items), 3)
self.assertEqual(import_items[0].index, 0)
2021-03-08 16:49:10 +00:00
self.assertEqual(import_items[0].data["Book Id"], "5498194")
self.assertEqual(import_items[0].normalized_data["isbn_13"], "9782070291342")
self.assertEqual(import_items[0].normalized_data["isbn_10"], "2070291340")
self.assertEqual(import_items[0].normalized_data["title"], "Marelle")
self.assertEqual(import_items[0].normalized_data["authors"], "Cortazar, Julio")
self.assertEqual(import_items[0].normalized_data["date_added"], "2006-08-09")
self.assertEqual(import_items[0].normalized_data["date_started"], "2007-04-16")
self.assertEqual(import_items[0].normalized_data["date_finished"], "2007-05-08")
2021-02-20 16:02:36 +00:00
self.assertEqual(import_items[1].index, 1)
2021-03-08 16:49:10 +00:00
self.assertEqual(import_items[1].data["Book Id"], "5015319")
2021-02-20 16:02:36 +00:00
self.assertEqual(import_items[2].index, 2)
2021-03-08 16:49:10 +00:00
self.assertEqual(import_items[2].data["Book Id"], "5015399")
2021-02-20 16:02:36 +00:00
2021-09-06 22:09:04 +00:00
def test_create_retry_job(self, *_):
2021-04-26 16:15:42 +00:00
"""trying again with items that didn't import"""
2021-11-11 17:21:28 +00:00
import_job = self.importer.create_job(
self.local_user, self.csv, False, "unlisted"
)
2021-03-08 16:49:10 +00:00
import_items = models.ImportItem.objects.filter(job=import_job).all()[:2]
2021-11-11 17:21:28 +00:00
retry = self.importer.create_retry_job(
self.local_user, import_job, import_items
)
2021-02-20 16:02:36 +00:00
self.assertNotEqual(import_job, retry)
2021-11-11 17:21:28 +00:00
self.assertEqual(retry.user, self.local_user)
2021-02-20 16:02:36 +00:00
self.assertEqual(retry.include_reviews, False)
2021-03-08 16:49:10 +00:00
self.assertEqual(retry.privacy, "unlisted")
2021-02-20 16:02:36 +00:00
retry_items = models.ImportItem.objects.filter(job=retry).all()
self.assertEqual(len(retry_items), 2)
self.assertEqual(retry_items[0].index, 0)
2021-03-08 16:49:10 +00:00
self.assertEqual(import_items[0].data["Book Id"], "5498194")
2021-02-20 16:02:36 +00:00
self.assertEqual(retry_items[1].index, 1)
2021-03-08 16:49:10 +00:00
self.assertEqual(retry_items[1].data["Book Id"], "5015319")
2021-02-20 16:02:36 +00:00
2021-09-06 22:09:04 +00:00
def test_handle_imported_book(self, *_):
2021-04-26 16:15:42 +00:00
"""librarything import added a book, this adds related connections"""
2021-11-11 17:21:28 +00:00
shelf = self.local_user.shelf_set.filter(identifier="read").first()
2021-02-20 16:02:36 +00:00
self.assertIsNone(shelf.books.first())
2021-11-11 17:21:28 +00:00
import_job = self.importer.create_job(
self.local_user, self.csv, False, "public"
)
import_item = import_job.items.first()
import_item.book = self.book
import_item.save()
2021-02-20 16:02:36 +00:00
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-11-13 17:50:56 +00:00
handle_imported_book(import_item)
2021-02-20 16:02:36 +00:00
shelf.refresh_from_db()
self.assertEqual(shelf.books.first(), self.book)
2021-11-11 17:21:28 +00:00
readthrough = models.ReadThrough.objects.get(user=self.local_user)
2021-02-20 16:02:36 +00:00
self.assertEqual(readthrough.book, self.book)
self.assertEqual(readthrough.start_date, make_date(2007, 4, 16))
self.assertEqual(readthrough.finish_date, make_date(2007, 5, 8))
2021-02-20 16:02:36 +00:00
2021-09-06 22:09:04 +00:00
def test_handle_imported_book_already_shelved(self, *_):
2021-04-26 16:15:42 +00:00
"""librarything import added a book, this adds related connections"""
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-11-11 17:21:28 +00:00
shelf = self.local_user.shelf_set.filter(identifier="to-read").first()
models.ShelfBook.objects.create(
shelf=shelf, user=self.local_user, book=self.book
2021-03-08 16:49:10 +00:00
)
2021-11-11 17:21:28 +00:00
import_job = self.importer.create_job(
self.local_user, self.csv, False, "public"
)
import_item = import_job.items.first()
import_item.book = self.book
import_item.save()
2021-02-20 16:02:36 +00:00
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-11-13 17:50:56 +00:00
handle_imported_book(import_item)
2021-02-20 16:02:36 +00:00
shelf.refresh_from_db()
self.assertEqual(shelf.books.first(), self.book)
2021-11-11 17:21:28 +00:00
self.assertIsNone(
self.local_user.shelf_set.get(identifier="read").books.first()
)
2021-11-11 17:21:28 +00:00
readthrough = models.ReadThrough.objects.get(user=self.local_user)
2021-02-20 16:02:36 +00:00
self.assertEqual(readthrough.book, self.book)
self.assertEqual(readthrough.start_date, make_date(2007, 4, 16))
self.assertEqual(readthrough.finish_date, make_date(2007, 5, 8))
2021-02-20 16:02:36 +00:00
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-08-03 17:25:53 +00:00
def test_handle_imported_book_review(self, *_):
2021-04-26 16:15:42 +00:00
"""librarything review import"""
2021-11-13 18:16:05 +00:00
import_job = self.importer.create_job(
self.local_user, self.csv, True, "unlisted"
)
2021-11-11 17:21:28 +00:00
import_item = import_job.items.filter(index=0).first()
import_item.book = self.book
import_item.save()
2021-02-20 16:02:36 +00:00
2021-11-12 17:17:00 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
2021-11-13 17:50:56 +00:00
handle_imported_book(import_item)
2021-11-11 17:21:28 +00:00
review = models.Review.objects.get(book=self.book, user=self.local_user)
2021-03-08 16:49:10 +00:00
self.assertEqual(review.content, "chef d'oeuvre")
2021-11-11 20:29:38 +00:00
self.assertEqual(review.rating, 4.5)
self.assertEqual(review.published_date, make_date(2007, 5, 8))
2021-03-08 16:49:10 +00:00
self.assertEqual(review.privacy, "unlisted")