Fix: default field generation

This commit is contained in:
Bertrand Benjamin 2022-12-27 08:00:28 +01:00
parent 2dc1cf6fb8
commit de9a4bc4be
2 changed files with 24 additions and 5 deletions

View File

@ -1,10 +1,9 @@
from __future__ import annotations from __future__ import annotations
import uuid from dataclasses import dataclass, field
from dataclasses import dataclass from datetime import datetime
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from uuid import UUID, uuid4
from pydantic import Field
if TYPE_CHECKING: if TYPE_CHECKING:
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
@ -14,7 +13,7 @@ if TYPE_CHECKING:
class Student: class Student:
name: str name: str
tribe: Tribe 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: def __post_init__(self) -> None:
self.tribe.register_student(self) self.tribe.register_student(self)
@ -26,3 +25,9 @@ class Student:
def __hash__(self) -> int: def __hash__(self) -> int:
return hash(self.id) 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}

View File

@ -1,10 +1,24 @@
from random import randint from random import randint
from uuid import UUID
from backend.model.student import Student from backend.model.student import Student
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
from tests.model.fakes import build_assessments, build_tribes 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(): def test_tribe_register_assessment():
assessments_qty = randint(1, 10) assessments_qty = randint(1, 10)
tribes = build_tribes(1) tribes = build_tribes(1)