I'm trying to test a delete endpoint, which is working when testing manually in Postman but it's failing my test with 500 error.
def test_delete_notification(self):
deleted_notification = URL + 'delete/' + str(self.notification.id)
response = requests.delete(
url=deleted_notification,
)
self.assertEqual(response.status_code, 200)
notifications = NotificationX.objects.filter(recipient_id=self.user.id,
deleted=True)
self.assertEqual(len(notifications), 1)
This is the test inside the TestCase. I'm using Django TestCase.
This is the code for the delete method:
def delete(self, request, notification_id):
notification = NotificationX.objects.get(id=notification_id)
notification.deleted = True
serialized_notification = NotificationSerializer(notification)
return Response(serialized_notification.data)
The URL:
path('delete/<uuid:notification_id>',
cls.as_view({'delete': 'delete'}, name='user_delete_notification')),
This is the error that is failing the test:
NotificationsCenter matching query does not exist.
F......S....
======================================================================
FAIL: test_delete_notification (notifications_center.notifications.tests.test_endpoints.TestNotificationsEndpoints)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/andressa/MEGAsync/Company Hero/repositories/hero-django/smart-store/smartstore/notifications_center/notifications/tests/test_endpoints.py", line 78, in test_delete_notification
self.assertEqual(response.status_code, 200)
AssertionError: 500 != 200
How can I fix this problem and make my test pass?