2021-03-08 11:49:10 -05:00
|
|
|
""" tests updating reading progress """
|
2021-02-08 12:38:28 -05:00
|
|
|
from datetime import datetime
|
2021-01-18 23:00:04 -05:00
|
|
|
from unittest.mock import patch
|
2020-11-27 21:06:24 -05:00
|
|
|
from django.test import TestCase, Client
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2021-01-17 16:09:49 -05:00
|
|
|
from bookwyrm import models
|
2020-11-27 21:06:24 -05:00
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
|
2021-08-03 19:21:29 -04:00
|
|
|
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
2021-09-06 17:50:33 -04:00
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
2021-03-08 11:49:10 -05:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay")
|
2021-09-06 21:39:14 -04:00
|
|
|
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
|
|
|
@patch("bookwyrm.activitystreams.remove_book_statuses_task.delay")
|
2020-11-27 21:06:24 -05:00
|
|
|
class ReadThrough(TestCase):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""readthrough tests"""
|
2021-03-08 11:49:10 -05:00
|
|
|
|
2020-11-27 21:06:24 -05:00
|
|
|
def setUp(self):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""basic user and book data"""
|
2020-11-27 21:06:24 -05:00
|
|
|
self.client = Client()
|
|
|
|
|
2021-08-02 19:05:40 -04:00
|
|
|
self.work = models.Work.objects.create(title="Example Work")
|
2020-11-27 21:06:24 -05:00
|
|
|
|
2021-08-02 19:05:40 -04:00
|
|
|
self.edition = models.Edition.objects.create(
|
|
|
|
title="Example Edition", parent_work=self.work
|
|
|
|
)
|
2020-11-27 21:06:24 -05:00
|
|
|
|
2021-09-06 17:48:45 -04:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
2021-08-03 13:25:53 -04:00
|
|
|
self.user = models.User.objects.create_user(
|
|
|
|
"cinco", "cinco@example.com", "seissiete", local=True, localname="cinco"
|
|
|
|
)
|
2020-11-27 21:06:24 -05:00
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-08 12:38:28 -05:00
|
|
|
self.client.force_login(self.user)
|
2020-11-27 21:06:24 -05:00
|
|
|
|
2021-09-06 21:55:48 -04:00
|
|
|
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
|
2021-09-06 21:58:45 -04:00
|
|
|
def test_create_basic_readthrough(self, *_):
|
2020-11-27 21:06:24 -05:00
|
|
|
"""A basic readthrough doesn't create a progress update"""
|
|
|
|
self.assertEqual(self.edition.readthrough_set.count(), 0)
|
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
self.client.post(
|
2021-06-09 14:16:52 -04:00
|
|
|
"/reading-status/start/{}".format(self.edition.id),
|
2021-03-08 11:49:10 -05:00
|
|
|
{
|
|
|
|
"start_date": "2020-11-27",
|
|
|
|
},
|
|
|
|
)
|
2020-11-27 21:06:24 -05:00
|
|
|
|
|
|
|
readthroughs = self.edition.readthrough_set.all()
|
|
|
|
self.assertEqual(len(readthroughs), 1)
|
|
|
|
self.assertEqual(readthroughs[0].progressupdate_set.count(), 0)
|
2021-02-08 12:38:28 -05:00
|
|
|
self.assertEqual(
|
2021-03-08 11:49:10 -05:00
|
|
|
readthroughs[0].start_date, datetime(2020, 11, 27, tzinfo=timezone.utc)
|
|
|
|
)
|
2020-11-28 03:07:04 -05:00
|
|
|
self.assertEqual(readthroughs[0].progress, None)
|
2020-11-27 21:06:24 -05:00
|
|
|
self.assertEqual(readthroughs[0].finish_date, None)
|
|
|
|
|
2021-09-06 21:55:48 -04:00
|
|
|
@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
|
2021-09-06 21:58:45 -04:00
|
|
|
def test_create_progress_readthrough(self, *_):
|
2021-04-26 12:15:42 -04:00
|
|
|
"""a readthrough with progress"""
|
2020-11-27 21:06:24 -05:00
|
|
|
self.assertEqual(self.edition.readthrough_set.count(), 0)
|
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
self.client.post(
|
2021-06-09 14:16:52 -04:00
|
|
|
"/reading-status/start/{}".format(self.edition.id),
|
2021-03-08 11:49:10 -05:00
|
|
|
{
|
|
|
|
"start_date": "2020-11-27",
|
|
|
|
},
|
|
|
|
)
|
2020-11-27 21:06:24 -05:00
|
|
|
|
|
|
|
readthroughs = self.edition.readthrough_set.all()
|
|
|
|
self.assertEqual(len(readthroughs), 1)
|
2021-02-08 12:38:28 -05:00
|
|
|
self.assertEqual(
|
2021-03-08 11:49:10 -05:00
|
|
|
readthroughs[0].start_date, datetime(2020, 11, 27, tzinfo=timezone.utc)
|
|
|
|
)
|
2020-11-27 21:06:24 -05:00
|
|
|
self.assertEqual(readthroughs[0].finish_date, None)
|
|
|
|
|
2020-11-27 21:17:32 -05:00
|
|
|
# Update progress
|
2021-03-08 11:49:10 -05:00
|
|
|
self.client.post(
|
|
|
|
"/edit-readthrough",
|
|
|
|
{
|
|
|
|
"id": readthroughs[0].id,
|
|
|
|
"progress": 100,
|
|
|
|
},
|
|
|
|
)
|
2020-11-27 21:17:32 -05:00
|
|
|
|
2021-03-08 11:49:10 -05:00
|
|
|
progress_updates = (
|
|
|
|
readthroughs[0].progressupdate_set.order_by("updated_date").all()
|
|
|
|
)
|
2021-06-08 18:03:50 -04:00
|
|
|
self.assertEqual(len(progress_updates), 1)
|
|
|
|
self.assertEqual(progress_updates[0].mode, models.ProgressMode.PAGE)
|
|
|
|
self.assertEqual(progress_updates[0].progress, 100)
|
2021-02-08 12:38:28 -05:00
|
|
|
|
|
|
|
# Edit doesn't publish anything
|
2021-03-08 11:49:10 -05:00
|
|
|
self.client.post(
|
|
|
|
"/delete-readthrough",
|
|
|
|
{
|
|
|
|
"id": readthroughs[0].id,
|
|
|
|
},
|
|
|
|
)
|
2021-01-20 01:30:51 -05:00
|
|
|
|
|
|
|
readthroughs = self.edition.readthrough_set.all()
|
|
|
|
updates = self.user.progressupdate_set.all()
|
|
|
|
self.assertEqual(len(readthroughs), 0)
|
|
|
|
self.assertEqual(len(updates), 0)
|