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

[feature request] Desenvolvimento dos testes #57

Merged
merged 25 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bd4c1ab
test: initial tests development
LuizaMaluf Nov 1, 2023
073bf82
test(auth): User test case
LuizaMaluf Nov 1, 2023
0991d5f
test: User Manager test case
LuizaMaluf Nov 1, 2023
d39d725
test: User Admin test case
LuizaMaluf Nov 1, 2023
558624f
test: User Admin test case
LuizaMaluf Nov 1, 2023
bd2b726
test: Contest model test case
LuizaMaluf Nov 2, 2023
05ee3e8
test: Contest status test case
LuizaMaluf Nov 4, 2023
c57a0f8
test: Contest admin test case
LuizaMaluf Nov 4, 2023
4dfdd5f
test: Contest urls test case
LuizaMaluf Nov 4, 2023
7ab68db
test: Contest urls test case
LuizaMaluf Nov 4, 2023
82701c2
test: Problem test case
LuizaMaluf Nov 6, 2023
a38e29e
test: Problem model form test case with errors
LuizaMaluf Nov 6, 2023
13b8d26
test: Problem URLs test case
LuizaMaluf Nov 6, 2023
62a80ff
test: Problem DetailView test case
LuizaMaluf Nov 6, 2023
6cf5eee
test: Contest View test case
LuizaMaluf Nov 6, 2023
b3099c5
Merge branch 'main' into tests
LuizaMaluf Nov 7, 2023
9657090
Merge branch 'main' into tests
bitterteriyaki Nov 8, 2023
4e76ef1
test(apps/contest): fix some wrong tests
bitterteriyaki Nov 8, 2023
9244895
test(apps/problems): fix problem test with error
bitterteriyaki Nov 8, 2023
7236fcd
test(apps/problems): fix contest URLs test case
bitterteriyaki Nov 8, 2023
a2b5c47
style(apps/problems): add a blank line to make the code cleaner
bitterteriyaki Nov 8, 2023
e95d324
test(apps/problems): fix problems tests
bitterteriyaki Nov 8, 2023
888f906
test(apps/contests): add a test to reversed detail URL
bitterteriyaki Nov 8, 2023
22e98f6
test(apps/problems): fix some tests
bitterteriyaki Nov 8, 2023
38f3091
test(apps/users): fix some tests for user app
bitterteriyaki Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions apps/contests/tests_contests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import unittest
from datetime import timedelta

from django.contrib.admin import AdminSite
from django.forms import CharField, Textarea
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone

from apps.contests.admin import ContestAdmin, ContestModelForm
from apps.contests.enums import ContestStatus
from apps.contests.models import Contest


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.assertEqual(self.contest.status, ContestStatus.PENDING)

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

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

def test_status_cancelled(self) -> None:
self.contest.cancelled = True
self.assertEqual(self.contest.status, ContestStatus.CANCELLED)


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

def test_running(self) -> None:
self.assertEqual(ContestStatus.RUNNING, "Running")

def test_finished(self) -> None:
self.assertEqual(ContestStatus.FINISHED, "Finished")

def test_cancelled(self) -> None:
self.assertEqual(ContestStatus.CANCELLED, "Cancelled")


class ContestModelFormTestCase(TestCase):
def test_description_field_widget(self) -> None:
form = ContestModelForm()
description_field = form.fields["description"]

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(form._meta.model, Contest)


class ContestAdminTestCase(unittest.TestCase):
bitterteriyaki marked this conversation as resolved.
Show resolved Hide resolved
def setUp(self) -> None:
self.site = AdminSite()
self.contest_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
)

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

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


class ContestsUrlsTestCase(TestCase):
def test_detail_url_resolves(self) -> None:
url = reverse("contests:detail", args=[1])
resolved_view_name = resolve(url).view_name
expected_view_name = "contests:detail"

self.assertEqual(resolved_view_name, expected_view_name)


class ContestViewTestCase(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_index_view(self) -> None:
response = self.client.get(reverse("contests:index"))
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])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Contest")
139 changes: 139 additions & 0 deletions apps/problems/tests_problems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from datetime import timedelta

from django.contrib.admin.sites import AdminSite

# from django.forms import CharField, IntegerField
from django.test import TestCase
from django.urls import resolve, reverse
from django.utils import timezone

from apps.contests.models import Contest
from apps.problems.admin import ProblemAdmin, ProblemModelForm
from apps.problems.models import Problem
from apps.problems.views import DetailView


class ProblemTestCase(TestCase):
def test_is_accessible(self) -> None:
now = timezone.now()

start_time = now - timedelta(hours=1)
end_time = now + timedelta(hours=1)
contest = Contest(start_time=start_time, end_time=end_time)

problem = Problem(contest=contest)

self.assertTrue(problem.is_accessible)

contest.start_time = now - timedelta(hours=2)
contest.end_time = now - timedelta(hours=1)
contest.save()

self.assertTrue(problem.is_accessible)

contest.start_time = now + timedelta(hours=1)
contest.end_time = now + timedelta(hours=2)
contest.save()

