jeudi 26 mars 2020

Django Rest Frame Work - Testing Request PATCH error

I am trying to test my Django Restframework API call. It all works via admin view in the browser. But the testing fails.

Here my test case:

class UrlUpdateTestCase(APITestCase):
    def setUp(self):
        url_attrs = {
            'url' : 'test.com',
            'source_url' : 'mother.com',
            'link_depth' : 3,
        }
        response = self.client.post('/api/v1/urls/new', url_attrs)

    def test_update_url(self):
        url = Url.objects.first()
        client = APIClient()
        response = client.patch(
            '/api/v1/urls/{}/'.format(url.id), data=
            {
                'url' : 'test.com.com',
                'source_url' : 'mother.com',
                'link_depth' : 3,
            },
            format='json',
        )
        if response.status_code != 201:
            print(response.content)
        updated = Url.objects.get(id=url.id)
        #print(updated)
        self.assertEqual(updated.url, 'test.com.com')

The error message in console and including print of request response.

..b'\n<!doctype html>\n<html lang="en">\n<head>\n  <title>Not Found</title>\n</head>\n<body>\n  <h1>Not Found</h1><p>The requested resource was not found on this server.</p>\n</body>\n</html>\n'
E
======================================================================
FAIL: test_update_url (denker.edge.tests.UrlUpdateTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/den/den/edge/tests.py", line 75, in test_update_url
    self.assertEqual(updated.url, 'test.com.com')
AssertionError: 'test.com' != 'test.com.com'
- test.com
+ test.com.com
?         ++++

url.py

urlpatterns = [
    path('api/v1/urls/', den.edge.api_views.UrlList.as_view()),
    path('api/v1/urls/new', den.edge.api_views.UrlCreate.as_view()),
    path('api/v1/urls/<int:id>', den.edge.api_views.UrlRetrieveUpdateDestroy.as_view()),
    path('admin/', admin.site.urls),
]

Looking at the print() of the request: it looks like the URL is not correct. I do not know why? The setup of the testcase works fine calling the creation the URL(model) entry in DB.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire