diff --git a/backend/model/tribe.py b/backend/model/tribe.py index 5833d1d..b4c5a28 100644 --- a/backend/model/tribe.py +++ b/backend/model/tribe.py @@ -8,6 +8,10 @@ if TYPE_CHECKING: from backend.model.student import Student +class TribeError(Exception): + pass + + @dataclass class Tribe: name: str @@ -18,7 +22,22 @@ class Tribe: def register_assessment(self, assessment: Assessment) -> None: self.assessments.append(assessment) + @property + def students_id(self) -> list[str]: + return [s.id for s in self.students] + def register_student(self, student: Student) -> None: + """Register a student + + If the student is already registered, it is modified. + """ + try: + old_student = next(filter(lambda s: s.id == student.id, self.students)) + except StopIteration: + pass + else: + self.students.remove(old_student) + self.students.append(student) def __eq__(self, other: object) -> bool: diff --git a/tests/model/test_student.py b/tests/model/test_student.py index 1358d51..799e2ab 100644 --- a/tests/model/test_student.py +++ b/tests/model/test_student.py @@ -1,9 +1,6 @@ -from random import randint -from uuid import UUID - from backend.model.student import Student from backend.model.tribe import Tribe -from tests.model.fakes import build_assessments, build_tribes +from tests.model.fakes import build_tribes def test_init_student(): @@ -17,20 +14,3 @@ def test_init_student(): student2 = Student(name="Hop", tribe=tribe) assert student.id != student2.id - - -def test_tribe_register_assessment(): - assessments_qty = randint(1, 10) - tribes = build_tribes(1) - assessments = build_assessments(tribes, assessments_qty) - - assert len(tribes[0].assessments) == assessments_qty - assert tribes[0].assessments == assessments - - -def test_tribe_register_student(): - tribe = Tribe("foo", "2nd") - student = Student(id="1", name="Bob", tribe=tribe) - - assert len(tribe.students) == 1 - assert tribe.students[0] == student diff --git a/tests/model/test_tribe.py b/tests/model/test_tribe.py new file mode 100644 index 0000000..a26b69e --- /dev/null +++ b/tests/model/test_tribe.py @@ -0,0 +1,33 @@ +from random import randint + +import pytest + +from backend.model.student import Student +from backend.model.tribe import Tribe, TribeError +from tests.model.fakes import build_assessments, build_tribes + + +def test_tribe_register_assessment(): + assessments_qty = randint(1, 10) + tribes = build_tribes(1) + assessments = build_assessments(tribes, assessments_qty) + + assert len(tribes[0].assessments) == assessments_qty + assert tribes[0].assessments == assessments + + +def test_tribe_register_student(): + tribe = Tribe("foo", "2nd") + student = Student(id="1", name="Bob", tribe=tribe) + + assert len(tribe.students) == 1 + assert tribe.students[0] == student + + +def test_tribe_register_student_already_exists_overwrite(): + tribe = Tribe("foo", "2nd") + student = Student(id="1", name="Bob", tribe=tribe) + changed_student = Student(id="1", name="Choupy", tribe=tribe) + + assert tribe.students[0] == changed_student + assert student.name not in [s.name for s in tribe.students]