Changes between Initial Version and Version 2 of Ticket #27476


Ignore:
Timestamp:
Nov 11, 2016, 5:39:03 AM (8 years ago)
Author:
Romeo Mihalcea
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #27476 – Description

    initial v2  
    11I have a model with `Decimal` fields, `Boolean` fields and some `Integer` fields. While testing with invalid data (I mostly supplied a string to these fields) I noticed that the integer fields do not raise a `ValidationError` while all of the other fields do so. Supplying a string to all Integer fields raise a `ValueError` instead which looks like all other fields have a basic validation in place while the Integers do not. I know I'm supposed to perform the validations before the data reaches the model but this is testing and I like to make sure everything is as intended.
     2
     3Example:
     4
     5
     6{{{
     7# model
     8class Worker(models.Model):
     9    ...
     10    disabled = models.BooleanField(default=False)
     11    max_execution = models.PositiveSmallIntegerField(default=3600, null=False)
     12
     13
     14# testcase
     15class TestModelWorker(unittest.TestCase):
     16
     17    def test_max_execution(self):
     18        # Here we expect a Python ValueError to be raised when providing a string
     19        # as an argument to what is a `PositiveSmallIntegerField`
     20        with self.assertRaises(ValueError) as e:
     21            Worker.create(
     22                name="Worker",
     23                max_execution='s'
     24            )
     25
     26        self.assertIn('invalid literal for int() with base 10', str(e.exception))
     27
     28    def test_disabled(self):
     29        # While here we can expect a `ValidationError` when providing a string to a `BooleanField`
     30        with self.assertRaises(ValidationError) as e:
     31            Worker.create(
     32                name="Worker",
     33                disabled='s'
     34            )
     35}}}
     36
Back to Top