From 9ef6f3fa322c1a8af376b3588a5102fd25d9b2fa Mon Sep 17 00:00:00 2001 From: Mattia Matteini Date: Mon, 18 Mar 2024 17:59:22 +0100 Subject: [PATCH] feat(domain): add question entities --- app/__init__.py | 0 app/domain/core/Question.py | 25 +++++++++++++ app/domain/core/QuestionId.py | 14 ++++++++ app/domain/core/enum/Action.py | 5 +++ app/domain/core/enum/QuestionType.py | 8 +++++ app/domain/core/enum/__init__.py | 0 app/domain/factories/QuestionFactory.py | 48 +++++++++++++++++++++++++ test/unit/domain/test_TestQuestion.py | 10 +++--- 8 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/domain/core/Question.py create mode 100644 app/domain/core/QuestionId.py create mode 100644 app/domain/core/enum/Action.py create mode 100644 app/domain/core/enum/QuestionType.py create mode 100644 app/domain/core/enum/__init__.py create mode 100644 app/domain/factories/QuestionFactory.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/domain/core/Question.py b/app/domain/core/Question.py new file mode 100644 index 0000000..d4c297c --- /dev/null +++ b/app/domain/core/Question.py @@ -0,0 +1,25 @@ +from typing import List + +from app.domain.core.Answer import Answer +from app.domain.core.QuestionId import QuestionId +from app.domain.core.enum.Action import Action +from app.domain.core.enum.QuestionType import QuestionType + + +class Question: + + def __init__( + self, + question_id: QuestionId, + text: str, + question_type: QuestionType, + available_answers: List[Answer], + selected_answer: List[Answer] = [], + action_needed: Action = None, + ): + self._id = question_id + self._text = text + self._type = question_type + self._availableAnswers = available_answers + self._selectedAnswer = selected_answer + self._actionNeeded = action_needed diff --git a/app/domain/core/QuestionId.py b/app/domain/core/QuestionId.py new file mode 100644 index 0000000..05231a5 --- /dev/null +++ b/app/domain/core/QuestionId.py @@ -0,0 +1,14 @@ +class QuestionId: + + def __init__(self, code: str): + self._code = code + + @property + def code(self): + return self._code + + def __eq__(self, other): + return self.code == other.code + + def __str__(self): + return f"QuestionId(code={self.code})" diff --git a/app/domain/core/enum/Action.py b/app/domain/core/enum/Action.py new file mode 100644 index 0000000..18b76cd --- /dev/null +++ b/app/domain/core/enum/Action.py @@ -0,0 +1,5 @@ +from enum import Enum + + +class Action(Enum): + METRICS_CHECK = 0 diff --git a/app/domain/core/enum/QuestionType.py b/app/domain/core/enum/QuestionType.py new file mode 100644 index 0000000..75ef8d6 --- /dev/null +++ b/app/domain/core/enum/QuestionType.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class QuestionType(Enum): + BOOLEAN = 0 + SINGLE_CHOICE = 1 + MULTIPLE_CHOICE = 2 + RATING = 3 diff --git a/app/domain/core/enum/__init__.py b/app/domain/core/enum/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/domain/factories/QuestionFactory.py b/app/domain/factories/QuestionFactory.py new file mode 100644 index 0000000..5bc8259 --- /dev/null +++ b/app/domain/factories/QuestionFactory.py @@ -0,0 +1,48 @@ +from typing import List + +from app.domain.core.Answer import Answer +from app.domain.core.Question import Question +from app.domain.core.QuestionId import QuestionId +from app.domain.core.enum.Action import Action +from app.domain.core.enum.QuestionType import QuestionType +from app.domain.factories.AnswerFactory import AnswerFactory + + +class QuestionFactory: + + def __init__(self): + self._answer_factory = AnswerFactory() + + def create_question( + self, + question_id: QuestionId, + text: str, + question_type: QuestionType, + available_answers: List[Answer], + selected_answer: List[Answer], + action_needed: Action, + ) -> Question: + return Question( + question_id, + text, + question_type, + available_answers, + selected_answer, + action_needed, + ) + + def create_boolean_question( + self, question_id: QuestionId, text: str, action_needed: Action = None + ) -> Question: + available_answers: List[Answer] = [ + self._answer_factory.create_boolean_answer(True), + self._answer_factory.create_boolean_answer(False), + ] + return Question( + question_id, + text, + QuestionType.BOOLEAN, + available_answers, + [], + action_needed, + ) diff --git a/test/unit/domain/test_TestQuestion.py b/test/unit/domain/test_TestQuestion.py index 2e704bb..cfef10d 100644 --- a/test/unit/domain/test_TestQuestion.py +++ b/test/unit/domain/test_TestQuestion.py @@ -1,10 +1,12 @@ import unittest +from app.domain.core.Question import Question +from app.domain.factories.QuestionFactory import QuestionFactory + class TestQuestion(unittest.TestCase): def setUp(self): - self.question = Question("What is your name?") - - - + self.question = QuestionFactory().create_boolean_question( + "1", "Is this a test?" + )