recopytex/tests/model/fakes.py

102 lines
2.7 KiB
Python

from random import choice, randint
from faker import Faker
from backend.model.assessment import Assessment, Domain, Exercise, Question, Skill
from backend.model.tribe import Tribe
from backend.repository.abstract_repository import AbstractRepository
faker = Faker()
def build_tribes(quantity: int = 1):
return [Tribe(name=faker.words(5), level=faker.words(5)) for _ in range(quantity)]
def build_assessments(tribes: list[Tribe], assessment_per_tribe: int = 1):
assessments = []
for t in tribes:
assessments += [
Assessment("faker.words(5)", t, randint(1, 3))
for _ in range(assessment_per_tribe)
]
return assessments
def build_exercises(assessments: list[Assessment], exercise_per_assessment=1):
exercises = []
for assessment in assessments:
exercises += [
Exercise("faker.words(4)", assessment, "today")
for _ in range(exercise_per_assessment)
]
return exercises
def build_skills(quantity=1):
return [Skill(faker.words(6), faker.text()) for _ in range(quantity)]
def build_domains(quantity=1):
return [Domain(faker.words(6), faker.text()) for _ in range(quantity)]
def build_questions(
exercises: list[Exercise],
question_per_exercise=1,
):
skills = build_skills()
domains = build_domains()
questions = []
for exercise in exercises:
questions += [
Question(
faker.words(2),
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
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")