Updates mocks in tests

This commit is contained in:
Mouse Reeve 2022-11-07 09:50:05 -08:00
parent 493fd68af4
commit 141d3aa813
8 changed files with 26 additions and 11 deletions

View file

@ -6,7 +6,7 @@ from django.test import TestCase
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import CalibreImporter from bookwyrm.importers import CalibreImporter
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
# pylint: disable=consider-using-with # pylint: disable=consider-using-with
@ -16,6 +16,7 @@ from bookwyrm.importers.importer import handle_imported_book
class CalibreImport(TestCase): class CalibreImport(TestCase):
"""importing from Calibre csv""" """importing from Calibre csv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test csv""" """use a test csv"""
self.importer = CalibreImporter() self.importer = CalibreImporter()

View file

@ -8,7 +8,7 @@ from django.test import TestCase
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import GoodreadsImporter from bookwyrm.importers import GoodreadsImporter
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
def make_date(*args): def make_date(*args):
@ -23,6 +23,7 @@ def make_date(*args):
class GoodreadsImport(TestCase): class GoodreadsImport(TestCase):
"""importing from goodreads csv""" """importing from goodreads csv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test csv""" """use a test csv"""
self.importer = GoodreadsImporter() self.importer = GoodreadsImporter()

View file

@ -1,4 +1,5 @@
""" testing import """ """ testing import """
from collections import namedtuple
import pathlib import pathlib
from unittest.mock import patch from unittest.mock import patch
import datetime import datetime
@ -9,8 +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 start_import_task, import_item_task from bookwyrm.models.import_job import start_import_task, import_item_task
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
def make_date(*args): def make_date(*args):
@ -25,6 +26,7 @@ def make_date(*args):
class GenericImporter(TestCase): class GenericImporter(TestCase):
"""importing from csv""" """importing from csv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test csv""" """use a test csv"""
@ -103,9 +105,13 @@ class GenericImporter(TestCase):
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"
) )
with patch("bookwyrm.importers.importer.start_import_task.delay") as mock: MockTask = namedtuple("Task", ("id"))
self.importer.start_import(import_job) with patch("bookwyrm.models.import_job.start_import_task.delay") as mock:
mock.return_value = MockTask(123)
import_job.start_job()
self.assertEqual(mock.call_count, 1) self.assertEqual(mock.call_count, 1)
import_job.refresh_from_db()
self.assertEqual(import_job.task_id, "123")
@responses.activate @responses.activate
def test_start_import_task(self, *_): def test_start_import_task(self, *_):
@ -114,7 +120,9 @@ class GenericImporter(TestCase):
self.local_user, self.csv, False, "unlisted" self.local_user, self.csv, False, "unlisted"
) )
with patch("bookwyrm.importers.importer.import_item_task.delay") as mock: MockTask = namedtuple("Task", ("id"))
with patch("bookwyrm.models.import_job.import_item_task.delay") as mock:
mock.return_value = MockTask(123)
start_import_task(import_job.id) start_import_task(import_job.id)
self.assertEqual(mock.call_count, 4) self.assertEqual(mock.call_count, 4)

View file

@ -8,7 +8,7 @@ from django.test import TestCase
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import LibrarythingImporter from bookwyrm.importers import LibrarythingImporter
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
def make_date(*args): def make_date(*args):
@ -23,6 +23,7 @@ def make_date(*args):
class LibrarythingImport(TestCase): class LibrarythingImport(TestCase):
"""importing from librarything tsv""" """importing from librarything tsv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test tsv""" """use a test tsv"""
self.importer = LibrarythingImporter() self.importer = LibrarythingImporter()

View file

@ -8,7 +8,7 @@ from django.test import TestCase
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import OpenLibraryImporter from bookwyrm.importers import OpenLibraryImporter
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
def make_date(*args): def make_date(*args):
@ -23,6 +23,7 @@ def make_date(*args):
class OpenLibraryImport(TestCase): class OpenLibraryImport(TestCase):
"""importing from openlibrary csv""" """importing from openlibrary csv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test csv""" """use a test csv"""
self.importer = OpenLibraryImporter() self.importer = OpenLibraryImporter()

View file

@ -8,7 +8,7 @@ from django.test import TestCase
from bookwyrm import models from bookwyrm import models
from bookwyrm.importers import StorygraphImporter from bookwyrm.importers import StorygraphImporter
from bookwyrm.importers.importer import handle_imported_book from bookwyrm.models.import_job import handle_imported_book
def make_date(*args): def make_date(*args):
@ -23,6 +23,7 @@ def make_date(*args):
class StorygraphImport(TestCase): class StorygraphImport(TestCase):
"""importing from storygraph csv""" """importing from storygraph csv"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""use a test csv""" """use a test csv"""
self.importer = StorygraphImporter() self.importer = StorygraphImporter()

View file

@ -11,6 +11,7 @@ from bookwyrm import models, views
class ImportManualReviewViews(TestCase): class ImportManualReviewViews(TestCase):
"""goodreads import views""" """goodreads import views"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""we need basic test data and mocks""" """we need basic test data and mocks"""
self.factory = RequestFactory() self.factory = RequestFactory()
@ -59,7 +60,7 @@ class ImportManualReviewViews(TestCase):
request = self.factory.post("") request = self.factory.post("")
request.user = self.local_user request.user = self.local_user
with patch("bookwyrm.importers.importer.import_item_task.delay") as mock: with patch("bookwyrm.models.import_job.import_item_task.delay") as mock:
views.approve_import_item(request, self.job.id, import_item.id) views.approve_import_item(request, self.job.id, import_item.id)
self.assertEqual(mock.call_count, 1) self.assertEqual(mock.call_count, 1)
import_item.refresh_from_db() import_item.refresh_from_db()

View file

@ -11,6 +11,7 @@ from bookwyrm import models, views
class ImportTroubleshootViews(TestCase): class ImportTroubleshootViews(TestCase):
"""goodreads import views""" """goodreads import views"""
# pylint: disable=invalid-name
def setUp(self): def setUp(self):
"""we need basic test data and mocks""" """we need basic test data and mocks"""
self.factory = RequestFactory() self.factory = RequestFactory()