From de9a4bc4bef050933a78c7407532972dc5e21660 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 27 Dec 2022 08:00:28 +0100 Subject: [PATCH] Fix: default field generation --- backend/model/student.py | 15 ++++++++++----- tests/model/test_student.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/backend/model/student.py b/backend/model/student.py index 29a8315..b66e5e3 100644 --- a/backend/model/student.py +++ b/backend/model/student.py @@ -1,10 +1,9 @@ from __future__ import annotations -import uuid -from dataclasses import dataclass +from dataclasses import dataclass, field +from datetime import datetime from typing import TYPE_CHECKING, Optional - -from pydantic import Field +from uuid import UUID, uuid4 if TYPE_CHECKING: from backend.model.tribe import Tribe @@ -14,7 +13,7 @@ if TYPE_CHECKING: class Student: name: str tribe: Tribe - id: Optional[str] = Field(default_factory=lambda: str(uuid.uuid1())) + id: str = field(default_factory=lambda: str(uuid4())) def __post_init__(self) -> None: self.tribe.register_student(self) @@ -26,3 +25,9 @@ class Student: def __hash__(self) -> int: return hash(self.id) + + def to_tuple(self) -> tuple: + return (self.id, self.name, self.tribe.name) + + def to_dict(self) -> dict: + return {"id": self.id, "name": self.name, "tribe_name": self.tribe.name} diff --git a/tests/model/test_student.py b/tests/model/test_student.py index b47e948..1358d51 100644 --- a/tests/model/test_student.py +++ b/tests/model/test_student.py @@ -1,10 +1,24 @@ 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 +def test_init_student(): + tribe = build_tribes(1)[0] + student = Student(name="Bob", tribe=tribe) + print(student) + + assert type(student.name) == str + assert type(student.tribe) == Tribe + assert type(student.id) == str + + 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)