2021-01-19 04:00:04 +00:00
|
|
|
from unittest.mock import patch
|
2020-11-28 02:06:24 +00:00
|
|
|
from django.test import TestCase, Client
|
|
|
|
from django.utils import timezone
|
|
|
|
from datetime import datetime
|
|
|
|
|
2021-01-17 21:09:49 +00:00
|
|
|
from bookwyrm import models
|
2020-11-28 02:06:24 +00:00
|
|
|
|
2021-01-19 04:00:04 +00:00
|
|
|
@patch('bookwyrm.broadcast.broadcast_task.delay')
|
2020-11-28 02:06:24 +00:00
|
|
|
class ReadThrough(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client = Client()
|
|
|
|
|
|
|
|
self.work = models.Work.objects.create(
|
|
|
|
title='Example Work'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.edition = models.Edition.objects.create(
|
|
|
|
title='Example Edition',
|
|
|
|
parent_work=self.work
|
|
|
|
)
|
|
|
|
self.work.default_edition = self.edition
|
|
|
|
self.work.save()
|
|
|
|
|
2021-01-14 07:02:56 +00:00
|
|
|
self.user = models.User.objects.create_user(
|
|
|
|
'cinco', 'cinco@example.com', 'seissiete',
|
|
|
|
local=True, localname='cinco')
|
2020-11-28 02:06:24 +00:00
|
|
|
|
|
|
|
self.client.force_login(self.user)
|
|
|
|
|
2021-01-19 04:00:04 +00:00
|
|
|
def test_create_basic_readthrough(self, delay_mock):
|
2020-11-28 02:06:24 +00:00
|
|
|
"""A basic readthrough doesn't create a progress update"""
|
|
|
|
self.assertEqual(self.edition.readthrough_set.count(), 0)
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.assertEqual(readthroughs[0].start_date,
|
|
|
|
datetime(2020, 11, 27, tzinfo=timezone.utc))
|
2020-11-28 08:07:04 +00:00
|
|
|
self.assertEqual(readthroughs[0].progress, None)
|
2020-11-28 02:06:24 +00:00
|
|
|
self.assertEqual(readthroughs[0].finish_date, None)
|
2021-01-19 04:00:04 +00:00
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
2020-11-28 02:06:24 +00:00
|
|
|
|
2021-01-19 04:00:04 +00:00
|
|
|
def test_create_progress_readthrough(self, delay_mock):
|
2020-11-28 02:06:24 +00:00
|
|
|
self.assertEqual(self.edition.readthrough_set.count(), 0)
|
|
|
|
|
|
|
|
self.client.post('/start-reading/{}'.format(self.edition.id), {
|
|
|
|
'start_date': '2020-11-27',
|
2020-11-28 08:07:04 +00:00
|
|
|
'progress': 50,
|
2020-11-28 02:06:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
readthroughs = self.edition.readthrough_set.all()
|
|
|
|
self.assertEqual(len(readthroughs), 1)
|
|
|
|
self.assertEqual(readthroughs[0].start_date,
|
|
|
|
datetime(2020, 11, 27, tzinfo=timezone.utc))
|
2020-11-28 08:07:04 +00:00
|
|
|
self.assertEqual(readthroughs[0].progress, 50)
|
2020-11-28 02:06:24 +00:00
|
|
|
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)
|
2021-01-19 04:00:04 +00:00
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
2020-11-28 02:17:32 +00:00
|
|
|
|
|
|
|
# Update progress
|
|
|
|
self.client.post('/edit-readthrough', {
|
|
|
|
'id': readthroughs[0].id,
|
2020-11-28 08:07:04 +00:00
|
|
|
'progress': 100,
|
2020-11-28 02:17:32 +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-01-19 04:00:04 +00:00
|
|
|
self.assertEqual(delay_mock.call_count, 1) # Edit doesn't publish anything
|
2021-01-20 06:30:51 +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)
|