jeudi 30 juillet 2020

Errors in post Django testing procedure

I'm trying to make a test for incrementig votes in Django polls tutorial application.

I made two identical tests in django shell and in test.py. None of them working and results are different. The app is working well.

In the 1st case response of post request is 200 , no incrementing value. In the 2nd case a '404' ! for same command as the 1st case and error for Choice object.

These are the two situations:

*1) django shell:

Code:

#python manage.py shell < 1.py
#>>> exec(open('1.py').read())

from django.test.utils import setup_test_environment
setup_test_environment()
from django.test import Client
client = Client()

from polls.models import Choice

print("votes before",Choice.objects.get(id=7).votes)

response = client.post('/polls/3/vote/', {'name':'choice','value':'7'} )
print(response.status_code)

response = client.get('/polls/3/results/')
print(response.content)

print("votes after",Choice.objects.get(id=7).votes)

Result:

votes before 5
200
b'<h1>What do you like?</h1>\n\n<ul>\n\n    <li>Coffee -- 5 votes</li>\n\n    <li>Coca Cola -- 4 votes</li>\n\n</ul>\n\n<a href="/polls/3/">Vote again?</a>\n'
votes after 5

*2) django test:

Code:

from .models import Choice

class TestVote(TestCase):

    def test_if_vote_is_incrementing1(self):
        response = self.client.post( '/polls/3/vote/', data = {'name':'choice','value':'7'} )
        self.assertEqual(response.status_code, 200)

    def test_if_vote_is_incrementing2(self):
        votes0 = Choice.objects.get(id=7).votes
        response = self.client.post( '/polls/3/vote/', data = {'name':'choice','value':'7'} )
        votes1 = Choice.objects.get(id=7).votes
        self.assertEqual(votes1, votes0+1)

Result:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
FE
======================================================================
ERROR: test_if_vote_is_incrementing2 (polls.tests.TestVote)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mihai/all/data/work2020/django/mysite4/polls/tests.py", line 13, in test_if_vote_is_incrementing2
    votes0 = Choice.objects.get(id=7).votes
  File "/home/mihai/env_dj/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/mihai/env_dj/lib/python3.7/site-packages/django/db/models/query.py", line 417, in get
    self.model._meta.object_name
polls.models.Choice.DoesNotExist: Choice matching query does not exist.

======================================================================
FAIL: test_if_vote_is_incrementing1 (polls.tests.TestVote)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mihai/all/data/work2020/django/mysite4/polls/tests.py", line 10, in test_if_vote_is_incrementing1
    self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 2 tests in 0.006s

FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...

Aucun commentaire:

Enregistrer un commentaire