def test_nullable_required(self, value): not_nullable_field = fields.Field() self.assertIs(True, not_nullable_field.required) self.assertIs(False, not_nullable_field.nullable) with self.assertRaises(fields.ValidationError): not_nullable_field.clean(value) nullable_field = fields.Field(nullable=True) self.assertIs(True, nullable_field.required) self.assertIs(value, nullable_field.clean(value), value)
def test_nullable_not_required(self): not_nullable_field = fields.Field(required=False) nullable_field = fields.Field(required=False, nullable=True) value = None self.assertIs(None, not_nullable_field.clean(value)) self.assertIs(None, nullable_field.clean(value)) value = "" with self.assertRaises(fields.ValidationError): not_nullable_field.clean(value) self.assertEqual(value, nullable_field.clean(value))
def test_allowed_type_invalid(self, case): field = fields.Field(required=True, nullable=False) field.allowed_type = case["allowed_type"] value = case["value"] with self.assertRaises(fields.ValidationError): field.clean(value)
def test_choices_invalid(self, case): field = fields.Field(required=True, nullable=False) field.choices = case["choices"] value = case["value"] with self.assertRaises(fields.ValidationError): field.clean(value)
def test_allowed_type_valid(self, case): field = fields.Field(required=True, nullable=False) field.allowed_type = case["allowed_type"] value = case["value"] try: self.assertEqual(value, field.clean(value)) except fields.ValidationError: self.fail("ValidationError raised")
def test_invalid(self, case): required, nullable, allowed_type, choices, value = case field = fields.Field(required=required, nullable=nullable) field.allowed_type = allowed_type field.choices = choices with self.assertRaises(fields.ValidationError): field.clean(value)
def test_choices_valid(self, case): field = fields.Field(required=True, nullable=False) field.choices = case["choices"] value = case["value"] try: self.assertEqual(value, field.clean(value), case) except fields.ValidationError: self.fail("ValidationError raised")
def test_valid(self, case): required, nullable, allowed_type, choices, value = case field = fields.Field(required=required, nullable=nullable) field.allowed_type = allowed_type field.choices = choices try: self.assertEqual(value, field.clean(value), case) except fields.ValidationError: self.fail("ValidationError raised")
def test_not_required(self): field = fields.Field(required=False) value = None self.assertIs(None, field.clean(value))
def test_required(self): field = fields.Field() self.assertIs(True, field.required) value = None with self.assertRaises(fields.ValidationError): field.clean(value)