adds tests for new task system

This commit is contained in:
Mouse Reeve 2021-11-11 15:17:32 -08:00
parent 908c9dc689
commit c33d791974
2 changed files with 24 additions and 12 deletions

View file

@ -112,14 +112,14 @@ def start_import_task(source, job_id):
"""trigger the child tasks for each row""" """trigger the child tasks for each row"""
job = ImportJob.objects.get(id=job_id) job = ImportJob.objects.get(id=job_id)
# these are sub-tasks so that one big task doesn't use up all the memory in celery # these are sub-tasks so that one big task doesn't use up all the memory in celery
for item in job.items.values("id").all(): for item in job.items.values_list("id", flat=True).all():
import_item_task.delay(source, item.id) import_item_task.delay(source, item)
@app.task(queue="low_priority") @app.task(queue="low_priority")
def import_item_task(source, item_id): def import_item_task(source, item_id):
"""resolve a row into a book""" """resolve a row into a book"""
item = models.ImportItem.objets.get(id=item_id) item = models.ImportItem.objects.get(id=item_id)
try: try:
item.resolve() item.resolve()
except Exception as err: # pylint: disable=broad-except except Exception as err: # pylint: disable=broad-except

View file

@ -10,7 +10,8 @@ import responses
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import Importer from bookwyrm.importers import Importer
from bookwyrm.importers.importer import import_data, handle_imported_book from bookwyrm.importers.importer import start_import_task, import_item_task
from bookwyrm.importers.importer import handle_imported_book
def make_date(*args): def make_date(*args):
@ -105,29 +106,40 @@ class GenericImporter(TestCase):
) )
MockTask = namedtuple("Task", ("id")) MockTask = namedtuple("Task", ("id"))
mock_task = MockTask(7) mock_task = MockTask(7)
with patch("bookwyrm.importers.importer.import_data.delay") as start: with patch("bookwyrm.importers.importer.start_import_task.delay") as start:
start.return_value = mock_task start.return_value = mock_task
self.importer.start_import(import_job) self.importer.start_import(import_job)
import_job.refresh_from_db() import_job.refresh_from_db()
self.assertEqual(import_job.task_id, "7") self.assertEqual(import_job.task_id, "7")
@responses.activate @responses.activate
def test_import_data(self, *_): def test_start_import_task(self, *_):
"""resolve entry""" """resolve entry"""
import_job = self.importer.create_job( import_job = self.importer.create_job(
self.local_user, self.csv, False, "unlisted" self.local_user, self.csv, False, "unlisted"
) )
book = models.Edition.objects.create(title="Test Book")
with patch("bookwyrm.importers.importer.import_item_task.delay") as mock:
start_import_task(self.importer.service, import_job.id)
self.assertEqual(mock.call_count, 4)
@responses.activate
def test_import_item_task(self, *_):
"""resolve entry"""
import_job = self.importer.create_job(
self.local_user, self.csv, False, "unlisted"
)
import_item = models.ImportItem.objects.get(job=import_job, index=0)
with patch( with patch(
"bookwyrm.models.import_job.ImportItem.get_book_from_isbn" "bookwyrm.models.import_job.ImportItem.get_book_from_isbn"
) as resolve: ) as resolve:
resolve.return_value = book resolve.return_value = self.book
with patch("bookwyrm.importers.importer.handle_imported_book"): import_item_task(self.importer.service, import_item.id)
import_data(self.importer.service, import_job.id) import_item.refresh_from_db()
import_item = models.ImportItem.objects.get(job=import_job, index=0) self.assertEqual(import_item.book.id, self.book.id)
self.assertEqual(import_item.book.id, book.id)
def test_handle_imported_book(self, *_): def test_handle_imported_book(self, *_):
"""import added a book, this adds related connections""" """import added a book, this adds related connections"""