moviewyrm/bookwyrm/tests/models/test_readthrough_model.py

48 lines
1.8 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-08-03 17:25:53 +00:00
from unittest.mock import patch
from django.test import TestCase
from django.core.exceptions import ValidationError
from bookwyrm import models
class ReadThrough(TestCase):
2021-04-26 16:15:42 +00:00
"""some activitypub oddness ahead"""
2021-03-08 16:49:10 +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-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
)
self.readthrough = models.ReadThrough.objects.create(
2021-03-08 16:49:10 +00:00
user=self.user, book=self.edition
)
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
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()
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)