-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3423 from bookwyrm-social/misc-tests
Adds some unit tests
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") |