def test_cleaned_data_false(self): field = BooleanField('', required=False) for value in [0, False, 'no', 'false', 'FALSE', 'False', '0', 'null']: field.value = value self.assertIsNone( field.cleaned_value, "invalid value for %r" % (value,) )
def test_cleaned_data_true(self): field = BooleanField('', required=False) for value in [1, True, 'yes', 'true', 'TRUE', 'True', 'LOL', '1', object(), type('', (), {})]: field.value = value self.assertTrue( field.cleaned_value, "invalid value for %s" % value )
def test_is_valid(self): field = BooleanField('', required=False) field.value = True self.assertTrue(field.is_valid) field.value = "yes" self.assertTrue(field.is_valid) field.value = 0 self.assertTrue(field.is_valid) field.value = False self.assertTrue(field.is_valid)
def test_validate_empty_with_default(): field = BooleanField('name', default=True) assert field.validate({}) is True assert field.validate('') is True
def default_field(): return BooleanField('name')
def test_validate_none_with_default(): field = BooleanField('name', default=True) assert field.validate(None) is True field = BooleanField('name', default=False) assert field.validate(None) is None
def test_validation_none_not_required(): field = BooleanField('name', required=False) assert field.validate(None) is None
def test_validate_none_required(): field = BooleanField('name', required=True) with pytest.raises(ValidationError): field.validate(None)
def test_required_false_default(self): with self.assertRaises(RuntimeError): # value cannot be required and false BooleanField('', required=True, default=False)
def test_required_default_true(self): field = BooleanField('', required=True, default=True) self.assertTrue(field.is_valid)
def test_required_with_false(self): field = BooleanField('', required=True) with self.assertRaises(ValidationError) as cm: field.validate(False) self.assertEqual(cm.exception.code, 'required')
def test_required_with_true(self): field = BooleanField('', required=True) try: field.validate(True) except ValidationError: self.fail('Unexpected ValidationError')
def test_cleaned_data_none(self): field = BooleanField('', required=False) field.value = None self.assertIsNone(field.cleaned_value)