示例#1
0
    def test_to_representation_with_with_specified_pk_field(self):

        class FakePkField(object):

            def to_representation(self, value):
                return value

        instance = PrimaryKeyRelatedField(
            queryset=self.Publisher.objects.all(), pk_field=FakePkField()
        )
        self.assertEqual(
            instance.to_representation(self.publisher),
            self.publisher.pk
        )
示例#2
0
    def test_to_internal_value_with_specified_pk_field(self):

        class FakePkField(object):

            def to_internal_value(self, data):
                return data['id']

        instance = PrimaryKeyRelatedField(
            queryset=self.Publisher.objects.all(), pk_field=FakePkField()
        )
        self.assertEqual(
            instance.to_internal_value({'id': self.publisher.id}),
            self.publisher
        )
示例#3
0
 def test_to_representation(self):
     instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all())
     self.assertEqual(
         instance.to_representation(self.book),
         self.book.pk
     )
示例#4
0
 def test_to_internal_value_raises_type_error(self):
     instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all())
     self.assertRaises(
         ValidationError,
         instance.to_internal_value, {'id': self.book.id}
     )
示例#5
0
 def test_to_internal_value_raises_object_does_not_exist_error(self):
     instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all())
     self.assertRaises(
         ValidationError,
         instance.to_internal_value, -1
     )
示例#6
0
 def test_to_internal_value(self):
     instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all())
     self.assertEqual(
         instance.to_internal_value(self.book.id),
         self.book
     )