J
J
Jekson2020-03-24 12:21:21
Unit testing
Jekson, 2020-03-24 12:21:21

DRF: Why doesn't the PUT method test pass?

With the use of Postman, the functionality is fulfilled, the instances are updated. Wrote this test

class VendorProfileUpdateViewTest(APITestCase):

    def test_check_partial_update_api(self):
        data = {"nda": "2020-11-11"}
        vendor = Vendors.objects.create(vendor_name="U4", country="Belarus", nda="2020-12-12", )
        VendorContacts.objects.create(contact_name="Mrk", phone="2373823", email="[email protected]", vendor=vendor)
        _id = vendor.vendorid
        url = reverse('vendor_update',  kwargs={'vendorid': _id})
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(vendor.nda, '2020-11-11')


And I get

FAIL: test_check_partial_update_api (vendors.tests.VendorProfileUpdateViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/y700/projects/solution/apps/vendors/tests.py", line 183, in test_check_partial_update_api
    self.assertEqual(vendor.nda, '2020-11-11')
AssertionError: '2020-12-12' != '2020-11-11'
- 2020-12-12
?       ^  ^
+ 2020-11-11
?       ^  ^


If I print out in the test what is given by the client, print(json.loads(response.content)['nda'])then I get what I expected - a new date 2020-11-11 . But assertEqual gives an error, I can’t understand what’s wrong.

views.py

class VendorProfileUpdateView(generics.RetrieveUpdateAPIView):

    serializer_class = VendorManagementUpdateSerializer
    lookup_field = 'vendorid'

    def get_queryset(self):
        vendorid = self.kwargs['vendorid']
        return Vendors.objects.filter(vendorid=vendorid)

    def put(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)


serializer.py
class VendorManagementUpdateSerializer(serializers.ModelSerializer):
    contacts = VendorContactSerializer(many=True)
    parent = serializers.PrimaryKeyRelatedField(queryset=Vendors.objects.all(), required=False, allow_null=True)

    class Meta:
        model = Vendors
        fields = ('vendorid',
                  'vendor_name',
                  'active',
                  'country',
                  'nda',
                  'parent',
                  'contacts',
                  )

    def update(self, instance, validated_data):
        # raise_errors_on_nested_writes('update', self, validated_data)

        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()

        return instance


urls.py
path('<int:vendorid>/', VendorProfileUpdateView.as_view(), name='vendor_update'),

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-03-24
@Lepilov

So perhaps it is necessary to re-calculate from the database?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question