2021-03-08 16:49:10 +00:00
|
|
|
""" testing models """
|
2021-08-03 17:25:53 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-20 07:40:11 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2021-04-28 22:19:24 +00:00
|
|
|
from bookwyrm import models
|
2021-01-20 07:40:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReadThrough(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""some activitypub oddness ahead"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-20 07:40:11 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""look, a shelf"""
|
2021-08-03 17:25:53 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
|
|
|
|
self.user = models.User.objects.create_user(
|
|
|
|
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
|
|
|
)
|
2021-01-20 07:40:11 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
self.work = models.Work.objects.create(title="Example Work")
|
|
|
|
self.edition = models.Edition.objects.create(
|
|
|
|
title="Example Edition", parent_work=self.work
|
|
|
|
)
|
2021-01-20 07:40:11 +00:00
|
|
|
|
|
|
|
self.readthrough = models.ReadThrough.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.user, book=self.edition
|
|
|
|
)
|
2021-01-20 07:40:11 +00:00
|
|
|
|
|
|
|
def test_progress_update(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""Test progress updates"""
|
2021-03-08 16:49:10 +00:00
|
|
|
self.readthrough.create_update() # No-op, no progress yet
|
2021-01-20 07:40:11 +00:00
|
|
|
self.readthrough.progress = 10
|
|
|
|
self.readthrough.create_update()
|
|
|
|
self.readthrough.progress = 20
|
|
|
|
self.readthrough.progress_mode = models.ProgressMode.PERCENT
|
|
|
|
self.readthrough.create_update()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
updates = self.readthrough.progressupdate_set.order_by("created_date").all()
|
2021-01-20 07:40:11 +00:00
|
|
|
self.assertEqual(len(updates), 2)
|
|
|
|
self.assertEqual(updates[0].progress, 10)
|
|
|
|
self.assertEqual(updates[0].mode, models.ProgressMode.PAGE)
|
|
|
|
self.assertEqual(updates[1].progress, 20)
|
|
|
|
self.assertEqual(updates[1].mode, models.ProgressMode.PERCENT)
|
|
|
|
|
|
|
|
self.readthrough.progress = -10
|
|
|
|
self.assertRaises(ValidationError, self.readthrough.clean_fields)
|
|
|
|
update = self.readthrough.create_update()
|
|
|
|
self.assertRaises(ValidationError, update.clean_fields)
|