Adds some unit tests

This commit is contained in:
Mouse Reeve 2024-08-24 14:54:52 -07:00
parent 413c26bc5e
commit 698a0b113c
2 changed files with 121 additions and 0 deletions

View file

@ -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")

View file

@ -89,3 +89,17 @@ class UtilitiesTags(TestCase):
result = utilities.get_isni_bio(data, self.author) result = utilities.get_isni_bio(data, self.author)
self.assertEqual(result, "Author of <em>One\\Dtwo</em>") self.assertEqual(result, "Author of <em>One\\Dtwo</em>")
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")