Skip to content

Commit dfdf0f8

Browse files
committed
some real tests
1 parent fa625ae commit dfdf0f8

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

polls/tests.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
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)
155

16-
"""
17-
was_published_recently() returns False for questions whose pub_date
18-
is older than 1 day.
19-
"""
6+
from .models import Poll, Choice, Vote
207

218

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)
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))
3014

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))
3118

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)
4019

20+
class PollViewTest(TestCase):
21+
def test_home(self):
22+
response = self.client.get('/')
23+
self.assertEqual(response.status_code, 200)
4124

25+
def test_login(self):
26+
User.objects.create_user(username='john', password='rambo')
27+
response = self.client.post('/accounts/login/', {'username': 'john', 'password': 'rambo'})
28+
self.assertRedirects(response, '/')
4229

43-
# Create your tests here.
30+
def test_register(self):
31+
# print(response.context['messages'])
32+
response = self.client.post('/accounts/register/', {'username': 'johny',
33+
'password1': 'rambo',
34+
'password2': 'rambo',
35+
'email': 'johny.rambo@usarmy.gov'
36+
})
37+
self.assertRedirects(response, '/accounts/login/')
38+
# assert that user got actually created in the backend
39+
self.assertIsNotNone(authenticate(username='johny', password='rambo'))

0 commit comments

Comments
 (0)