Skip to content

Commit

Permalink
Fixed template tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fsargent committed Jul 24, 2024
1 parent ab23a95 commit 1b0ae5f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- run: poetry install
- name: Run Tests
run: |
poetry run python manage.py test
poetry run pytest
71 changes: 43 additions & 28 deletions approval_polls/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def test_index_view_with_empty_page(self):
"""
create_poll(question="Empty page poll.")
response = self.client.get("/?page=2")
self.assertContains(response, "(page 1 of 1)", status_code=200)
print(response)

self.assertContains(response, "Page 1 of 1", status_code=200)


class PollDetailTests(TestCase):
Expand Down Expand Up @@ -200,7 +202,7 @@ def test_results_view_with_no_ballots(self):
poll = create_poll(question="Choice poll.")
poll.choice_set.create(choice_text="Choice text.")
response = self.client.get(reverse("results", args=(poll.id,)))
self.assertContains(response, "0 votes (0%)", status_code=200)
self.assertContains(response, "0 votes", status_code=200)

def test_results_view_with_ballots(self):
"""
Expand All @@ -211,7 +213,7 @@ def test_results_view_with_ballots(self):
choice = poll.choice_set.create(choice_text="Choice text.")
create_ballot(poll).vote_set.create(choice=choice)
response = self.client.get(reverse("results", args=(poll.id,)))
self.assertContains(response, "1 vote (50%)", status_code=200)
self.assertContains(response, "1 vote", status_code=200)


class PollVoteTests(TestCase):
Expand Down Expand Up @@ -354,44 +356,57 @@ def test_create_skips_blank_choices(self):
class UserProfileTests(TestCase):
def setUp(self):
self.client = Client()
User.objects.create_user("user1", "[email protected]", "password123")
self.client.login(
username="user1", email="[email protected]", password="password123"
self.user = User.objects.create_user(
"user1", "[email protected]", "password123"
)
self.client.login(username="user1", password="password123")

def test_user_profile_member_since(self):
def test_user_profile_contains_correct_info(self):
response = self.client.get(reverse("my_info"))
stored_date = User.objects.get(username="user1").date_joined
desired_date = timezone.localtime(stored_date)
test_user_date_joined = desired_date.strftime("%B %d, %Y").replace(" 0", " ")
self.assertContains(response, "Member since: " + str(test_user_date_joined))
self.assertEqual(response.status_code, 200)

def test_user_profile_last_login(self):
response = self.client.get(reverse("my_info"))
stored_date = User.objects.get(username="user1").last_login
desired_date = timezone.localtime(stored_date)
test_user_last_login = desired_date.strftime("%B %d, %Y").replace(" 0", " ")
self.assertContains(response, "Last Login: " + str(test_user_last_login))
# Check if the response contains the user's information
self.assertContains(response, self.user.username)
self.assertContains(response, self.user.email)

def test_show_polls_created_no_polls(self):
# Check if date_joined is present (we don't need to check the exact format)
self.assertContains(response, self.user.date_joined.strftime("%Y"))
self.assertContains(response, self.user.date_joined.strftime("%B"))

def test_user_profile_polls_count(self):
response = self.client.get(reverse("my_info"))
html_string = '<p><a href="/my-polls/">Polls I created</a>: 0</p>'
self.assertContains(response, html_string, html=True)
self.assertEqual(response.status_code, 200)

def test_show_polls_created_one_poll(self):
poll = Poll.objects.create(
# Initially, the user should have 0 polls
content = response.content.decode("utf-8")
self.assertRegex(
content,
r'<dt class="col-sm-3">Polls created:</dt>\s*<dd class="col-sm-9">\s*<a href="/my-polls/">0</a>',
)

# Create a poll
Poll.objects.create(
question="Which is your favorite color?",
pub_date=timezone.now() + datetime.timedelta(days=0),
user=User.objects.get(username="user1"),
pub_date=timezone.now(),
user=self.user,
vtype=2,
)

for _ in range(0):
create_ballot(poll)
# Check again, now the user should have 1 poll
response = self.client.get(reverse("my_info"))
content = response.content.decode("utf-8")
self.assertRegex(
content,
r'<dt class="col-sm-3">Polls created:</dt>\s*<dd class="col-sm-9">\s*<a href="/my-polls/">1</a>',
)

def test_user_profile_links(self):
response = self.client.get(reverse("my_info"))
html_string = '<p><a href="/my-polls/">Polls I created</a>: 1</p>'
self.assertContains(response, html_string, html=True)
self.assertEqual(response.status_code, 200)

# Check if the necessary links are present
self.assertContains(response, reverse("my_polls"))
self.assertContains(response, "/accounts/password/change")


class UpdatePollTests(TestCase):
Expand Down

0 comments on commit 1b0ae5f

Please sign in to comment.