2016-04-09 17:17:34 +00:00
|
|
|
import pickle
|
|
|
|
|
|
|
|
from django.forms import BooleanField, ValidationError
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class BooleanFieldTest(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_booleanfield_clean_1(self):
|
|
|
|
f = BooleanField()
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean('')
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(None)
|
|
|
|
self.assertTrue(f.clean(True))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(False)
|
|
|
|
self.assertTrue(f.clean(1))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(0)
|
|
|
|
self.assertTrue(f.clean('Django rocks'))
|
|
|
|
self.assertTrue(f.clean('True'))
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean('False')
|
|
|
|
|
|
|
|
def test_booleanfield_clean_2(self):
|
|
|
|
f = BooleanField(required=False)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(f.clean(''), False)
|
|
|
|
self.assertIs(f.clean(None), False)
|
|
|
|
self.assertIs(f.clean(True), True)
|
|
|
|
self.assertIs(f.clean(False), False)
|
|
|
|
self.assertIs(f.clean(1), True)
|
|
|
|
self.assertIs(f.clean(0), False)
|
|
|
|
self.assertIs(f.clean('1'), True)
|
|
|
|
self.assertIs(f.clean('0'), False)
|
|
|
|
self.assertIs(f.clean('Django rocks'), True)
|
|
|
|
self.assertIs(f.clean('False'), False)
|
|
|
|
self.assertIs(f.clean('false'), False)
|
|
|
|
self.assertIs(f.clean('FaLsE'), False)
|
2016-04-09 17:17:34 +00:00
|
|
|
|
|
|
|
def test_boolean_picklable(self):
|
|
|
|
self.assertIsInstance(pickle.loads(pickle.dumps(BooleanField())), BooleanField)
|
|
|
|
|
|
|
|
def test_booleanfield_changed(self):
|
|
|
|
f = BooleanField()
|
|
|
|
self.assertFalse(f.has_changed(None, None))
|
|
|
|
self.assertFalse(f.has_changed(None, ''))
|
|
|
|
self.assertFalse(f.has_changed('', None))
|
|
|
|
self.assertFalse(f.has_changed('', ''))
|
|
|
|
self.assertTrue(f.has_changed(False, 'on'))
|
|
|
|
self.assertFalse(f.has_changed(True, 'on'))
|
|
|
|
self.assertTrue(f.has_changed(True, ''))
|
|
|
|
# Initial value may have mutated to a string due to show_hidden_initial (#19537)
|
|
|
|
self.assertTrue(f.has_changed('False', 'on'))
|
2016-04-24 19:06:30 +00:00
|
|
|
# HiddenInput widget sends string values for boolean but doesn't clean them in value_from_datadict
|
|
|
|
self.assertFalse(f.has_changed(False, 'False'))
|
|
|
|
self.assertFalse(f.has_changed(True, 'True'))
|
|
|
|
self.assertTrue(f.has_changed(False, 'True'))
|
|
|
|
self.assertTrue(f.has_changed(True, 'False'))
|