moviewyrm/bookwyrm/tests/views/test_readthrough.py

108 lines
3.6 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" tests updating reading progress """
2021-02-08 17:38:28 +00:00
from datetime import datetime
from unittest.mock import patch
from django.test import TestCase, Client
from django.utils import timezone
2021-01-17 21:09:49 +00:00
from bookwyrm import models
2021-03-08 16:49:10 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
class ReadThrough(TestCase):
2021-04-26 16:15:42 +00:00
"""readthrough tests"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""basic user and book data"""
self.client = Client()
2021-03-08 16:49:10 +00:00
self.work = models.Work.objects.create(title="Example Work")
self.edition = models.Edition.objects.create(
2021-03-08 16:49:10 +00:00
title="Example Edition", parent_work=self.work
)
self.user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"cinco", "cinco@example.com", "seissiete", local=True, localname="cinco"
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-08 17:38:28 +00:00
self.client.force_login(self.user)
def test_create_basic_readthrough(self, delay_mock):
"""A basic readthrough doesn't create a progress update"""
self.assertEqual(self.edition.readthrough_set.count(), 0)
2021-03-08 16:49:10 +00:00
self.client.post(
"/start-reading/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
},
)
readthroughs = self.edition.readthrough_set.all()
self.assertEqual(len(readthroughs), 1)
self.assertEqual(readthroughs[0].progressupdate_set.count(), 0)
2021-02-08 17:38:28 +00:00
self.assertEqual(
2021-03-08 16:49:10 +00:00
readthroughs[0].start_date, datetime(2020, 11, 27, tzinfo=timezone.utc)
)
self.assertEqual(readthroughs[0].progress, None)
self.assertEqual(readthroughs[0].finish_date, None)
self.assertEqual(delay_mock.call_count, 1)
def test_create_progress_readthrough(self, delay_mock):
2021-04-26 16:15:42 +00:00
"""a readthrough with progress"""
self.assertEqual(self.edition.readthrough_set.count(), 0)
2021-03-08 16:49:10 +00:00
self.client.post(
"/start-reading/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
"progress": 50,
},
)
readthroughs = self.edition.readthrough_set.all()
self.assertEqual(len(readthroughs), 1)
2021-02-08 17:38:28 +00:00
self.assertEqual(
2021-03-08 16:49:10 +00:00
readthroughs[0].start_date, datetime(2020, 11, 27, tzinfo=timezone.utc)
)
self.assertEqual(readthroughs[0].progress, 50)
self.assertEqual(readthroughs[0].finish_date, None)
progress_updates = readthroughs[0].progressupdate_set.all()
self.assertEqual(len(progress_updates), 1)
self.assertEqual(progress_updates[0].mode, models.ProgressMode.PAGE)
self.assertEqual(progress_updates[0].progress, 50)
self.assertEqual(delay_mock.call_count, 1)
# Update progress
2021-03-08 16:49:10 +00:00
self.client.post(
"/edit-readthrough",
{
"id": readthroughs[0].id,
"progress": 100,
},
)
2021-03-08 16:49:10 +00:00
progress_updates = (
readthroughs[0].progressupdate_set.order_by("updated_date").all()
)
self.assertEqual(len(progress_updates), 2)
self.assertEqual(progress_updates[1].mode, models.ProgressMode.PAGE)
self.assertEqual(progress_updates[1].progress, 100)
2021-02-08 17:38:28 +00:00
# Edit doesn't publish anything
self.assertEqual(delay_mock.call_count, 1)
2021-03-08 16:49:10 +00:00
self.client.post(
"/delete-readthrough",
{
"id": readthroughs[0].id,
},
)
readthroughs = self.edition.readthrough_set.all()
updates = self.user.progressupdate_set.all()
self.assertEqual(len(readthroughs), 0)
self.assertEqual(len(updates), 0)