From e129b9c1f1787311cc94c33eb11e6f49d1d0f18a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 27 Aug 2021 11:16:51 -0700 Subject: [PATCH] Adds more readthrough model tests --- .../tests/models/test_readthrough_model.py | 78 ++++++++++++++++--- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/bookwyrm/tests/models/test_readthrough_model.py b/bookwyrm/tests/models/test_readthrough_model.py index 596753f79..2b6e4fdd2 100644 --- a/bookwyrm/tests/models/test_readthrough_model.py +++ b/bookwyrm/tests/models/test_readthrough_model.py @@ -1,7 +1,9 @@ """ testing models """ +import datetime from unittest.mock import patch from django.test import TestCase from django.core.exceptions import ValidationError +from django.utils import timezone from bookwyrm import models @@ -21,27 +23,79 @@ class ReadThrough(TestCase): title="Example Edition", parent_work=self.work ) - self.readthrough = models.ReadThrough.objects.create( - user=self.user, book=self.edition + 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, ) def test_progress_update(self): """Test progress updates""" - self.readthrough.create_update() # No-op, no progress yet - self.readthrough.progress = 10 - self.readthrough.create_update() - self.readthrough.progress = 20 - self.readthrough.progress_mode = models.ProgressMode.PERCENT - self.readthrough.create_update() + readthrough = models.ReadThrough.objects.create( + user=self.user, book=self.edition + ) - updates = self.readthrough.progressupdate_set.order_by("created_date").all() + 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) - self.readthrough.progress = -10 - self.assertRaises(ValidationError, self.readthrough.clean_fields) - update = self.readthrough.create_update() + readthrough.progress = -10 + self.assertRaises(ValidationError, readthrough.clean_fields) + update = readthrough.create_update() self.assertRaises(ValidationError, update.clean_fields)