Skip to content

Commit 6719da2

Browse files
committed
test(profiles/athletes): add tests for model Athlete
1 parent 946f83b commit 6719da2

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

profiles/models/athletes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class Athlete(models.Model):
1010
fiscal_code = models.CharField(max_length=16, unique=True, verbose_name="Codice Fiscale")
1111
category = models.ForeignKey(
1212
"Category",
13-
on_delete=models.SET_NULL,
14-
null=True,
15-
blank=True,
13+
on_delete=models.PROTECT,
1614
verbose_name="Categoria",
1715
related_name="athletes"
1816
)
@@ -41,7 +39,7 @@ def __repr__(self):
4139

4240
class Category(models.Model):
4341
code = models.CharField(max_length=4, unique=True)
44-
name = models.CharField(max_length=50)
42+
description = models.CharField(max_length=50)
4543
age_range = models.CharField(max_length=20, blank=True, null=True)
4644

4745
class Meta:

profiles/tests/athletes/__init__.py

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from django.test import TestCase
2+
from profiles.models.athletes import Athlete, Category
3+
from profiles.models.trainers import Trainer
4+
5+
6+
class AthleteModelTest(TestCase):
7+
8+
@classmethod
9+
def setUpTestData(cls):
10+
"""Create a valid instance of Category and Trainer shared by all tests."""
11+
cls.category = Category.objects.create(
12+
code='JM',
13+
description='Junior Maschile',
14+
age_range='Under 20',
15+
)
16+
cls.trainer = Trainer.objects.create(
17+
first_name='Paolo',
18+
last_name='Bianchi',
19+
fiscal_code='BNCPLO80A01H211B',
20+
)
21+
22+
def setUp(self):
23+
"""Create a valid dinamic instance of Athlete for each test."""
24+
self.athlete = Athlete.objects.create(
25+
first_name='Mario',
26+
last_name='Rossi',
27+
date_of_birth='1990-01-01',
28+
place_of_birth='Roma',
29+
fiscal_code='RSSMRA90A01H211B',
30+
category=self.category,
31+
)
32+
33+
def test_athlete_creation(self):
34+
"""Test creating a new athlete without a trainer."""
35+
self.assertEqual(self.athlete.first_name, 'Mario')
36+
self.assertEqual(self.athlete.last_name, 'Rossi')
37+
self.assertEqual(self.athlete.date_of_birth, '1990-01-01')
38+
self.assertEqual(self.athlete.place_of_birth, 'Roma')
39+
self.assertEqual(self.athlete.fiscal_code, 'RSSMRA90A01H211B')
40+
self.assertEqual(self.athlete.category, self.category)
41+
self.assertEqual(self.athlete.category.code, 'JM')
42+
self.assertIsNone(self.athlete.trainer)
43+
44+
def test_athlete_with_trainer(self):
45+
"""Test creating a new athlete with a trainer."""
46+
self.athlete.trainer = self.trainer
47+
self.athlete.save()
48+
self.assertEqual(self.athlete.trainer, self.trainer)
49+
self.assertEqual(self.athlete.trainer.first_name, 'Paolo')
50+
self.assertEqual(self.athlete.trainer.last_name, 'Bianchi')
51+
self.assertEqual(self.athlete.trainer.fiscal_code, 'BNCPLO80A01H211B')
52+
53+
54+
def test_athlete_string_representation(self):
55+
"""Test __str__ method."""
56+
self.assertEqual(
57+
str(self.athlete),
58+
'Mario Rossi (RSSMRA90A01H211B)'
59+
)
60+
61+
def test__athlete_repr_representation(self):
62+
"""Test __repr__ method."""
63+
self.assertEqual(
64+
repr(self.athlete),
65+
'Athlete(first_name=Mario, last_name=Rossi, date_of_birth=1990-01-01, fiscal_code=RSSMRA90A01H211B)'
66+
)
67+
68+
def test__athlete_fiscal_code_uniqueness(self):
69+
"""Test that the fiscal code must be unique."""
70+
pass
71+
72+
def test__athlete_required_fields(self):
73+
"""Test that required fields cannot be left empty."""
74+
pass

profiles/tests/test_trainers.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)