2016-12-19 23:47:30 +00:00
|
|
|
from django import forms
|
2016-03-22 01:06:54 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-04-23 17:13:31 +00:00
|
|
|
from django.db import IntegrityError, models, transaction
|
2016-03-22 01:06:54 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
|
|
|
|
|
|
|
from .models import BooleanModel, FksToBooleans, NullBooleanModel
|
|
|
|
|
|
|
|
|
|
|
|
class BooleanFieldTests(TestCase):
|
2016-04-23 17:13:31 +00:00
|
|
|
def _test_get_prep_value(self, f):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(f.get_prep_value(True), True)
|
|
|
|
self.assertIs(f.get_prep_value('1'), True)
|
|
|
|
self.assertIs(f.get_prep_value(1), True)
|
|
|
|
self.assertIs(f.get_prep_value(False), False)
|
|
|
|
self.assertIs(f.get_prep_value('0'), False)
|
|
|
|
self.assertIs(f.get_prep_value(0), False)
|
|
|
|
self.assertIsNone(f.get_prep_value(None))
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
def _test_to_python(self, f):
|
|
|
|
self.assertIs(f.to_python(1), True)
|
|
|
|
self.assertIs(f.to_python(0), False)
|
|
|
|
|
2016-04-23 17:13:31 +00:00
|
|
|
def test_booleanfield_get_prep_value(self):
|
|
|
|
self._test_get_prep_value(models.BooleanField())
|
2016-03-22 01:06:54 +00:00
|
|
|
|
2016-04-23 17:13:31 +00:00
|
|
|
def test_nullbooleanfield_get_prep_value(self):
|
2017-05-06 14:56:28 +00:00
|
|
|
self._test_get_prep_value(models.BooleanField(null=True))
|
|
|
|
|
|
|
|
def test_nullbooleanfield_old_get_prep_value(self):
|
2016-04-23 17:13:31 +00:00
|
|
|
self._test_get_prep_value(models.NullBooleanField())
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
def test_booleanfield_to_python(self):
|
|
|
|
self._test_to_python(models.BooleanField())
|
|
|
|
|
|
|
|
def test_nullbooleanfield_to_python(self):
|
2017-05-06 14:56:28 +00:00
|
|
|
self._test_to_python(models.BooleanField(null=True))
|
|
|
|
|
|
|
|
def test_nullbooleanfield_old_to_python(self):
|
2016-03-22 01:06:54 +00:00
|
|
|
self._test_to_python(models.NullBooleanField())
|
|
|
|
|
|
|
|
def test_booleanfield_choices_blank(self):
|
|
|
|
"""
|
|
|
|
BooleanField with choices and defaults doesn't generate a formfield
|
|
|
|
with the blank option (#9640, #10549).
|
|
|
|
"""
|
|
|
|
choices = [(1, 'Si'), (2, 'No')]
|
|
|
|
f = models.BooleanField(choices=choices, default=1, null=False)
|
|
|
|
self.assertEqual(f.formfield().choices, choices)
|
|
|
|
|
2016-12-19 23:47:30 +00:00
|
|
|
def test_nullbooleanfield_formfield(self):
|
2017-05-06 14:56:28 +00:00
|
|
|
f = models.BooleanField(null=True)
|
|
|
|
self.assertIsInstance(f.formfield(), forms.NullBooleanField)
|
2016-12-19 23:47:30 +00:00
|
|
|
f = models.NullBooleanField()
|
|
|
|
self.assertIsInstance(f.formfield(), forms.NullBooleanField)
|
|
|
|
|
2016-03-22 01:06:54 +00:00
|
|
|
def test_return_type(self):
|
|
|
|
b = BooleanModel.objects.create(bfield=True)
|
|
|
|
b.refresh_from_db()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(b.bfield, True)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
b2 = BooleanModel.objects.create(bfield=False)
|
|
|
|
b2.refresh_from_db()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(b2.bfield, False)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
2017-05-06 14:56:28 +00:00
|
|
|
b3 = NullBooleanModel.objects.create(nbfield=True, nbfield_old=True)
|
2016-03-22 01:06:54 +00:00
|
|
|
b3.refresh_from_db()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(b3.nbfield, True)
|
2017-05-06 14:56:28 +00:00
|
|
|
self.assertIs(b3.nbfield_old, True)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
2017-05-06 14:56:28 +00:00
|
|
|
b4 = NullBooleanModel.objects.create(nbfield=False, nbfield_old=False)
|
2016-03-22 01:06:54 +00:00
|
|
|
b4.refresh_from_db()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(b4.nbfield, False)
|
2017-05-06 14:56:28 +00:00
|
|
|
self.assertIs(b4.nbfield_old, False)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
# When an extra clause exists, the boolean conversions are applied with
|
|
|
|
# an offset (#13293).
|
|
|
|
b5 = BooleanModel.objects.all().extra(select={'string_col': 'string'})[0]
|
|
|
|
self.assertNotIsInstance(b5.pk, bool)
|
|
|
|
|
|
|
|
def test_select_related(self):
|
|
|
|
"""
|
|
|
|
Boolean fields retrieved via select_related() should return booleans.
|
|
|
|
"""
|
|
|
|
bmt = BooleanModel.objects.create(bfield=True)
|
|
|
|
bmf = BooleanModel.objects.create(bfield=False)
|
2017-05-06 14:56:28 +00:00
|
|
|
nbmt = NullBooleanModel.objects.create(nbfield=True, nbfield_old=True)
|
|
|
|
nbmf = NullBooleanModel.objects.create(nbfield=False, nbfield_old=False)
|
2016-03-22 01:06:54 +00:00
|
|
|
m1 = FksToBooleans.objects.create(bf=bmt, nbf=nbmt)
|
|
|
|
m2 = FksToBooleans.objects.create(bf=bmf, nbf=nbmf)
|
|
|
|
|
|
|
|
# select_related('fk_field_name')
|
|
|
|
ma = FksToBooleans.objects.select_related('bf').get(pk=m1.id)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(ma.bf.bfield, True)
|
|
|
|
self.assertIs(ma.nbf.nbfield, True)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
# select_related()
|
|
|
|
mb = FksToBooleans.objects.select_related().get(pk=m1.id)
|
|
|
|
mc = FksToBooleans.objects.select_related().get(pk=m2.id)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(mb.bf.bfield, True)
|
|
|
|
self.assertIs(mb.nbf.nbfield, True)
|
2017-05-06 14:56:28 +00:00
|
|
|
self.assertIs(mb.nbf.nbfield_old, True)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(mc.bf.bfield, False)
|
|
|
|
self.assertIs(mc.nbf.nbfield, False)
|
2017-05-06 14:56:28 +00:00
|
|
|
self.assertIs(mc.nbf.nbfield_old, False)
|
2016-03-22 01:06:54 +00:00
|
|
|
|
|
|
|
def test_null_default(self):
|
|
|
|
"""
|
|
|
|
A BooleanField defaults to None, which isn't a valid value (#15124).
|
|
|
|
"""
|
|
|
|
boolean_field = BooleanModel._meta.get_field('bfield')
|
|
|
|
self.assertFalse(boolean_field.has_default())
|
|
|
|
b = BooleanModel()
|
|
|
|
self.assertIsNone(b.bfield)
|
|
|
|
with transaction.atomic():
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
b.save()
|
|
|
|
|
|
|
|
nb = NullBooleanModel()
|
|
|
|
self.assertIsNone(nb.nbfield)
|
2017-05-06 14:56:28 +00:00
|
|
|
self.assertIsNone(nb.nbfield_old)
|
2016-03-22 01:06:54 +00:00
|
|
|
nb.save() # no error
|
|
|
|
|
|
|
|
|
|
|
|
class ValidationTest(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_boolean_field_doesnt_accept_empty_input(self):
|
|
|
|
f = models.BooleanField()
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
f.clean(None, None)
|
|
|
|
|
|
|
|
def test_nullbooleanfield_blank(self):
|
|
|
|
"""
|
|
|
|
NullBooleanField shouldn't throw a validation error when given a value
|
|
|
|
of None.
|
|
|
|
"""
|
2017-05-06 14:56:28 +00:00
|
|
|
nullboolean = NullBooleanModel(nbfield=None, nbfield_old=None)
|
2016-03-22 01:06:54 +00:00
|
|
|
nullboolean.full_clean()
|