Add initial tests and some fixes

Make timezones aware, and create a progress update if we can upon
starting a readthrough
This commit is contained in:
Joel Bradshaw 2020-11-27 18:06:24 -08:00
parent f86140c7e4
commit 6455cc7fe9
2 changed files with 71 additions and 2 deletions

View file

@ -0,0 +1,60 @@
from django.test import TestCase, Client
from django.utils import timezone
from datetime import datetime
from bookwyrm import view_actions as actions, models
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()
self.user = models.User.objects.create()
self.client.force_login(self.user)
def test_create_basic_readthrough(self):
"""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))
self.assertEqual(readthroughs[0].pages_read, None)
self.assertEqual(readthroughs[0].finish_date, None)
def test_create_progress_readthrough(self):
self.assertEqual(self.edition.readthrough_set.count(), 0)
self.client.post('/start-reading/{}'.format(self.edition.id), {
'start_date': '2020-11-27',
'pages_read': 50,
})
readthroughs = self.edition.readthrough_set.all()
self.assertEqual(len(readthroughs), 1)
self.assertEqual(readthroughs[0].start_date,
datetime(2020, 11, 27, tzinfo=timezone.utc))
self.assertEqual(readthroughs[0].pages_read, 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)

View file

@ -363,6 +363,13 @@ def start_reading(request, book_id):
if readthrough.start_date: if readthrough.start_date:
readthrough.save() readthrough.save()
# create a progress update if we have a page
if readthrough.pages_read:
readthrough.progressupdate_set.create(
user=request.user,
progress=readthrough.pages_read,
mode=models.ProgressMode.PAGE)
# shelve the book # shelve the book
if request.POST.get('reshelve', True): if request.POST.get('reshelve', True):
try: try:
@ -728,7 +735,8 @@ def update_readthrough(request, book=None, create=True):
start_date = request.POST.get('start_date') start_date = request.POST.get('start_date')
if start_date: if start_date:
try: try:
start_date = dateutil.parser.parse(start_date) start_date = timezone.make_aware(
dateutil.parser.parse(start_date))
readthrough.start_date = start_date readthrough.start_date = start_date
except ParserError: except ParserError:
pass pass
@ -736,7 +744,8 @@ def update_readthrough(request, book=None, create=True):
finish_date = request.POST.get('finish_date') finish_date = request.POST.get('finish_date')
if finish_date: if finish_date:
try: try:
finish_date = dateutil.parser.parse(finish_date) finish_date = timezone.make_aware(
dateutil.parser.parse(finish_date))
readthrough.finish_date = finish_date readthrough.finish_date = finish_date
except ParserError: except ParserError:
pass pass