2021-11-10 17:53:52 +00:00
|
|
|
""" testing import """
|
|
|
|
import pathlib
|
|
|
|
from unittest.mock import patch
|
|
|
|
import datetime
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.importers import StorygraphImporter
|
|
|
|
from bookwyrm.importers.importer import handle_imported_book
|
|
|
|
|
|
|
|
|
|
|
|
def make_date(*args):
|
|
|
|
"""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")
|
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
|
|
|
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
|
|
|
class StorygraphImport(TestCase):
|
|
|
|
"""importing from storygraph csv"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""use a test csv"""
|
|
|
|
self.importer = StorygraphImporter()
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/storygraph.csv")
|
|
|
|
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-11-10 17:53:52 +00:00
|
|
|
"mouse", "mouse@mouse.mouse", "password", local=True
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_job(self, *_):
|
|
|
|
"""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"
|
|
|
|
)
|
2021-11-10 17:53:52 +00:00
|
|
|
|
2021-12-29 21:43:28 +00:00
|
|
|
import_items = (
|
|
|
|
models.ImportItem.objects.filter(job=import_job).order_by("index").all()
|
|
|
|
)
|
2021-11-10 17:53:52 +00:00
|
|
|
self.assertEqual(len(import_items), 2)
|
|
|
|
self.assertEqual(import_items[0].index, 0)
|
2021-11-11 20:39:12 +00:00
|
|
|
self.assertEqual(import_items[0].normalized_data["title"], "Always Coming Home")
|
2021-11-10 17:53:52 +00:00
|
|
|
self.assertEqual(import_items[1].index, 1)
|
2021-11-11 20:39:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
import_items[1].normalized_data["title"], "Subprime Attention Crisis"
|
|
|
|
)
|
|
|
|
self.assertEqual(import_items[1].normalized_data["rating"], "5.0")
|
2021-11-10 17:53:52 +00:00
|
|
|
|
|
|
|
def test_handle_imported_book(self, *_):
|
|
|
|
"""storygraph import added a book, this adds related connections"""
|
2021-11-11 17:21:28 +00:00
|
|
|
shelf = self.local_user.shelf_set.filter(identifier="to-read").first()
|
2021-11-10 17:53:52 +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-11-10 17:53:52 +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-10 17:53:52 +00:00
|
|
|
|
|
|
|
shelf.refresh_from_db()
|
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
|
|
|
self.assertEqual(
|
|
|
|
shelf.shelfbook_set.first().shelved_date, make_date(2021, 5, 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
|
|
|
def test_handle_imported_book_rating(self, *_):
|
|
|
|
"""storygraph rating import"""
|
2021-11-11 17:21:28 +00:00
|
|
|
import_job = self.importer.create_job(
|
2021-11-13 18:16:05 +00:00
|
|
|
self.local_user, self.csv, True, "unlisted"
|
2021-11-10 17:53:52 +00:00
|
|
|
)
|
2021-11-11 17:21:28 +00:00
|
|
|
import_item = import_job.items.filter(index=1).first()
|
|
|
|
import_item.book = self.book
|
|
|
|
import_item.save()
|
2021-11-10 17:53:52 +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.ReviewRating.objects.get(book=self.book, user=self.local_user)
|
2021-11-10 17:53:52 +00:00
|
|
|
self.assertIsInstance(review, models.ReviewRating)
|
|
|
|
self.assertEqual(review.rating, 5.0)
|
|
|
|
self.assertEqual(review.published_date, make_date(2021, 5, 10))
|
|
|
|
self.assertEqual(review.privacy, "unlisted")
|