self.assertFalse(problem.is_accessible)


# teste a seguir passou, porém na
# hora do commit apresenta erros
# class ProblemModelFormTesteCase(TestCase):


# def test_form_fields(self) -> None:
# data = {"description":
# "Problem description",
# "score": 10,"memory_limit":
# 1024,"time_limit": 60,}
# form = ProblemModelForm(data)
# self.assertIsInstance
# (form.fields["description"], CharField)
# self.assertIsInstance
# (form.fields["score"], IntegerField)


# self.assertIsInstance
# (form.fields["memory_limit"], IntegerField)
# self.assertIsInstance(form.fields
# ["time_limit"], IntegerField)
# self.assertEqual(
# form.fields["description"]
# .widget.attrs, {"rows": 14, "cols": 80})


# self.assertEqual(form.fields
# ["score"].min_value, 0)
# self.assertFalse(form.fields
# ["score"].required)
# self.assertEqual(form.fields
# ["memory_limit"].min_value, 0)


# self.assertFalse(form.fields
# ["memory_limit"].required)
# self.assertEqual(form.fields
# ["memory_limit"].help_text, "In bytes.")
# self.assertEqual(form.fields
# ["time_limit"].min_value, 0)
# self.assertFalse(form.fields
# ["time_limit"].required)
# self.assertEqual(form.fields["time_limit"]
# .help_text, "In seconds.")


class ProblemAdminTestCase(TestCase):
def setUp(self) -> None:
self.site = AdminSite()
self.admin = ProblemAdmin(Problem, self.site)
now = timezone.now()
self.contest = Contest(
title="Test Contest 1",
description="This is a test contest",
start_time=now,
end_time=now + timedelta(hours=1),
cancelled=False,
)
self.contest.save()

def test_admin_fields(self) -> None:
self.assertEqual(self.admin.form, ProblemModelForm)

self.assertEqual(
self.admin.list_display,
("title", "contest", "memory_limit", "time_limit"),
)
self.assertEqual(self.admin.list_filter, ("contest", "score"))

expected_fieldsets = [
(("General"), {"fields": ("title", "description")}),
(("Meta"), {"fields": ("contest", "score")}),
(("Limits"), {"fields": ("memory_limit", "time_limit")}),
]
self.assertEqual(self.admin.fieldsets, expected_fieldsets)


class ProblemURLsTestCase(TestCase):
def test_detail_url_resolves_to_detail_view(self) -> None:
url = reverse("problems:detail", args=[1])

resolver = resolve(url)

self.assertEqual(resolver.url_name, DetailView)

def test_detail_url_reverse(self) -> None:
expected_url = "/problems/1/"

generated_url = reverse("problems:detail", args=[1])

self.assertEqual(generated_url, expected_url)


class DetailViewTestCase(TestCase):
def test_detail_view_model_is_problem(self) -> None:
self.assertEqual(DetailView.model, Problem)

def test_detail_view_template_name_is_correct(self) -> None:
self.assertEqual(DetailView.template_name, "problems/detail.html")
74 changes: 74 additions & 0 deletions apps/users/tests_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.test import TestCase

from apps.users.admin import UserAdmin
from apps.users.models import User


class UserModelTestCase(TestCase):
def test_username_field_is_email(self) -> None:
self.assertEqual(User().USERNAME_FIELD, "email")

def test_required_fields_include_username(self) -> None:
self.assertIn("username", User().REQUIRED_FIELDS)


class UserManagerTestCase(TestCase):
def test_create_user(self) -> None:
user = User.objects.create_user(
username="testuser",
email="[email protected]",
password="testpassword",
)
self.assertEqual(user.email, "[email protected]")
self.assertEqual(user.username, "testuser")
self.assertFalse(user.is_staff)
self.assertTrue(user.is_active)

def test_create_superuser(self) -> None:
superuser = User.objects.create_superuser(
username="adminuser",
email="[email protected]",
password="adminpassword",
)
self.assertEqual(superuser.email, "[email protected]")
self.assertEqual(superuser.username, "adminuser")
self.assertTrue(superuser.is_staff)
self.assertTrue(superuser.is_superuser)


class UserAdminTestCase(TestCase):
def test_list_display(self) -> None:
self.assertEqual(
UserAdmin.list_display,
("username", "email", "is_staff", "is_active"),
)

def test_fieldsets(self) -> None:
expected_fieldsets = [
(("Personal info"), {"fields": ("username", "email", "password")}),
(
("Permissions"),
{
"fields": (
"user_permissions",
"groups",
"is_active",
"is_staff",
"is_superuser",
)
},
),
]
self.assertEqual(UserAdmin.fieldsets, expected_fieldsets)

def test_add_fieldsets(self) -> None:
expected_add_fieldsets = (
(
None,
{
"classes": ("wide",),
"fields": ("username", "email", "password1", "password2"),
},
),
)
self.assertEqual(UserAdmin.add_fieldsets, expected_add_fieldsets)
Loading