Update BookwyrmExportJob tests

This commit is contained in:
Bart Schuurmans 2024-03-27 20:39:57 +01:00
parent 9afd0ebb54
commit 797d5cb508
2 changed files with 11 additions and 74 deletions

View file

@ -179,9 +179,10 @@ def export_json(user: User):
def export_user(user: User): def export_user(user: User):
"""export user data""" """export user data"""
data = user.to_activity() data = user.to_activity()
data["icon"]["url"] = ( if data.get("icon", False):
url2relativepath(data["icon"]["url"]) if data.get("icon", False) else {} data["icon"]["url"] = url2relativepath(data["icon"]["url"])
) else:
data["icon"] = {}
return data return data
@ -236,7 +237,7 @@ def export_book(user: User, edition: Edition):
data["work"] = edition.parent_work.to_activity() data["work"] = edition.parent_work.to_activity()
data["edition"] = edition.to_activity() data["edition"] = edition.to_activity()
if data["edition"].get("cover"): if data["edition"].get("cover", False):
data["edition"]["cover"]["url"] = url2relativepath( data["edition"]["cover"]["url"] = url2relativepath(
data["edition"]["cover"]["url"] data["edition"]["cover"]["url"]
) )

View file

@ -153,25 +153,15 @@ class BookwyrmExportJob(TestCase):
book=self.edition, book=self.edition,
) )
self.job = models.BookwyrmExportJob.objects.create( self.job = models.BookwyrmExportJob.objects.create(user=self.local_user)
user=self.local_user, export_json={}
) # run the first stage of the export
with patch("bookwyrm.models.bookwyrm_export_job.create_archive_task.delay"):
models.bookwyrm_export_job.create_export_json_task(job_id=self.job.id)
self.job.refresh_from_db()
def test_add_book_to_user_export_job(self): def test_add_book_to_user_export_job(self):
"""does AddBookToUserExportJob ...add the book to the export?""" """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.assertIsNotNone(self.job.export_json["books"])
self.assertEqual(len(self.job.export_json["books"]), 1) self.assertEqual(len(self.job.export_json["books"]), 1)
book = self.job.export_json["books"][0] book = self.job.export_json["books"][0]
@ -192,27 +182,12 @@ class BookwyrmExportJob(TestCase):
def test_start_export_task(self): def test_start_export_task(self):
"""test saved list task saves initial json and data""" """test saved list task saves initial json and data"""
with patch("bookwyrm.models.bookwyrm_export_job.json_export.delay"):
models.bookwyrm_export_job.start_export_task(
job_id=self.job.id, no_children=False
)
self.job.refresh_from_db()
self.assertIsNotNone(self.job.export_data) self.assertIsNotNone(self.job.export_data)
self.assertIsNotNone(self.job.export_json) self.assertIsNotNone(self.job.export_json)
self.assertEqual(self.job.export_json["name"], self.local_user.name) self.assertEqual(self.job.export_json["name"], self.local_user.name)
def test_export_saved_lists_task(self): def test_export_saved_lists_task(self):
"""test export_saved_lists_task adds the saved lists""" """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.assertIsNotNone(self.job.export_json["saved_lists"])
self.assertEqual( self.assertEqual(
self.job.export_json["saved_lists"][0], self.saved_list.remote_id self.job.export_json["saved_lists"][0], self.saved_list.remote_id
@ -220,60 +195,21 @@ class BookwyrmExportJob(TestCase):
def test_export_follows_task(self): def test_export_follows_task(self):
"""test export_follows_task adds the follows""" """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.assertIsNotNone(self.job.export_json["follows"])
self.assertEqual(self.job.export_json["follows"][0], self.rat_user.remote_id) self.assertEqual(self.job.export_json["follows"][0], self.rat_user.remote_id)
def test_export_blocks_task(self): def test_export_blocks_task(self):
"""test export_blocks_task adds the blocks""" """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.assertIsNotNone(self.job.export_json["blocks"])
self.assertEqual(self.job.export_json["blocks"][0], self.badger_user.remote_id) self.assertEqual(self.job.export_json["blocks"][0], self.badger_user.remote_id)
def test_export_reading_goals_task(self): def test_export_reading_goals_task(self):
"""test export_reading_goals_task adds the goals""" """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.assertIsNotNone(self.job.export_json["goals"])
self.assertEqual(self.job.export_json["goals"][0]["goal"], 128937123) self.assertEqual(self.job.export_json["goals"][0]["goal"], 128937123)
def test_json_export(self): def test_json_export(self):
"""test json_export job adds settings""" """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.assertIsNotNone(self.job.export_json["settings"])
self.assertFalse(self.job.export_json["settings"]["show_goal"]) self.assertFalse(self.job.export_json["settings"]["show_goal"])
self.assertEqual( self.assertEqual(