From 224345c80468ca97dd6a927cd03c0c9fc6a007a1 Mon Sep 17 00:00:00 2001 From: LuizaMaluf Date: Tue, 14 Nov 2023 19:22:51 -0300 Subject: [PATCH] feat(validators): contest admin form validator test --- .gitignore | 2 +- apps/contests/tests.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 68bc17f..979488d 100644 --- a/.gitignore +++ b/.gitignore @@ -84,7 +84,7 @@ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: +# intended to in multiple environments; otherwise, check them in: # .python-version # pipenv diff --git a/apps/contests/tests.py b/apps/contests/tests.py index d0eba8a..a5644bc 100644 --- a/apps/contests/tests.py +++ b/apps/contests/tests.py @@ -1,6 +1,7 @@ from datetime import timedelta from django.contrib.admin import AdminSite +from django.core.exceptions import ValidationError from django.forms import CharField, Textarea from django.test import TestCase from django.urls import resolve, reverse @@ -9,6 +10,7 @@ from apps.contests.admin import ContestAdmin, ContestModelForm from apps.contests.enums import ContestStatus from apps.contests.models import Contest +from apps.users.models import User class ContestTestCase(TestCase): @@ -18,6 +20,7 @@ def setUp(self) -> None: description="This is a test contest", cancelled=False, ) + self.user = User(username="test_user") def test_status_pending(self) -> None: self.contest.start_time = timezone.now() + timedelta(hours=1) @@ -38,6 +41,20 @@ def test_status_cancelled(self) -> None: self.contest.cancelled = True self.assertEqual(self.contest.status, ContestStatus.CANCELLED) + def test_clean_method(self) -> None: + invalid_contest = Contest( + title="Invalid Contest", + description="This contest has invalid times", + start_time=timezone.now() + timedelta(days=2), + end_time=timezone.now() + timedelta(days=1), + ) + with self.assertRaises(ValidationError) as context: + invalid_contest.clean() + + self.assertEqual( + context.exception.messages, ["Start time must be before end time."] + ) + class ContestStatusTestCase(TestCase): def test_pending(self) -> None: