Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds some unit tests #3423

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading