diff --git a/apps/contests/tests_contests.py b/apps/contests/tests.py similarity index 73% rename from apps/contests/tests_contests.py rename to apps/contests/tests.py index fff305c..0d536d3 100644 --- a/apps/contests/tests_contests.py +++ b/apps/contests/tests.py @@ -1,4 +1,3 @@ -import unittest from datetime import timedelta from django.contrib.admin import AdminSite @@ -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: @@ -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) @@ -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") @@ -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): @@ -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, @@ -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")