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 )
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 )
def test_to_representation(self): instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all()) self.assertEqual( instance.to_representation(self.book), self.book.pk )
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} )
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 )
def test_to_internal_value(self): instance = PrimaryKeyRelatedField(queryset=self.Book.objects.all()) self.assertEqual( instance.to_internal_value(self.book.id), self.book )