recopytex/tests/model/fakes.py

126 lines
3.3 KiB
Python
Raw Normal View History

from random import choice, randint
from faker import Faker
from faker.providers import DynamicProvider
from backend.model.assessment import Assessment, Domain, Exercise, Question, Skill
from backend.model.student import Student
from backend.model.tribe import Tribe
2022-12-26 06:03:52 +00:00
from backend.repository.abstract_repository import AbstractRepository
level_provider = DynamicProvider(
provider_name="level",
elements=["2nd", "1ST", "SNT", "1G", "TG", "EnsSci"],
)
faker = Faker("fr_FR")
faker.add_provider(level_provider)
def build_tribes(quantity: int = 1) -> list[Tribe]:
return [Tribe(name=faker.word(), level=faker.level()) for _ in range(quantity)]
def build_assessments(
tribes: list[Tribe], assessment_per_tribe: int = 1
) -> list[Assessment]:
assessments = []
for t in tribes:
assessments += [
Assessment("faker.word()", t, randint(1, 3))
for _ in range(assessment_per_tribe)
]
return assessments
def build_exercises(
assessments: list[Assessment], exercise_per_assessment=1
) -> list[Exercise]:
exercises = []
for assessment in assessments:
exercises += [
Exercise("faker.word()", assessment, "today")
for _ in range(exercise_per_assessment)
]
return exercises
def build_skills(quantity=1) -> list[Skill]:
return [Skill(faker.word(), faker.text()) for _ in range(quantity)]
def build_domains(quantity=1) -> list[Domain]:
return [Domain(faker.word(), faker.text()) for _ in range(quantity)]
def build_questions(
exercises: list[Exercise],
question_per_exercise=1,
) -> list[Question]:
skills = build_skills()
domains = build_domains()
questions = []
for exercise in exercises:
questions += [
Question(
faker.word(),
exercise,
description="desc",
skill=choice(skills),
domain=choice(domains),
is_leveled=choice([True, False]),
scale=randint(1, 20),
)
for _ in range(question_per_exercise)
]
return questions
2022-12-26 06:03:52 +00:00
def build_student(
tribes: list[Tribe],
students_per_tribe=1,
) -> list[Student]:
students = []
for tribe in tribes:
students += [
Student(name=faker.name(), tribe=tribe) for _ in range(students_per_tribe)
]
return students
2022-12-26 06:03:52 +00:00
class FakeTribeRepository(AbstractRepository):
def __init__(self, tribes):
self._tribes = {t.name: t for t in tribes}
def add(self, tribe):
if tribe.name not in self._tribes.keys():
self._tribes[tribe.name] = tribe
raise KeyError(f"{tribe} already exists")
def update(self, name, tribe):
try:
self._tribes.pop(name)
self._tribes[tribe.name] = tribe
except KeyError:
raise KeyError(f"The tribe {tribe} does not exists")
def list(self):
return list(self._tribes.values())
def get(self, name):
try:
return self._tribes[name]
except KeyError:
raise KeyError(f"The tribe {tribe} does not exists")
def delete(self, name):
try:
self._tribes.pop(name)
except KeyError:
raise KeyError(f"The tribe {tribe} does not exists")