moviewyrm/bookwyrm/tests/models/test_readthrough_model.py

104 lines
3.5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-08-27 18:16:51 +00:00
import datetime
2021-08-03 17:25:53 +00:00
from unittest.mock import patch
from django.test import TestCase
from django.core.exceptions import ValidationError
2021-08-27 18:16:51 +00:00
from django.utils import timezone
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"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
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
)
2021-08-27 18:16:51 +00:00
def test_valid_date(self):
"""can't finish a book before you start it"""
start = timezone.now()
finish = start + datetime.timedelta(days=1)
# just make sure there's no errors
models.ReadThrough.objects.create(
user=self.user,
book=self.edition,
start_date=start,
finish_date=finish,
)
def test_valid_date_null_start(self):
"""can't finish a book before you start it"""
start = timezone.now()
finish = start + datetime.timedelta(days=1)
# just make sure there's no errors
models.ReadThrough.objects.create(
user=self.user,
book=self.edition,
finish_date=finish,
)
def test_valid_date_null_finish(self):
"""can't finish a book before you start it"""
start = timezone.now()
# just make sure there's no errors
models.ReadThrough.objects.create(
user=self.user,
book=self.edition,
start_date=start,
)
def test_valid_date_null(self):
"""can't finish a book before you start it"""
# just make sure there's no errors
models.ReadThrough.objects.create(
user=self.user,
book=self.edition,
)
def test_valid_date_same(self):
"""can't finish a book before you start it"""
start = timezone.now()
# just make sure there's no errors
models.ReadThrough.objects.create(
user=self.user,
book=self.edition,
start_date=start,
finish_date=start,
2021-03-08 16:49:10 +00:00
)
def test_progress_update(self):
2021-04-26 16:15:42 +00:00
"""Test progress updates"""
2021-08-27 18:16:51 +00:00
readthrough = models.ReadThrough.objects.create(
user=self.user, book=self.edition
)
readthrough.create_update() # No-op, no progress yet
readthrough.progress = 10
readthrough.create_update()
readthrough.progress = 20
readthrough.progress_mode = models.ProgressMode.PERCENT
readthrough.create_update()
updates = 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)
2021-08-27 18:16:51 +00:00
readthrough.progress = -10
self.assertRaises(ValidationError, readthrough.clean_fields)
update = readthrough.create_update()
self.assertRaises(ValidationError, update.clean_fields)