diff --git a/bookwyrm/importers/calibre_import.py b/bookwyrm/importers/calibre_import.py index 18c6358a8..ce20fbfa1 100644 --- a/bookwyrm/importers/calibre_import.py +++ b/bookwyrm/importers/calibre_import.py @@ -1,4 +1,6 @@ """ handle reading a csv from calibre """ +from bookwyrm.models import Shelf + from . import Importer @@ -9,4 +11,4 @@ class CalibreImporter(Importer): def get_shelf(self, normalized_row): # Calibre export does not indicate which shelf to use. Go with a default one for now - return "to-read" + return Shelf.TO_READ diff --git a/bookwyrm/tests/data/calibre.csv b/bookwyrm/tests/data/calibre.csv new file mode 100644 index 000000000..4f936cfa9 --- /dev/null +++ b/bookwyrm/tests/data/calibre.csv @@ -0,0 +1,2 @@ +authors,author_sort,rating,library_name,timestamp,formats,size,isbn,identifiers,comments,tags,series,series_index,languages,title,cover,title_sort,publisher,pubdate,id,uuid +"Seanan McGuire","McGuire, Seanan","5","Bücher","2021-01-19T22:41:16+01:00","epub, original_epub","1433809","9780756411800","goodreads:39077187,isbn:9780756411800","REPLACED COMMENTS (BOOK DESCRIPTION) BECAUSE IT IS REALLY LONG.","Cryptids, Fantasy, Romance, Magic","InCryptid","8.0","eng","That Ain't Witchcraft","/home/tastytea/Bücher/Seanan McGuire/That Ain't Witchcraft (864)/cover.jpg","That Ain't Witchcraft","Daw Books","2019-03-05T01:00:00+01:00","864","3051ed45-8943-4900-a22a-d2704e3583df" diff --git a/bookwyrm/tests/importers/test_calibre_import.py b/bookwyrm/tests/importers/test_calibre_import.py new file mode 100644 index 000000000..5257caa63 --- /dev/null +++ b/bookwyrm/tests/importers/test_calibre_import.py @@ -0,0 +1,67 @@ +""" testing import """ +import pathlib +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.importers import CalibreImporter +from bookwyrm.importers.importer import handle_imported_book + + +# 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 CalibreImport(TestCase): + """importing from Calibre csv""" + + def setUp(self): + """use a test csv""" + self.importer = CalibreImporter() + datafile = pathlib.Path(__file__).parent.joinpath("../data/calibre.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" + ), patch("bookwyrm.lists_stream.populate_lists_task.delay"): + self.local_user = models.User.objects.create_user( + "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""" + import_job = self.importer.create_job( + self.local_user, self.csv, False, "public" + ) + + import_items = ( + models.ImportItem.objects.filter(job=import_job).order_by("index").all() + ) + self.assertEqual(len(import_items), 1) + self.assertEqual(import_items[0].index, 0) + self.assertEqual(import_items[0].normalized_data["title"], "That Ain't Witchcraft") + + def test_handle_imported_book(self, *_): + """calibre import added a book, this adds related connections""" + shelf = self.local_user.shelf_set.filter(identifier=models.Shelf.TO_READ).first() + self.assertIsNone(shelf.books.first()) + + 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() + + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): + handle_imported_book(import_item) + + shelf.refresh_from_db() + self.assertEqual(shelf.books.first(), self.book)