#27476: model Integer fields (and derivatives) do not raise a ValidationError 
while
other types do
-------------------------------------+-------------------------------------
     Reporter:  Romeo Mihalcea       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  models, integer,     |             Triage Stage:
  ValidationError                    |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Romeo Mihalcea:

Old description:

> I 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.
>
> Example:
>

> {{{
> # model
> class Worker(models.Model):
>     ...
>     disabled = models.BooleanField(default=False)
>     max_execution = models.PositiveSmallIntegerField(default=3600,
> null=False)
>

> # testcase
> class TestModelWorker(unittest.TestCase):
>
>     def test_max_execution(self):
>         # Here we expect a Python ValueError to be raised when providing
> a string
>         # as an argument to what is a `PositiveSmallIntegerField`
>         with self.assertRaises(ValueError) as e:
>             Worker.create(
>                 name="Worker",
>                 max_execution='s'
>             )
>
>         self.assertIn('invalid literal for int() with base 10',
> str(e.exception))
>
>     def test_disabled(self):
>         # While here we can expect a `ValidationError` when providing a
> string to a `BooleanField`
>         with self.assertRaises(ValidationError) as e:
>             Worker.create(
>                 name="Worker",
>                 disabled='s'
>             )
> }}}

New description:

 I 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.

 Example:


 {{{
 # model
 class Worker(models.Model):
     ...
     disabled = models.BooleanField(default=False)
     max_execution = models.PositiveSmallIntegerField(default=3600,
 null=False)


 # testcase
 class TestModelWorker(unittest.TestCase):

     def test_max_execution(self):
         # Here we expect a Python ValueError to be raised when providing a
 string
         # as an argument to what is a `PositiveSmallIntegerField`
         with self.assertRaises(ValueError) as e:
             Worker.create(
                 name="Worker",
                 max_execution='s'
             )

         self.assertIn('invalid literal for int() with base 10',
 str(e.exception))

     def test_disabled(self):
         # While here we can expect a `ValidationError` when providing a
 string to a `BooleanField`
         with self.assertRaises(ValidationError) as e:
             Worker.create(
                 name="Worker",
                 disabled='s'
             )
         self.assertIn('value must be either True or False',
 str(e.exception))
 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27476#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.f8c517885f7ecd961d0391e3b83f515e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to