Skip to content

Commit 49b0ab0

Browse files
authored
Merge pull request #19 from deronnax/real_tests
some real tests
2 parents 0f29658 + 1818c4b commit 49b0ab0

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

polls/tests.py

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1+
from django.contrib.auth import authenticate
2+
from django.contrib.auth.models import User
13
from django.test import TestCase
2-
import datetime
3-
44
from django.utils import timezone
5-
from django.urls import reverse
6-
7-
from .models import Question
8-
9-
10-
class QuestionModelTest(TestCase):
11-
def test_was_published_recently_with_future_question(self):
12-
time = timezone.now() + datetime.timedelta(days=30)
13-
future_question = Question(pub_date=time)
14-
self.assertIs(future_question.was_published_recently(), False)
15-
16-
"""
17-
was_published_recently() returns False for questions whose pub_date
18-
is older than 1 day.
19-
"""
20-
21-
22-
def test_was_published_recently_with_old_question(self):
23-
"""
24-
was_published_recently() returns False for questions whose pub_date
25-
is older than 1 day.
26-
"""
27-
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
28-
old_question = Question(pub_date=time)
29-
self.assertIs(old_question.was_published_recently(), False)
30-
31-
32-
def test_was_published_recently_with_recent_question(self):
33-
"""
34-
was_published_recently() returns True for questions whose pub_date
35-
is within the last day.
36-
"""
37-
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
38-
recent_question = Question(pub_date=time)
39-
self.assertIs(recent_question.was_published_recently(), True)
40-
41-
425

43-
# Create your tests here.
6+
from .models import Poll, Vote
7+
8+
9+
class PollModelTest(TestCase):
10+
def test_user_can_vote(self):
11+
user = User.objects.create_user('john')
12+
poll = Poll.objects.create(owner=user)
13+
self.assertTrue(poll.user_can_vote(user))
14+
15+
choice = poll.choice_set.create(choice_text='pizza')
16+
Vote.objects.create(user=user, poll=poll, choice=choice)
17+
self.assertFalse(poll.user_can_vote(user))
18+
19+
20+
class PollViewTest(TestCase):
21+
def test_home(self):
22+
response = self.client.get('/')
23+
self.assertEqual(response.status_code, 200)
24+
25+
def test_login(self):
26+
User.objects.create_user(username='john', password='rambo')
27+
response = self.client.post(
28+
'/accounts/login/', {'username': 'john', 'password': 'rambo'}
29+
)
30+
self.assertRedirects(response, '/')
31+
32+
def test_register(self):
33+
response = self.client.post(
34+
'/accounts/register/',
35+
{
36+
'username': 'johny',
37+
'password1': 'rambo',
38+
'password2': 'rambo',
39+
'email': 'johny.rambo@usarmy.gov',
40+
},
41+
)
42+
self.assertRedirects(response, '/accounts/login/')
43+
# assert that user got actually created in the backend
44+
self.assertIsNotNone(authenticate(username='johny', password='rambo'))

0 commit comments

Comments
 (0)