def test_multiple_valid_values(): field = IntegerField('name', multiple=True) assert field.validate([1, 2, 4, 8]) == [1, 2, 4, 8] assert field.validate(['1', 4, '6']) == [1, 4, 6]
def test_validate_none_required(): field = IntegerField('name', required=True) with pytest.raises(ValidationError): field.validate(None)
def test_validate_less_than_max(): field = IntegerField('name', max=7) assert field.validate(6) == 6
def test_validate_more_than_min(): field = IntegerField('name', min=7) assert field.validate(8) == 8
def default_field(): return IntegerField('name')
def test_validate_equal_to_max(): field = IntegerField('name', max=7) assert field.validate(7) == 7
def test_validate_less_than_min(): field = IntegerField('name', min=7) with pytest.raises(ValidationError): field.validate(6)
def test_validate_more_than_max(): field = IntegerField('name', max=7) with pytest.raises(ValidationError): field.validate(8)
def test_validate_invalid_value_with_default(): field = IntegerField('name', default=4, max=8) with pytest.raises(ValidationError): field.validate(10)
def test_required(self): field = IntegerField('', required=True) with self.assertRaises(ValidationError) as cm: field.validate(None) self.assertEqual(cm.exception.code, 'required')
def test_validate_empty_with_default(): field = IntegerField('name,', default=3) assert field.validate('') == 3 assert field.validate(()) == 3
def test_validate_valid_value_with_default(): field = IntegerField('name', default=4, max=8) assert field.validate(1) == 1 assert field.validate(0) == 0
def test_validate_none_with_default(): field = IntegerField('name', default=5) assert field.validate(None) == 5 field = IntegerField('name', default=0) assert field.validate(None) == 0
def test_validate_none_not_required(): field = IntegerField('name', required=False) assert field.validate(None) is None
def test_validate_equal_to_min(): field = IntegerField('name', min=7) assert field.validate(7)
def test_multiple_invalid_value(): field = IntegerField('name', multiple=True) with pytest.raises(ValidationError): field.validate([4, 5, 'a'])
def test_cleaned_data(self): field = IntegerField('', default=4) self.assertEqual(field.cleaned_value, 4) field.value = "True" with self.assertRaises(ValidationError): # noinspection PyStatementEffect field.cleaned_value field.value = '10' self.assertEqual(field.cleaned_value, 10)
def test_is_valid_int(self): field = IntegerField('') field.value = 10 self.assertTrue(field.is_valid) field.value = "10" self.assertTrue(field.is_valid) field.value = "3.5" self.assertFalse(field.is_valid) field.value = "-4" self.assertTrue(field.is_valid)
def test_is_valid_min(self): field = IntegerField('', minimum=-10) field.value = -10 self.assertTrue(field.is_valid) field.value = -11 self.assertFalse(field.is_valid)
def test_is_valid_max(self): field = IntegerField('', maximum=20) field.value = 20 self.assertTrue(field.is_valid) field.value = 21 self.assertFalse(field.is_valid)