forked from mirrors/bookwyrm
Importer tests
This commit is contained in:
parent
d9d0919ad4
commit
acde30887d
8 changed files with 69 additions and 38 deletions
|
@ -30,7 +30,7 @@ class GoodreadsImport(TestCase):
|
||||||
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse", "mouse@mouse.mouse", "password", local=True
|
"mouse", "mouse@mouse.mouse", "password", local=True
|
||||||
)
|
)
|
||||||
|
|
|
@ -33,7 +33,7 @@ class GenericImporter(TestCase):
|
||||||
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse", "mouse@mouse.mouse", "password", local=True
|
"mouse", "mouse@mouse.mouse", "password", local=True
|
||||||
)
|
)
|
||||||
|
|
|
@ -32,7 +32,7 @@ class LibrarythingImport(TestCase):
|
||||||
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mmai", "mmai@mmai.mmai", "password", local=True
|
"mmai", "mmai@mmai.mmai", "password", local=True
|
||||||
)
|
)
|
||||||
|
|
|
@ -30,7 +30,7 @@ class StorygraphImport(TestCase):
|
||||||
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
self.csv = open(datafile, "r", encoding=self.importer.encoding)
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse", "mouse@mouse.mouse", "password", local=True
|
"mouse", "mouse@mouse.mouse", "password", local=True
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
""" test for app action functionality """
|
""" test for app action functionality """
|
||||||
import pathlib
|
import pathlib
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
from bookwyrm.tests.validate_html import validate_html
|
|
||||||
|
|
||||||
from bookwyrm import forms, models, views
|
from bookwyrm import forms, models, views
|
||||||
|
from bookwyrm.tests.validate_html import validate_html
|
||||||
|
|
||||||
|
|
||||||
class ImportViews(TestCase):
|
class ImportViews(TestCase):
|
||||||
|
@ -18,7 +19,7 @@ class ImportViews(TestCase):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse@local.com",
|
"mouse@local.com",
|
||||||
"mouse@mouse.mouse",
|
"mouse@mouse.mouse",
|
||||||
|
@ -44,15 +45,29 @@ class ImportViews(TestCase):
|
||||||
import_job = models.ImportJob.objects.create(user=self.local_user, mappings={})
|
import_job = models.ImportJob.objects.create(user=self.local_user, mappings={})
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.tasks.app.AsyncResult") as async_result:
|
|
||||||
async_result.return_value = []
|
|
||||||
result = view(request, import_job.id)
|
result = view(request, import_job.id)
|
||||||
|
|
||||||
self.assertIsInstance(result, TemplateResponse)
|
self.assertIsInstance(result, TemplateResponse)
|
||||||
validate_html(result.render())
|
validate_html(result.render())
|
||||||
self.assertEqual(result.status_code, 200)
|
self.assertEqual(result.status_code, 200)
|
||||||
|
|
||||||
|
def test_import_status_reformat(self):
|
||||||
|
"""there are so many views, this just makes sure it LOADS"""
|
||||||
|
view = views.ImportStatus.as_view()
|
||||||
|
import_job = models.ImportJob.objects.create(user=self.local_user, mappings={})
|
||||||
|
request = self.factory.post("")
|
||||||
|
request.user = self.local_user
|
||||||
|
with patch(
|
||||||
|
"bookwyrm.importers.goodreads_import.GoodreadsImporter.update_legacy_job"
|
||||||
|
) as mock:
|
||||||
|
result = view(request, import_job.id)
|
||||||
|
self.assertEqual(mock.call_args[0][0], import_job)
|
||||||
|
|
||||||
|
self.assertEqual(result.status_code, 302)
|
||||||
|
|
||||||
def test_start_import(self):
|
def test_start_import(self):
|
||||||
"""retry failed items"""
|
"""start a job"""
|
||||||
view = views.Import.as_view()
|
view = views.Import.as_view()
|
||||||
form = forms.ImportForm()
|
form = forms.ImportForm()
|
||||||
form.data["source"] = "Goodreads"
|
form.data["source"] = "Goodreads"
|
||||||
|
@ -74,3 +89,20 @@ class ImportViews(TestCase):
|
||||||
job = models.ImportJob.objects.get()
|
job = models.ImportJob.objects.get()
|
||||||
self.assertFalse(job.include_reviews)
|
self.assertFalse(job.include_reviews)
|
||||||
self.assertEqual(job.privacy, "public")
|
self.assertEqual(job.privacy, "public")
|
||||||
|
|
||||||
|
def test_retry_item(self):
|
||||||
|
"""try again on a single row"""
|
||||||
|
job = models.ImportJob.objects.create(user=self.local_user, mappings={})
|
||||||
|
item = models.ImportItem.objects.create(
|
||||||
|
index=0,
|
||||||
|
job=job,
|
||||||
|
book_guess=self.book,
|
||||||
|
fail_reason="no match",
|
||||||
|
data={},
|
||||||
|
normalized_data={},
|
||||||
|
)
|
||||||
|
request = self.factory.post("")
|
||||||
|
request.user = self.local_user
|
||||||
|
with patch("bookwyrm.importers.importer.import_item_task.delay") as mock:
|
||||||
|
views.retry_item(request, job.id, item.id)
|
||||||
|
self.assertEqual(mock.call_count, 1)
|
||||||
|
|
|
@ -16,7 +16,7 @@ class ImportManualReviewViews(TestCase):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse@local.com",
|
"mouse@local.com",
|
||||||
"mouse@mouse.mouse",
|
"mouse@mouse.mouse",
|
||||||
|
|
|
@ -16,7 +16,7 @@ class ImportTroubleshootViews(TestCase):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||||
):
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||||
self.local_user = models.User.objects.create_user(
|
self.local_user = models.User.objects.create_user(
|
||||||
"mouse@local.com",
|
"mouse@local.com",
|
||||||
"mouse@mouse.mouse",
|
"mouse@mouse.mouse",
|
||||||
|
|
|
@ -37,7 +37,9 @@ class Import(View):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""ingest a goodreads csv"""
|
"""ingest a goodreads csv"""
|
||||||
form = forms.ImportForm(request.POST, request.FILES)
|
form = forms.ImportForm(request.POST, request.FILES)
|
||||||
if form.is_valid():
|
if not form.is_valid():
|
||||||
|
return HttpResponseBadRequest()
|
||||||
|
|
||||||
include_reviews = request.POST.get("include_reviews") == "on"
|
include_reviews = request.POST.get("include_reviews") == "on"
|
||||||
privacy = request.POST.get("privacy")
|
privacy = request.POST.get("privacy")
|
||||||
source = request.POST.get("source")
|
source = request.POST.get("source")
|
||||||
|
@ -54,9 +56,7 @@ class Import(View):
|
||||||
try:
|
try:
|
||||||
job = importer.create_job(
|
job = importer.create_job(
|
||||||
request.user,
|
request.user,
|
||||||
TextIOWrapper(
|
TextIOWrapper(request.FILES["csv_file"], encoding=importer.encoding),
|
||||||
request.FILES["csv_file"], encoding=importer.encoding
|
|
||||||
),
|
|
||||||
include_reviews,
|
include_reviews,
|
||||||
privacy,
|
privacy,
|
||||||
)
|
)
|
||||||
|
@ -66,4 +66,3 @@ class Import(View):
|
||||||
importer.start_import(job)
|
importer.start_import(job)
|
||||||
|
|
||||||
return redirect(f"/import/{job.id}")
|
return redirect(f"/import/{job.id}")
|
||||||
return HttpResponseBadRequest()
|
|
||||||
|
|
Loading…
Reference in a new issue