如何为在单个请求上创建多个数据库对象的 API 视图编写测试?
How to write test for API views which create multiple DB objects on single request?
我的数据库中有事务table,
我 API 根据单个请求在数据库中创建多个事务行,
我已经编写代码来测试 API 但不知何故它失败了,下面是我的代码
这是我的序列化程序:
class TransactionSerializer(serializers.ModelSerializer):
"""Transaction Serializer"""
class Meta:
"""meta class"""
model = Transaction
fields = '__all__'
read_only_fields = ['id', 'user']
extra_kwargs = {'staff': {'required': True}}
这是我的看法:
class CreateMultipleTransactionAPIView(APIView):
"""create multiple transactions"""
def post(self, request):
"""handling post request"""
serializer = TransactionSerializer(data=request.data,
many=True)
if serializer.is_valid():
serializer.save(user=self.request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
这是我的测试代码失败:
def test_create_multiple_transactions_errors(self):
url = reverse('transaction_apis:create_multiple')
request_data = {'items': [{}]}
response = self.client.post(url, request_data)
print(response.content_type)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
运行 测试后的输出:
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
AssertionError: 201 != 400
在请求 API 时只需使用 format=json 和对象列表,如下代码
def test_create_multiple_transactions_errors(self):
url = reverse('sa_transaction_apis:create_multiple')
request_data = {
'service_name': 'S2',
'service_price': 321,
'payment_type': 'W'
}
response = self.client.post(url, request_data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_multiple_transactions_success(self):
url = reverse('sa_transaction_apis:create_multiple')
datas = list()
datas.append({
'service_name': 'S2',
'service_price': 321,
'staff': self.staff.pk,
'created_at': "2016-10-12T12:31:08.553139",
'payment_type': 'W'
})
response = self.client.post(url, datas, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
测试现在使用 100% cc
我的数据库中有事务table, 我 API 根据单个请求在数据库中创建多个事务行,
我已经编写代码来测试 API 但不知何故它失败了,下面是我的代码
这是我的序列化程序:
class TransactionSerializer(serializers.ModelSerializer):
"""Transaction Serializer"""
class Meta:
"""meta class"""
model = Transaction
fields = '__all__'
read_only_fields = ['id', 'user']
extra_kwargs = {'staff': {'required': True}}
这是我的看法:
class CreateMultipleTransactionAPIView(APIView):
"""create multiple transactions"""
def post(self, request):
"""handling post request"""
serializer = TransactionSerializer(data=request.data,
many=True)
if serializer.is_valid():
serializer.save(user=self.request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
这是我的测试代码失败:
def test_create_multiple_transactions_errors(self):
url = reverse('transaction_apis:create_multiple')
request_data = {'items': [{}]}
response = self.client.post(url, request_data)
print(response.content_type)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
运行 测试后的输出:
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
AssertionError: 201 != 400
在请求 API 时只需使用 format=json 和对象列表,如下代码
def test_create_multiple_transactions_errors(self):
url = reverse('sa_transaction_apis:create_multiple')
request_data = {
'service_name': 'S2',
'service_price': 321,
'payment_type': 'W'
}
response = self.client.post(url, request_data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_multiple_transactions_success(self):
url = reverse('sa_transaction_apis:create_multiple')
datas = list()
datas.append({
'service_name': 'S2',
'service_price': 321,
'staff': self.staff.pk,
'created_at': "2016-10-12T12:31:08.553139",
'payment_type': 'W'
})
response = self.client.post(url, datas, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
测试现在使用 100% cc