Skip to content

Commit b80ed1a

Browse files
committed
test(documentation): add tests for SportCertificate CBViews
1 parent 41ab904 commit b80ed1a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from django.test import TestCase, Client
2+
from django.urls import reverse
3+
from datetime import date
4+
from documentation.models import SportCertificate
5+
from profiles.models import SportDoctor
6+
7+
8+
class SportCertificateViewsTest(TestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
"""Create initial data for all tests."""
12+
cls.doctor = SportDoctor.objects.create(
13+
first_name='Giovanni',
14+
last_name='Verdi',
15+
vat_number='12345678901'
16+
)
17+
cls.certificate = SportCertificate.objects.create(
18+
facility_id='FAC123',
19+
issued_date=date(2024, 1, 1),
20+
expiration_date=date(2025, 1, 1),
21+
doctor=cls.doctor
22+
)
23+
24+
def setUp(self):
25+
self.client = Client()
26+
27+
# Test ListView
28+
def test_sport_certificate_list_view(self):
29+
"""Test that the sport certificate list view returns status 200 and uses the correct template."""
30+
url = reverse('sport_certificate_list')
31+
response = self.client.get(url)
32+
self.assertEqual(response.status_code, 200)
33+
self.assertTemplateUsed(response, 'documentation/sport_certificate_list.html')
34+
self.assertIn(self.certificate, response.context['sport_certificate_list'])
35+
36+
# Test DetailView
37+
def test_sport_certificate_detail_view(self):
38+
"""Test that the sport certificate detail view returns status 200 and displays the correct object."""
39+
url = reverse('sport_certificate_detail', kwargs={"pk":self.certificate.pk})
40+
response = self.client.get(url)
41+
self.assertEqual(response.status_code, 200)
42+
self.assertTemplateUsed(response, 'documentation/sport_certificate_detail.html')
43+
self.assertEqual(response.context['sport_certificate_detail'], self.certificate)
44+
45+
# Test CreateView
46+
def test_sport_certificate_create_view(self):
47+
"""Test creating a new sport certificate."""
48+
url = reverse('sport_certificate_create')
49+
payload = {
50+
'facility_id': 'FAC456',
51+
'issued_date': '2024-06-01',
52+
'expiration_date': '2025-06-01',
53+
'doctor': self.doctor.pk
54+
}
55+
response = self.client.post(url, payload)
56+
#
57+
self.assertEqual(response.status_code, 302)
58+
self.assertEqual(SportCertificate.objects.count(), 2)
59+
certificate = SportCertificate.objects.get(facility_id='FAC456')
60+
self.assertEqual(certificate.doctor, self.doctor)
61+
62+
# Test UpdateView #
63+
def test_sport_certificate_update_view(self):
64+
"""Test updating an existing sport certificate."""
65+
url = reverse('sport_certificate_update', kwargs={"pk": self.certificate.pk})
66+
payload = {
67+
'facility_id': 'FAC789',
68+
'issued_date': '2024-07-01',
69+
'expiration_date': '2025-07-01',
70+
'doctor': self.doctor.pk
71+
}
72+
response = self.client.post(url, payload)
73+
self.assertEqual(response.status_code, 302)
74+
self.certificate.refresh_from_db()
75+
self.assertEqual(self.certificate.facility_id, 'FAC789')
76+
77+
# Test DeleteView #
78+
def test_sport_certificate_delete_view(self):
79+
"""Test deleting an existing sport certificate."""
80+
url = reverse('sport_certificate_delete', kwargs={"pk": self.certificate.pk})
81+
response = self.client.post(url)
82+
self.assertEqual(response.status_code, 302)
83+
self.assertFalse(SportCertificate.objects.filter(pk=self.certificate.pk).exists())

documentation/views/sport_certificates.py

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SportCertificateCreateView(CreateView):
2323
model = SportCertificate
2424
form_class = SportCertificateForm
2525
template_name = "documentation/sport_certificate_form.html"
26+
# Redirect to the list view after creation (status code 302)
2627
success_url = reverse_lazy("sport_certificate_list")
2728

2829

@@ -32,6 +33,7 @@ class SportCertificateUpdateView(UpdateView):
3233
form_class = SportCertificateForm
3334
template_name = "documentation/sport_certificate_form.html"
3435

36+
# Override the get_success_url method to redirect (302) to the detail view after updating
3537
def get_success_url(self):
3638
return reverse_lazy("sport_certificate_detail", kwargs={"pk": self.object.pk})
3739

@@ -40,4 +42,5 @@ class SportCertificateDeleteView(DeleteView):
4042
"""View for deleting a sport certificate."""
4143
model = SportCertificate
4244
template_name = "documentation/sport_certificate_confirm_delete.html"
45+
# Redirect to the list view after deletion (status code 302)
4346
success_url = reverse_lazy("sport_certificate_list")

0 commit comments

Comments
 (0)