2016-04-10 01:17:34 +08:00
|
|
|
import pickle
|
|
|
|
|
2020-02-12 21:48:49 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import BooleanField
|
2016-04-10 01:17:34 +08:00
|
|
|
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-17 02:19:18 +08: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-10 01:17:34 +08: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-25 03:06:30 +08: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'))
|
2017-07-13 22:55:32 +08:00
|
|
|
|
|
|
|
def test_disabled_has_changed(self):
|
|
|
|
f = BooleanField(disabled=True)
|
|
|
|
self.assertIs(f.has_changed('True', 'False'), False)
|