From 566ba8e2f59eca76fe7ec01e6e8e6a5bbaa35e2a Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 26 Dec 2022 11:46:29 +0100 Subject: [PATCH] Feat: autogenerate id for student if none is given --- backend/model/student.py | 7 +++++-- tests/model/test_student.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/model/student.py b/backend/model/student.py index 4404eca..29a8315 100644 --- a/backend/model/student.py +++ b/backend/model/student.py @@ -1,7 +1,10 @@ from __future__ import annotations +import uuid from dataclasses import dataclass -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional + +from pydantic import Field if TYPE_CHECKING: from backend.model.tribe import Tribe @@ -9,9 +12,9 @@ if TYPE_CHECKING: @dataclass class Student: - id: str name: str tribe: Tribe + id: Optional[str] = Field(default_factory=lambda: str(uuid.uuid1())) def __post_init__(self) -> None: self.tribe.register_student(self) diff --git a/tests/model/test_student.py b/tests/model/test_student.py index dad320f..b47e948 100644 --- a/tests/model/test_student.py +++ b/tests/model/test_student.py @@ -16,7 +16,7 @@ def test_tribe_register_assessment(): def test_tribe_register_student(): tribe = Tribe("foo", "2nd") - student = Student("1", "Bob", tribe) + student = Student(id="1", name="Bob", tribe=tribe) assert len(tribe.students) == 1 assert tribe.students[0] == student