예제 #1
0
 def test_bind(self):
     instance = AbstractSerializer(label='test_value')
     instance.bind('field', None)
     self.assertEqual(instance.field_name, 'field')
     self.assertEqual(instance.label, 'test_value')
     self.assertIsNone(instance.parent)
     self.assertEqual(instance.source_attrs, ['field', ])
예제 #2
0
 def test_bind_with_defined_label(self):
     instance = AbstractSerializer()
     instance.bind('test_field', None)
     self.assertEqual(instance.field_name, 'test_field')
     self.assertEqual(instance.label, 'Test field')
     self.assertIsNone(instance.parent)
     self.assertEqual(instance.source_attrs, ['test_field', ])
예제 #3
0
 def test_bind_with_defined_source(self):
     instance = AbstractSerializer(source='FakeModel.pk')
     instance.bind('test_field', None)
     self.assertEqual(instance.field_name, 'test_field')
     self.assertEqual(instance.label, 'Test field')
     self.assertIsNone(instance.parent)
     self.assertEqual(instance.source_attrs, ['FakeModel', 'pk'])
예제 #4
0
    def test_run_validation_with_empty_value(self):

        def get_default():
            return empty

        instance = AbstractSerializer(required=False)
        instance.get_default = get_default
        self.assertEqual(instance.run_validation(empty), empty)
예제 #5
0
    def test_run_validation_with_dictionary(self):

        def to_internal_value(value):
            return value

        instance = AbstractSerializer(required=False)
        instance.to_internal_value = to_internal_value
        self.assertEqual(
            instance.run_validation({"key": "value"}), {"key": "value"}
        )
예제 #6
0
 def test_init_save_args_and_kwargs(self):
     instance = AbstractSerializer(read_only=True)
     self.assertEqual(instance._args, ())
     self.assertIn('read_only', instance._kwargs)
     self.assertEqual(instance._kwargs['read_only'], True)
예제 #7
0
 def test_init_required_as_false_value(self):
     instance = AbstractSerializer(required=False)
     self.assertFalse(instance.required)
예제 #8
0
 def test_get_validators(self):
     instance = AbstractSerializer()
     self.assertEqual(instance.get_validators(), [])
예제 #9
0
    def test_validate_empty_values_raises_skip_field_exception(self):
        instance = AbstractSerializer()

        with self.assertRaises(SkipField):
            instance.partial = True
            instance.validate_empty_values(empty)
예제 #10
0
 def test_init_without_validators(self):
     instance = AbstractSerializer()
     self.assertEqual(instance.validators, [])
예제 #11
0
 def test_deepcopy(self):
     instance = AbstractSerializer(validators=[FakeValidator, ])
     instance_copy = copy.deepcopy(instance)
     self.assertIsInstance(instance_copy, AbstractSerializer)
     self.assertNotEqual(instance, instance_copy)
     self.assertEqual(instance.validators, instance_copy.validators)
예제 #12
0
 def test_run_validators_passed(self):
     validators = [FakeValidator(), ]
     instance = AbstractSerializer(validators=validators)
     self.assertIsNone(instance.run_validators({"key": "value"}))
예제 #13
0
 def test_init_required_with_empty_default_value_and_true_read_only(self):
     instance = AbstractSerializer(default=empty, read_only=True)
     self.assertFalse(instance.required)
예제 #14
0
 def test_validate_empty_values_returns_none(self):
     instance = AbstractSerializer(allow_null=True)
     self.assertEqual(
         instance.validate_empty_values(None),
         (True, None)
     )
예제 #15
0
 def test_validate_empty_values_returns_value(self):
     instance = AbstractSerializer()
     self.assertEqual(
         instance.validate_empty_values({"key": "value"}),
         (False, {"key": "value"})
     )
예제 #16
0
    def test_validate_empty_values_raises_field_is_not_null(self):
        instance = AbstractSerializer(allow_null=False)

        with self.assertRaises(ValidationError):
            instance.validate_empty_values(None)
예제 #17
0
 def test_validate_empty_values_returns_default_value_for_empty(self):
     instance = AbstractSerializer(required=False)
     self.assertEqual(
         instance.validate_empty_values(empty),
         (True, None)
     )
예제 #18
0
    def test_validate_empty_values_raises_field_is_required(self):
        instance = AbstractSerializer(required=True)

        with self.assertRaises(ValidationError):
            instance.validate_empty_values(empty)
예제 #19
0
 def test_init_required_as_true_value(self):
     instance = AbstractSerializer(required=True)
     self.assertTrue(instance.required)
예제 #20
0
    def test_run_validators_raises_validation_exception(self):
        validators = [FakeValidatorWithValidationError(), ]
        instance = AbstractSerializer(validators=validators)

        with self.assertRaises(ValidationError):
            instance.run_validators({"key": "value"})
예제 #21
0
 def test_init_required_with_true_read_only_flag(self):
     instance = AbstractSerializer(read_only=True)
     self.assertFalse(instance.required)
예제 #22
0
 def test_get_initial_method(self):
     instance = AbstractSerializer(initial='value')
     self.assertEqual(instance.get_initial(), 'value')
예제 #23
0
 def test_init_required_with_default_value(self):
     instance = AbstractSerializer(default='value')
     self.assertFalse(instance.required)
예제 #24
0
 def test_not_implemented_to_representation_method_returns_none(self):
     instance = AbstractSerializer()
     self.assertIsNone(instance.to_representation({"key": "value"}))
예제 #25
0
 def test_init_with_validators(self):
     validators = [FakeValidator(), ]
     instance = AbstractSerializer(validators=validators)
     self.assertEqual(instance._validators, validators)
예제 #26
0
    def test_raise_error_returns_validator_error(self):
        instance = AbstractSerializer(required=False)

        with self.assertRaises(ValidationError):
            instance.raise_error('null')
예제 #27
0
 def test_get_initialized_validators(self):
     instance = AbstractSerializer()
     default_validators = [FakeValidator(), ]
     instance.default_validators = default_validators
     self.assertEqual(instance.get_validators(), default_validators)
예제 #28
0
    def test_raise_error_returns_message_not_found(self):
        instance = AbstractSerializer(required=False)

        with self.assertRaises(AssertionError):
            instance.raise_error('unknown')
예제 #29
0
 def test_not_implemented_to_internal_value_method_returns_none(self):
     instance = AbstractSerializer()
     self.assertIsNone(instance.to_internal_value({"key": "value"}))
예제 #30
0
 def test_validate_empty_values_returns_default(self):
     instance = AbstractSerializer(read_only=True)
     self.assertEqual(
         instance.validate_empty_values({"key": "value"}),
         (True, None)
     )