From 070fa04b63ad313fae9fe0b31f2819e90dfa18e3 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Tue, 19 Jan 2021 23:40:11 -0800 Subject: [PATCH] Add validators and more tests I don't think these validators will do anything unless we use them or are submitting a form, but they're there nonetheless --- bookwyrm/models/readthrough.py | 4 +- .../tests/models/test_readthrough_model.py | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 bookwyrm/tests/models/test_readthrough_model.py diff --git a/bookwyrm/models/readthrough.py b/bookwyrm/models/readthrough.py index 8ec3c9c6..7daafaaf 100644 --- a/bookwyrm/models/readthrough.py +++ b/bookwyrm/models/readthrough.py @@ -1,6 +1,7 @@ ''' progress in a book ''' from django.db import models from django.utils import timezone +from django.core import validators from .base_model import BookWyrmModel @@ -13,6 +14,7 @@ class ReadThrough(BookWyrmModel): user = models.ForeignKey('User', on_delete=models.PROTECT) book = models.ForeignKey('Edition', on_delete=models.PROTECT) progress = models.IntegerField( + validators=[validators.MinValueValidator(0)], null=True, blank=True) progress_mode = models.CharField( @@ -43,7 +45,7 @@ class ProgressUpdate(BookWyrmModel): ''' Store progress through a book in the database. ''' user = models.ForeignKey('User', on_delete=models.PROTECT) readthrough = models.ForeignKey('ReadThrough', on_delete=models.CASCADE) - progress = models.IntegerField() + progress = models.IntegerField(validators=[validators.MinValueValidator(0)]) mode = models.CharField( max_length=3, choices=ProgressMode.choices, diff --git a/bookwyrm/tests/models/test_readthrough_model.py b/bookwyrm/tests/models/test_readthrough_model.py new file mode 100644 index 00000000..3fcdf1e4 --- /dev/null +++ b/bookwyrm/tests/models/test_readthrough_model.py @@ -0,0 +1,51 @@ +''' testing models ''' +from django.test import TestCase +from django.core.exceptions import ValidationError + +from bookwyrm import models, settings + + +class ReadThrough(TestCase): + ''' some activitypub oddness ahead ''' + def setUp(self): + ''' look, a shelf ''' + self.user = models.User.objects.create_user( + 'mouse', 'mouse@mouse.mouse', 'mouseword', + local=True, localname='mouse') + + 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.readthrough = models.ReadThrough.objects.create( + user=self.user, + book=self.edition) + + 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() + + updates = self.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() + self.assertRaises(ValidationError, update.clean_fields)