Skip to content

Commit

Permalink
Merge pull request #3423 from bookwyrm-social/misc-tests
Browse files Browse the repository at this point in the history
Adds some unit tests
  • Loading branch information
mouse-reeve authored Aug 28, 2024
2 parents 3555ef9 + 698a0b1 commit 4123478
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
107 changes: 107 additions & 0 deletions bookwyrm/tests/models/test_job.py
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", "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", "[email protected]", "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")
14 changes: 14 additions & 0 deletions bookwyrm/tests/templatetags/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ def test_get_isni_bio(self, *_):

result = utilities.get_isni_bio(data, self.author)
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"), "[email protected]"
)
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")

0 comments on commit 4123478

Please sign in to comment.