From 698a0b113cc2f91af775968b5d571ad7c94a6434 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 24 Aug 2024 14:54:52 -0700 Subject: [PATCH] Adds some unit tests --- bookwyrm/tests/models/test_job.py | 107 ++++++++++++++++++ bookwyrm/tests/templatetags/test_utilities.py | 14 +++ 2 files changed, 121 insertions(+) create mode 100644 bookwyrm/tests/models/test_job.py diff --git a/bookwyrm/tests/models/test_job.py b/bookwyrm/tests/models/test_job.py new file mode 100644 index 0000000000..41e28ba7b3 --- /dev/null +++ b/bookwyrm/tests/models/test_job.py @@ -0,0 +1,107 @@ +""" testing models """ +from unittest.mock import patch + +from django.test import TestCase + +from bookwyrm import models +from bookwyrm.models.job import ChildJob, ParentJob + + +class TestParentJob(TestCase): + """job manager""" + + @classmethod + def setUpTestData(cls): + """we're trying to transport user data""" + with ( + patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), + patch("bookwyrm.activitystreams.populate_stream_task.delay"), + patch("bookwyrm.lists_stream.populate_lists_task.delay"), + ): + cls.local_user = models.User.objects.create_user( + "mouse", "mouse@mouse.mouse", "password", local=True + ) + + def test_complete_job(self): + """mark a job as complete""" + job = ParentJob.objects.create(user=self.local_user) + self.assertFalse(job.complete) + self.assertEqual(job.status, "pending") + + job.complete_job() + + job.refresh_from_db() + self.assertTrue(job.complete) + self.assertEqual(job.status, "complete") + + def test_complete_job_with_children(self): + """mark a job with children as complete""" + job = ParentJob.objects.create(user=self.local_user) + child = ChildJob.objects.create(parent_job=job) + self.assertFalse(child.complete) + self.assertEqual(child.status, "pending") + + job.complete_job() + + child.refresh_from_db() + self.assertEqual(child.status, "stopped") + + def test_pending_child_jobs(self): + """queryset of child jobs for a parent""" + job = ParentJob.objects.create(user=self.local_user) + child = ChildJob.objects.create(parent_job=job) + ChildJob.objects.create(parent_job=job, complete=True) + + self.assertEqual(job.pending_child_jobs.count(), 1) + self.assertEqual(job.pending_child_jobs.first(), child) + + +class TestChildJob(TestCase): + """job manager""" + + @classmethod + def setUpTestData(cls): + """we're trying to transport user data""" + with ( + patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), + patch("bookwyrm.activitystreams.populate_stream_task.delay"), + patch("bookwyrm.lists_stream.populate_lists_task.delay"), + ): + cls.local_user = models.User.objects.create_user( + "mouse", "mouse@mouse.mouse", "password", local=True + ) + + def test_complete_job(self): + """a child job completed, so its parent is complete""" + job = ParentJob.objects.create(user=self.local_user) + child = ChildJob.objects.create(parent_job=job) + self.assertFalse(job.complete) + + child.complete_job() + + job.refresh_from_db() + self.assertTrue(job.complete) + self.assertEqual(job.status, "complete") + + def test_complete_job_with_siblings(self): + """a child job completed, but its parent is not complete""" + job = ParentJob.objects.create(user=self.local_user) + child = ChildJob.objects.create(parent_job=job) + ChildJob.objects.create(parent_job=job) + self.assertFalse(job.complete) + + child.complete_job() + + job.refresh_from_db() + self.assertFalse(job.complete) + + def test_set_status(self): + """a parent job is activated when a child task is activated""" + job = ParentJob.objects.create(user=self.local_user) + child = ChildJob.objects.create(parent_job=job) + self.assertEqual(job.status, "pending") + + child.set_status("active") + job.refresh_from_db() + + self.assertEqual(job.status, "active") diff --git a/bookwyrm/tests/templatetags/test_utilities.py b/bookwyrm/tests/templatetags/test_utilities.py index bfd4f41ae2..a6571075ee 100644 --- a/bookwyrm/tests/templatetags/test_utilities.py +++ b/bookwyrm/tests/templatetags/test_utilities.py @@ -89,3 +89,17 @@ def test_get_isni_bio(self, *_): result = utilities.get_isni_bio(data, self.author) self.assertEqual(result, "Author of One\\Dtwo") + + def test_id_to_username(self, *_): + """given an arbitrary remote id, return the username""" + self.assertEqual( + utilities.id_to_username("http://example.com/rat"), "rat@example.com" + ) + self.assertEqual(utilities.id_to_username(None), "a new user account") + + def test_get_file_size(self, *_): + """display the size of a file in human readable terms""" + self.assertEqual(utilities.get_file_size(5), "5.0 bytes") + self.assertEqual(utilities.get_file_size(5120), "5.00 KB") + self.assertEqual(utilities.get_file_size(5242880), "5.00 MB") + self.assertEqual(utilities.get_file_size(5368709000), "5.00 GB")