Exemple #1
0
    def test_get_attribute_raises_not_implemented_error(self):

        class FakeObject(object):

            def __init__(self, pk):
                self.pk = pk

        obj = FakeObject(10)

        instance = RelatedField(read_only=True)
        instance.bind('pk', None)

        with self.assertRaises(NotImplementedError):
            self.assertEqual(instance.get_attribute(obj), obj.pk)
Exemple #2
0
    def test_to_internal_value_raises_error_for_empty_value(self):
        child_instance = RelatedField(read_only=True)
        instance = ManyRelatedField(
            child_relation=child_instance, allow_empty=False
        )

        with self.assertRaises(ValidationError):
            instance.to_internal_value([])
Exemple #3
0
 def test_get_value_returns_empty_value(self):
     child_instance = RelatedField(read_only=True)
     instance = ManyRelatedField(child_relation=child_instance)
     instance.bind('pk_list', None)
     self.assertEqual(
         instance.get_value({'pks': [1, 2, 3]}),
         empty
     )
Exemple #4
0
    def test_get_attribute_raises_not_implemented_error(self):

        class FakeObject(object):

            def __init__(self, pk):
                self.pk = pk

            def __str__(self):
                return str(self.pk)

        child_instance = RelatedField(read_only=True)
        instance = ManyRelatedField(child_relation=child_instance)

        with self.assertRaises(NotImplementedError):
            instance.get_attribute(FakeObject(10))
Exemple #5
0
    def test_run_validation_raises_skip_field_exception_for_empty_string(self):
        instance = RelatedField(read_only=True)

        with self.assertRaises(SkipField):
            instance.run_validation('')
Exemple #6
0
    def test_run_validation(self):
        instance = RelatedField(read_only=True)

        with self.assertRaises(SkipField):
            value = object()
            instance.run_validation(value)
Exemple #7
0
    def test_init_raises_assertion_error_for_read_only_field(self):

        with self.assertRaises(AssertionError):
            RelatedField(read_only=False)
Exemple #8
0
    def test_init_raises_assertion_error_for_not_specified_queryset(self):

        with self.assertRaises(AssertionError):
            RelatedField()
Exemple #9
0
    def test_to_internal_value_raises_error_for_not_a_list_argument(self):
        child_instance = RelatedField(read_only=True)
        instance = ManyRelatedField(child_relation=child_instance)

        with self.assertRaises(ValidationError):
            instance.to_internal_value(object())
Exemple #10
0
 def test_use_pk_only_optimization_returns_False(self):
     instance = RelatedField(read_only=True)
     self.assertFalse(instance.use_pk_only_optimization())
Exemple #11
0
    def test_get_queryset_raises_not_implemented_error(self):
        instance = RelatedField(read_only=True)

        with self.assertRaises(NotImplementedError):
            instance.get_queryset()