J
J
Jekson2020-03-18 12:27:43
Django
Jekson, 2020-03-18 12:27:43

Testing a POST method using unittest Client()?

I can't understand why when testing the POST method returns me a 404 error.
There is such a class

class CsvToDatabase(APIView):
    permission_classes = (permissions.AllowAny,)
    serializer_class = VendorsCsvSerializer

    def post(self, request, format=None):
        r_data = request.data
        for data in r_data:
            внутренняя логика
            .....
            serializer = VendorsCsvSerializer(data=data)
            try:
                serializer.is_valid(raise_exception=True)
                serializer.save()
            except ValidationError:
                return Response({"errors": (serializer.errors,)},
                                status=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(request.data, status=status.HTTP_200_OK)


And here is a test
class VendorCsvCreateTest(APITestCase):
    def test_vendor_from_csv_create(self):
        url = reverse('csv_vendor_create')
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)


urls.py
urlpatterns = [
    path('csv_upload/', FileUploadView.as_view(), name='csv_upload'),
    path('from_csv_create/', CsvToDatabase.as_view(), name='csv_vendor_create'),
    path('create/', VendorsCreateView.as_view(), name='vendor_create'),]


In the CsvToDatabase class, I have two statuses: 200 in case of successful loading and 400 in case of failure. I take the same json (data = my json) that works if sent via Postman and I get 404. Where does this status come from? Everything works from the front. But if I pass an empty list instead of data, then everything is OK - it returns 200. That is, the url is working.

Error log
FAIL: test_vendor_from_csv_create (vendors.tests.VendorCsvCreateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/y700/projects/solution/apps/vendors/tests.py", line 108, in test_vendor_from_csv_create
    self.assertEqual(response.status_code, status.HTTP_200_OK)
AssertionError: 404 != 200


In theory, if the data was not correct, I would have received 400, but not 404 at all.

ps I tend to think what's the matter in content_type. I have data is a list of dictionaries data = [{}, {}] and in
response = self.client.post(url, data, format='json')
format=json is written.
How to specify the appropriate type?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question