From f71ef286b629c3e8744bb7d895b1a30f827ca484 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 12 Nov 2021 08:55:47 -0800 Subject: [PATCH] Updates mocks --- bookwyrm/activitystreams.py | 4 ++-- bookwyrm/importers/importer.py | 4 ++-- bookwyrm/tests/importers/test_importer.py | 22 ++++++++++++------- .../importers/test_librarything_import.py | 6 ++--- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index a9ca17e2..e1a52d26 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -277,12 +277,12 @@ def add_status_on_create(sender, instance, created, *args, **kwargs): def add_status_on_create_command(sender, instance, created): """runs this code only after the database commit completes""" - priority=HIGH + priority = HIGH # check if this is an old status, de-prioritize if so # (this will happen if federation is very slow, or, more expectedly, on csv import) one_day = 60 * 60 * 24 if (instance.created_date - instance.published_date).seconds > one_day: - priority=LOW + priority = LOW add_status_task.apply_async( args=(instance.id,), diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index 3e0b0c28..b32e2df7 100644 --- a/bookwyrm/importers/importer.py +++ b/bookwyrm/importers/importer.py @@ -151,9 +151,9 @@ def handle_imported_book(source, user, item, include_reviews, privacy): if item.shelf and not existing_shelf: desired_shelf = models.Shelf.objects.get(identifier=item.shelf, user=user) shelved_date = item.date_added or timezone.now() - models.ShelfBook.objects.create( + models.ShelfBook( book=item.book, shelf=desired_shelf, user=user, shelved_date=shelved_date - ) + ).save(priority=LOW) for read in item.reads: # check for an existing readthrough with the same dates diff --git a/bookwyrm/tests/importers/test_importer.py b/bookwyrm/tests/importers/test_importer.py index 05377cce..963eca54 100644 --- a/bookwyrm/tests/importers/test_importer.py +++ b/bookwyrm/tests/importers/test_importer.py @@ -136,7 +136,13 @@ class GenericImporter(TestCase): "bookwyrm.models.import_job.ImportItem.get_book_from_isbn" ) as resolve: resolve.return_value = self.book - import_item_task(self.importer.service, import_item.id) + + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" + ) as mock: + import_item_task(self.importer.service, import_item.id) + kwargs = mock.call_args.kwargs + self.assertEqual(kwargs["queue"], "low_priority") import_item.refresh_from_db() self.assertEqual(import_item.book.id, self.book.id) @@ -153,7 +159,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): handle_imported_book( self.importer.service, self.local_user, import_item, False, "public" ) @@ -163,7 +169,7 @@ class GenericImporter(TestCase): def test_handle_imported_book_already_shelved(self, *_): """import added a book, this adds related connections""" - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): shelf = self.local_user.shelf_set.filter(identifier="to-read").first() models.ShelfBook.objects.create( shelf=shelf, @@ -179,7 +185,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): handle_imported_book( self.importer.service, self.local_user, import_item, False, "public" ) @@ -203,7 +209,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): handle_imported_book( self.importer.service, self.local_user, import_item, False, "public" ) @@ -223,7 +229,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): with patch("bookwyrm.models.Status.broadcast") as broadcast_mock: handle_imported_book( self.importer.service, @@ -249,7 +255,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): handle_imported_book( self.importer.service, self.local_user, import_item, True, "unlisted" ) @@ -267,7 +273,7 @@ class GenericImporter(TestCase): import_item.book = self.book import_item.save() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): handle_imported_book( self.importer.service, self.local_user, import_item, False, "unlisted" ) diff --git a/bookwyrm/tests/importers/test_librarything_import.py b/bookwyrm/tests/importers/test_librarything_import.py index e9352896..55bfef72 100644 --- a/bookwyrm/tests/importers/test_librarything_import.py +++ b/bookwyrm/tests/importers/test_librarything_import.py @@ -9,7 +9,7 @@ import responses from bookwyrm import models from bookwyrm.importers import LibrarythingImporter -from bookwyrm.importers.importer import import_data, handle_imported_book +from bookwyrm.importers.importer import start_import_task, handle_imported_book def make_date(*args): @@ -85,7 +85,7 @@ class LibrarythingImport(TestCase): self.assertEqual(retry_items[1].data["Book Id"], "5015319") @responses.activate - def test_import_data(self, *_): + def test_start_import_task(self, *_): """resolve entry""" import_job = self.importer.create_job( self.local_user, self.csv, False, "unlisted" @@ -97,7 +97,7 @@ class LibrarythingImport(TestCase): ) as resolve: resolve.return_value = book with patch("bookwyrm.importers.importer.handle_imported_book"): - import_data(self.importer.service, import_job.id) + start_import_task(self.importer.service, import_job.id) import_item = models.ImportItem.objects.get(job=import_job, index=0) self.assertEqual(import_item.book.id, book.id)