mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
[test] testing issue https://github.com/tomchristie/django-rest-framework/issues/2954
This commit is contained in:
parent
010f2ee9bd
commit
5054504aa4
40
tests/test_issue2954.py
Normal file
40
tests/test_issue2954.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from django.core.exceptions import ValidationError
|
||||
from rest_framework import serializers
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
factory = APIRequestFactory()
|
||||
|
||||
|
||||
class TestInvalidModel(TestCase):
|
||||
|
||||
def test_invalid_model(self):
|
||||
"""
|
||||
"""
|
||||
class InvalidModel(models.Model):
|
||||
afield = models.CharField(blank=True, max_length=10)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# never save this object.
|
||||
raise ValidationError('a validation error')
|
||||
|
||||
class TestSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = InvalidModel
|
||||
|
||||
serializer = TestSerializer(data={'afield': 'foo'})
|
||||
serializer.is_valid()
|
||||
self.assertRaises(ValidationError, serializer.save)
|
||||
|
||||
class TestModelViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = TestSerializer
|
||||
queryset = InvalidModel.objects.all()
|
||||
|
||||
request = factory.post('/', '{"afield": "foo"}', content_type='application/json')
|
||||
view = TestModelViewSet.as_view(actions={'post': 'create'})
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
Loading…
Reference in New Issue
Block a user