mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-02-21 13:26:21 +00:00
fix tests
This commit is contained in:
parent
2c231acebe
commit
765fc1e43d
2 changed files with 130 additions and 15 deletions
|
@ -1,16 +1,11 @@
|
||||||
"""test bookwyrm user export functions"""
|
"""test bookwyrm user export functions"""
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.core.serializers.json import DjangoJSONEncoder
|
|
||||||
from django.test import TestCase
|
|
||||||
from django.test.utils import override_settings
|
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
import bookwyrm.models.bookwyrm_export_job as export_job
|
|
||||||
|
|
||||||
|
|
||||||
class BookwyrmExportJob(TestCase):
|
class BookwyrmExportJob(TestCase):
|
||||||
|
@ -143,17 +138,136 @@ class BookwyrmExportJob(TestCase):
|
||||||
book=self.edition,
|
book=self.edition,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.job = models.BookwyrmExportJob.objects.create(user=self.local_user)
|
self.job = models.BookwyrmExportJob.objects.create(
|
||||||
|
user=self.local_user, export_json={}
|
||||||
|
)
|
||||||
|
|
||||||
def test_export_saved_lists_task(self):
|
def test_add_book_to_user_export_job(self):
|
||||||
"""test saved list task"""
|
"""does AddBookToUserExportJob ...add the book to the export?"""
|
||||||
|
|
||||||
|
self.job.export_json["books"] = []
|
||||||
|
self.job.save()
|
||||||
|
|
||||||
|
with patch("bookwyrm.models.bookwyrm_export_job.AddFileToTar.start_job"):
|
||||||
|
model = models.bookwyrm_export_job
|
||||||
|
edition_job = model.AddBookToUserExportJob.objects.create(
|
||||||
|
edition=self.edition, parent_job=self.job
|
||||||
|
)
|
||||||
|
|
||||||
|
edition_job.start_job()
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
self.assertIsNotNone(self.job.export_json["books"])
|
||||||
|
self.assertEqual(len(self.job.export_json["books"]), 1)
|
||||||
|
book = self.job.export_json["books"][0]
|
||||||
|
self.assertEqual(book["work"]["id"], self.work.remote_id)
|
||||||
|
self.assertEqual(len(book["authors"]), 1)
|
||||||
|
self.assertEqual(len(book["shelves"]), 1)
|
||||||
|
self.assertEqual(len(book["lists"]), 1)
|
||||||
|
self.assertEqual(len(book["comments"]), 1)
|
||||||
|
self.assertEqual(len(book["reviews"]), 1)
|
||||||
|
self.assertEqual(len(book["quotations"]), 1)
|
||||||
|
self.assertEqual(len(book["readthroughs"]), 1)
|
||||||
|
|
||||||
|
def test_start_export_task(self):
|
||||||
|
"""test saved list task saves initial json and data"""
|
||||||
|
|
||||||
with patch("bookwyrm.models.bookwyrm_export_job.json_export.delay"):
|
with patch("bookwyrm.models.bookwyrm_export_job.json_export.delay"):
|
||||||
models.bookwyrm_export_job.start_export_task(
|
models.bookwyrm_export_job.start_export_task(
|
||||||
job_id=self.job.id, no_children=False
|
job_id=self.job.id, no_children=False
|
||||||
)
|
)
|
||||||
print(self.job.user)
|
|
||||||
print(self.job.export_data)
|
self.job.refresh_from_db()
|
||||||
print(self.job.export_json)
|
|
||||||
# IDK how to test this...
|
self.assertIsNotNone(self.job.export_data)
|
||||||
pass
|
self.assertIsNotNone(self.job.export_json)
|
||||||
|
self.assertEqual(self.job.export_json["name"], self.local_user.name)
|
||||||
|
|
||||||
|
def test_export_saved_lists_task(self):
|
||||||
|
"""test export_saved_lists_task adds the saved lists"""
|
||||||
|
|
||||||
|
models.bookwyrm_export_job.export_saved_lists_task(
|
||||||
|
job_id=self.job.id, no_children=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(self.job.export_json["saved_lists"])
|
||||||
|
self.assertEqual(
|
||||||
|
self.job.export_json["saved_lists"][0], self.saved_list.remote_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_export_follows_task(self):
|
||||||
|
"""test export_follows_task adds the follows"""
|
||||||
|
|
||||||
|
models.bookwyrm_export_job.export_follows_task(
|
||||||
|
job_id=self.job.id, no_children=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(self.job.export_json["follows"])
|
||||||
|
self.assertEqual(self.job.export_json["follows"][0], self.rat_user.remote_id)
|
||||||
|
|
||||||
|
def test_export_blocks_task(self):
|
||||||
|
|
||||||
|
"""test export_blocks_task adds the blocks"""
|
||||||
|
|
||||||
|
models.bookwyrm_export_job.export_blocks_task(
|
||||||
|
job_id=self.job.id, no_children=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(self.job.export_json["blocks"])
|
||||||
|
self.assertEqual(self.job.export_json["blocks"][0], self.badger_user.remote_id)
|
||||||
|
|
||||||
|
def test_export_reading_goals_task(self):
|
||||||
|
"""test export_reading_goals_task adds the goals"""
|
||||||
|
|
||||||
|
models.bookwyrm_export_job.export_reading_goals_task(
|
||||||
|
job_id=self.job.id, no_children=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(self.job.export_json["goals"])
|
||||||
|
self.assertEqual(self.job.export_json["goals"][0]["goal"], 128937123)
|
||||||
|
|
||||||
|
def test_json_export(self):
|
||||||
|
"""test json_export job adds settings"""
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"bookwyrm.models.bookwyrm_export_job.export_saved_lists_task.delay"
|
||||||
|
), patch(
|
||||||
|
"bookwyrm.models.bookwyrm_export_job.export_follows_task.delay"
|
||||||
|
), patch(
|
||||||
|
"bookwyrm.models.bookwyrm_export_job.export_blocks_task.delay"
|
||||||
|
), patch(
|
||||||
|
"bookwyrm.models.bookwyrm_export_job.trigger_books_jobs.delay"
|
||||||
|
):
|
||||||
|
|
||||||
|
models.bookwyrm_export_job.json_export(
|
||||||
|
job_id=self.job.id, no_children=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.job.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertIsNotNone(self.job.export_json["settings"])
|
||||||
|
self.assertFalse(self.job.export_json["settings"]["show_goal"])
|
||||||
|
self.assertEqual(
|
||||||
|
self.job.export_json["settings"]["preferred_timezone"],
|
||||||
|
"America/Los Angeles",
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
self.job.export_json["settings"]["default_post_privacy"], "followers"
|
||||||
|
)
|
||||||
|
self.assertFalse(self.job.export_json["settings"]["show_suggested_users"])
|
||||||
|
|
||||||
|
def test_get_books_for_user(self):
|
||||||
|
"""does get_books_for_user get all the books"""
|
||||||
|
|
||||||
|
data = models.bookwyrm_export_job.get_books_for_user(self.local_user)
|
||||||
|
|
||||||
|
self.assertEqual(len(data), 1)
|
||||||
|
self.assertEqual(data[0].title, "Example Edition")
|
||||||
|
|
|
@ -41,6 +41,7 @@ class ExportUserViews(TestCase):
|
||||||
|
|
||||||
request = self.factory.post("")
|
request = self.factory.post("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
with patch("bookwyrm.models.bookwyrm_export_job.BookwyrmExportJob.start_job"):
|
||||||
export = views.ExportUser.as_view()(request)
|
export = views.ExportUser.as_view()(request)
|
||||||
self.assertIsInstance(export, HttpResponse)
|
self.assertIsInstance(export, HttpResponse)
|
||||||
self.assertEqual(export.status_code, 302)
|
self.assertEqual(export.status_code, 302)
|
||||||
|
|
Loading…
Reference in a new issue