class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
class TestBookViewSet:
@pytest.fixture
def request_factory(self):
factory = APIRequestFactory()
return factory
def create_book(self, request_factory):
req = request_factory.post('api/v1/books', {
'name': 'test_from_test',
'isbn': '123-456789012',
'country': 'india',
'number_of_pages': 26,
'authors': ['test1'],
'publisher': 'pub1',
'release_date': '2019-05-26'
}, format='json')
resp = views.BookViewSet.as_view({'post': 'create'})(req)
return resp
def test_update(self, request_factory):
self.create_book(request_factory)
req = request_factory.patch('api/v1/books', {"name": "updated_name"}, content_type='application/json')
resp = views.BookViewSet.as_view({'patch': 'update'})(req, pk=1)
print(resp.data)
assert resp.status_code == 200
assert resp.data['message'] == "The book test_from_test was deleted successfully."
resp.data
中的test_update
给出以下错误。而当我在邮递员中测试API时,它会接受部分输入。
{'isbn': [ErrorDetail(string='This field is required.', code='required')], 'country': [ErrorDetail(string='This field is required.', code='required')], 'number_of_pages': [ErrorDetail(string='This field is required.', code='required')], 'publisher': [ErrorDetail(string='This field is required.', code='required')], 'release_date': [ErrorDetail(string='This field is required.', code='required')], 'authors': [ErrorDetail(string='This field is required.', code='required')]}
很乐意在serializers.py
和models.py
中提供更多详细信息。