Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
test(apps/contest): fix some wrong tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bitterteriyaki committed Nov 8, 2023
1 parent 9657090 commit 4e76ef1
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions apps/contests/tests_contests.py → apps/contests/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from datetime import timedelta

from django.contrib.admin import AdminSite
Expand All @@ -14,18 +13,15 @@

class ContestTestCase(TestCase):
def setUp(self) -> None:
now = timezone.now()
self.contest = Contest(
id=1,
title="Test Contest",
description="This is a test contest",
start_time=now,
end_time=now + timedelta(hours=1),
cancelled=False,
)

def test_status_pending(self) -> None:
self.contest.start_time = timezone.now() + timedelta(hours=1)
self.contest.end_time = timezone.now() + timedelta(hours=2)
self.assertEqual(self.contest.status, ContestStatus.PENDING)

def test_status_running(self) -> None:
Expand All @@ -34,6 +30,7 @@ def test_status_running(self) -> None:
self.assertEqual(self.contest.status, ContestStatus.RUNNING)

def test_status_finished(self) -> None:
self.contest.start_time = timezone.now() - timedelta(hours=2)
self.contest.end_time = timezone.now() - timedelta(hours=1)
self.assertEqual(self.contest.status, ContestStatus.FINISHED)

Expand All @@ -42,7 +39,7 @@ def test_status_cancelled(self) -> None:
self.assertEqual(self.contest.status, ContestStatus.CANCELLED)


class ContestStatusTesteCase(TestCase):
class ContestStatusTestCase(TestCase):
def test_pending(self) -> None:
self.assertEqual(ContestStatus.PENDING, "Pending")

Expand All @@ -59,42 +56,43 @@ def test_cancelled(self) -> None:
class ContestModelFormTestCase(TestCase):
def test_description_field_widget(self) -> None:
form = ContestModelForm()

description_field = form.fields["description"]
attrs = {"rows": 14, "cols": 80}

self.assertIsInstance(description_field, CharField)

self.assertIsInstance(description_field.widget, Textarea)
self.assertEqual(
description_field.widget.attrs, {"rows": 14, "cols": 80}
)

def test_model_and_fields(self) -> None:
form = ContestModelForm()
self.assertEqual(description_field.widget.attrs, attrs)

self.assertEqual(form._meta.model, Contest)
def test_form_model_to_be_contest(self) -> None:
self.assertEqual(ContestModelForm()._meta.model, Contest)


class ContestAdminTestCase(unittest.TestCase):
class ContestAdminTestCase(TestCase):
def setUp(self) -> None:
self.site = AdminSite()
self.contest_admin = ContestAdmin(Contest, self.site)
self.admin = ContestAdmin(Contest, self.site)

def test_list_display(self) -> None:
expected_list_display = ("title", "start_time", "end_time", "status")
self.assertEqual(
self.contest_admin.list_display, expected_list_display
)
list_display = self.admin.list_display
expected = ("title", "start_time", "end_time", "status")

self.assertEqual(list_display, expected)

def test_list_filter(self) -> None:
expected_list_filter = ("start_time", "end_time")
self.assertEqual(self.contest_admin.list_filter, expected_list_filter)
list_filter = self.admin.list_filter
expected = ("start_time", "end_time")

self.assertEqual(list_filter, expected)

def test_fieldsets(self) -> None:
expected_fieldsets = [
fieldsets = self.admin.fieldsets
expected = [
(("General"), {"fields": ("title", "description")}),
(("Other"), {"fields": ("start_time", "end_time", "cancelled")}),
]
self.assertEqual(self.contest_admin.fieldsets, expected_fieldsets)

self.assertEqual(fieldsets, expected)


class ContestsUrlsTestCase(TestCase):
Expand All @@ -109,8 +107,7 @@ def test_detail_url_resolves(self) -> None:
class ContestViewTestCase(TestCase):
def setUp(self) -> None:
now = timezone.now()
self.contest = Contest(
id=1,
self.contest = Contest._default_manager.create(
title="Test Contest",
description="This is a test contest",
start_time=now,
Expand All @@ -119,13 +116,13 @@ def setUp(self) -> None:
)

def test_index_view(self) -> None:
response = self.client.get(reverse("contests:index"))
response = self.client.get(reverse("home"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Contest")

def test_detail_view(self) -> None:
response = self.client.get(
reverse("contests:detail", args=[self.contest.id])
)
url = reverse("contests:detail", args=[self.contest.id])
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Contest")

0 comments on commit 4e76ef1

Please sign in to comment.