forked from mirrors/bookwyrm
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
This commit is contained in:
parent
edba55f7c2
commit
070fa04b63
2 changed files with 54 additions and 1 deletions
|
@ -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,
|
||||
|
|
51
bookwyrm/tests/models/test_readthrough_model.py
Normal file
51
bookwyrm/tests/models/test_readthrough_model.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue