|
| 1 | +# Copyright 2020 Google LLC. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from django.test import Client, TestCase # noqa: 401 |
| 16 | +from django.urls import reverse |
| 17 | +from django.utils import timezone |
| 18 | + |
| 19 | +from .models import Choice, Question |
| 20 | + |
| 21 | + |
| 22 | +class PollViewTests(TestCase): |
| 23 | + def setUp(self): |
| 24 | + question = Question( |
| 25 | + question_text="This is a test question", |
| 26 | + pub_date=timezone.now() |
| 27 | + ) |
| 28 | + question.save() |
| 29 | + self.question = question |
| 30 | + |
| 31 | + choice = Choice( |
| 32 | + choice_text="This is a test choice", |
| 33 | + votes=0 |
| 34 | + ) |
| 35 | + choice.question = question |
| 36 | + choice.save() |
| 37 | + self.choice = choice |
| 38 | + |
| 39 | + self.client = Client() |
| 40 | + |
| 41 | + def test_index_view(self): |
| 42 | + response = self.client.get('/') |
| 43 | + assert response.status_code == 200 |
| 44 | + assert self.question.question_text in str(response.content) |
| 45 | + |
| 46 | + def test_detail_view(self): |
| 47 | + response = self.client.get( |
| 48 | + reverse('polls:detail', args=(self.question.id,))) |
| 49 | + assert response.status_code == 200 |
| 50 | + assert self.question.question_text in str(response.content) |
| 51 | + assert self.choice.choice_text in str(response.content) |
| 52 | + |
| 53 | + def test_results_view(self): |
| 54 | + response = self.client.get( |
| 55 | + reverse('polls:results', args=(self.question.id,))) |
| 56 | + assert response.status_code == 200 |
| 57 | + assert self.question.question_text in str(response.content) |
| 58 | + assert self.choice.choice_text in str(response.content) |
0 commit comments