From 85065b2a1311f30a18d7da455de3bb993d2df3d5 Mon Sep 17 00:00:00 2001 From: Mattia Matteini Date: Wed, 24 Apr 2024 18:11:09 +0200 Subject: [PATCH] fix: fix circular imports --- application/__init__.py | 2 +- ...QuestionService.py => question_service.py} | 5 +- domain/graph/core/__init__.py | 12 ++-- domain/graph/core/{Answer.py => answer.py} | 2 +- .../graph/core/{AnswerId.py => answer_id.py} | 0 domain/graph/core/enum/__init__.py | 4 +- .../graph/core/enum/{Action.py => action.py} | 0 .../{QuestionType.py => question_type.py} | 0 domain/graph/core/{Project.py => project.py} | 2 +- .../core/{ProjectId.py => project_id.py} | 0 .../graph/core/{Question.py => question.py} | 6 +- .../core/{QuestionId.py => question_id.py} | 0 domain/graph/factories/__init__.py | 6 +- .../{AnswerFactory.py => answer_factory.py} | 3 +- .../{ProjectFactory.py => project_factory.py} | 3 +- ...QuestionFactory.py => question_factory.py} | 34 ++++++----- domain/graph/repositories/__init__.py | 2 +- ...onRepository.py => question_repository.py} | 4 +- domain/project/core/__init__.py | 2 +- ...ableQuestion.py => selectable_question.py} | 58 +++++++------------ domain/project/core/selection/__init__.py | 10 +--- ...tegy.py => multiple_selection_strategy.py} | 4 +- ...ctionStrategy.py => selection_strategy.py} | 2 +- ...rategy.py => single_selection_strategy.py} | 0 domain/project/factories/__init__.py | 2 +- ...tory.py => selectable_question_factory.py} | 17 +++--- storage/repositories/__init__.py | 2 +- ...sitory.py => graph_question_repository.py} | 0 .../domain/test_TestSelectableQuestion.py | 2 +- 29 files changed, 88 insertions(+), 96 deletions(-) rename application/{QuestionService.py => question_service.py} (69%) rename domain/graph/core/{Answer.py => answer.py} (85%) rename domain/graph/core/{AnswerId.py => answer_id.py} (100%) rename domain/graph/core/enum/{Action.py => action.py} (100%) rename domain/graph/core/enum/{QuestionType.py => question_type.py} (100%) rename domain/graph/core/{Project.py => project.py} (82%) rename domain/graph/core/{ProjectId.py => project_id.py} (100%) rename domain/graph/core/{Question.py => question.py} (83%) rename domain/graph/core/{QuestionId.py => question_id.py} (100%) rename domain/graph/factories/{AnswerFactory.py => answer_factory.py} (77%) rename domain/graph/factories/{ProjectFactory.py => project_factory.py} (65%) rename domain/graph/factories/{QuestionFactory.py => question_factory.py} (56%) rename domain/graph/repositories/{QuestionRepository.py => question_repository.py} (82%) rename domain/project/core/{SelectableQuestion.py => selectable_question.py} (55%) rename domain/project/core/selection/{MultipleSelectionStrategy.py => multiple_selection_strategy.py} (91%) rename domain/project/core/selection/{SelectionStrategy.py => selection_strategy.py} (91%) rename domain/project/core/selection/{SingleSelectionStrategy.py => single_selection_strategy.py} (100%) rename domain/project/factories/{SelectableQuestionFactory.py => selectable_question_factory.py} (80%) rename storage/repositories/{GraphQuestionRepository.py => graph_question_repository.py} (100%) diff --git a/application/__init__.py b/application/__init__.py index 6bb543d..4162aa4 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -1 +1 @@ -from application.QuestionService import QuestionService +from application.question_service import QuestionService diff --git a/application/QuestionService.py b/application/question_service.py similarity index 69% rename from application/QuestionService.py rename to application/question_service.py index cc5ac08..b8d9491 100644 --- a/application/QuestionService.py +++ b/application/question_service.py @@ -1,5 +1,6 @@ -from domain.graph.core import ProjectId, Question -from domain.graph.repositories import QuestionRepository +from domain.graph.core.project_id import ProjectId +from domain.graph.core.question import Question +from domain.graph.repositories.question_repository import QuestionRepository class QuestionService: diff --git a/domain/graph/core/__init__.py b/domain/graph/core/__init__.py index 241ec61..3da63b1 100644 --- a/domain/graph/core/__init__.py +++ b/domain/graph/core/__init__.py @@ -1,6 +1,6 @@ -from domain.graph.core.AnswerId import AnswerId -from domain.graph.core.Answer import Answer -from domain.graph.core.QuestionId import QuestionId -from domain.graph.core.Question import Question -from domain.graph.core.ProjectId import ProjectId -from domain.graph.core.Project import Project +from domain.graph.core.answer_id import AnswerId +from domain.graph.core.answer import Answer +from domain.graph.core.question_id import QuestionId +from domain.graph.core.question import Question +from domain.graph.core.project_id import ProjectId +from domain.graph.core.project import Project diff --git a/domain/graph/core/Answer.py b/domain/graph/core/answer.py similarity index 85% rename from domain/graph/core/Answer.py rename to domain/graph/core/answer.py index 0b5ad90..a5fc31c 100644 --- a/domain/graph/core/Answer.py +++ b/domain/graph/core/answer.py @@ -1,6 +1,6 @@ from pydantic import BaseModel -from domain.graph.core import AnswerId +from domain.graph.core.answer_id import AnswerId class Answer(BaseModel): diff --git a/domain/graph/core/AnswerId.py b/domain/graph/core/answer_id.py similarity index 100% rename from domain/graph/core/AnswerId.py rename to domain/graph/core/answer_id.py diff --git a/domain/graph/core/enum/__init__.py b/domain/graph/core/enum/__init__.py index 33fd8e9..c89ab6b 100644 --- a/domain/graph/core/enum/__init__.py +++ b/domain/graph/core/enum/__init__.py @@ -1,2 +1,2 @@ -from domain.graph.core.enum.Action import Action -from domain.graph.core.enum.QuestionType import QuestionType +from domain.graph.core.enum.action import Action +from domain.graph.core.enum.question_type import QuestionType diff --git a/domain/graph/core/enum/Action.py b/domain/graph/core/enum/action.py similarity index 100% rename from domain/graph/core/enum/Action.py rename to domain/graph/core/enum/action.py diff --git a/domain/graph/core/enum/QuestionType.py b/domain/graph/core/enum/question_type.py similarity index 100% rename from domain/graph/core/enum/QuestionType.py rename to domain/graph/core/enum/question_type.py diff --git a/domain/graph/core/Project.py b/domain/graph/core/project.py similarity index 82% rename from domain/graph/core/Project.py rename to domain/graph/core/project.py index fef8cc0..389abd5 100644 --- a/domain/graph/core/Project.py +++ b/domain/graph/core/project.py @@ -1,6 +1,6 @@ from pydantic import BaseModel -from domain.graph.core import ProjectId +from domain.graph.core.project_id import ProjectId class Project(BaseModel): diff --git a/domain/graph/core/ProjectId.py b/domain/graph/core/project_id.py similarity index 100% rename from domain/graph/core/ProjectId.py rename to domain/graph/core/project_id.py diff --git a/domain/graph/core/Question.py b/domain/graph/core/question.py similarity index 83% rename from domain/graph/core/Question.py rename to domain/graph/core/question.py index 67c89a4..a00b1df 100644 --- a/domain/graph/core/Question.py +++ b/domain/graph/core/question.py @@ -2,8 +2,10 @@ from pydantic import BaseModel, field_serializer -from domain.graph.core import Answer, QuestionId -from domain.graph.core.enum import Action, QuestionType +from domain.graph.core.answer import Answer +from domain.graph.core.question_id import QuestionId +from domain.graph.core.enum.action import Action +from domain.graph.core.enum.question_type import QuestionType class Question(BaseModel): diff --git a/domain/graph/core/QuestionId.py b/domain/graph/core/question_id.py similarity index 100% rename from domain/graph/core/QuestionId.py rename to domain/graph/core/question_id.py diff --git a/domain/graph/factories/__init__.py b/domain/graph/factories/__init__.py index 8649784..c37829e 100644 --- a/domain/graph/factories/__init__.py +++ b/domain/graph/factories/__init__.py @@ -1,3 +1,3 @@ -from domain.graph.factories.AnswerFactory import AnswerFactory -from domain.graph.factories.ProjectFactory import ProjectFactory -from domain.graph.factories.QuestionFactory import QuestionFactory +from domain.graph.factories.answer_factory import AnswerFactory +from domain.graph.factories.project_factory import ProjectFactory +from domain.graph.factories.question_factory import QuestionFactory diff --git a/domain/graph/factories/AnswerFactory.py b/domain/graph/factories/answer_factory.py similarity index 77% rename from domain/graph/factories/AnswerFactory.py rename to domain/graph/factories/answer_factory.py index 203bd9b..77f5304 100644 --- a/domain/graph/factories/AnswerFactory.py +++ b/domain/graph/factories/answer_factory.py @@ -1,4 +1,5 @@ -from domain.graph.core import Answer, AnswerId +from domain.graph.core.answer import Answer +from domain.graph.core.answer_id import AnswerId class AnswerFactory: diff --git a/domain/graph/factories/ProjectFactory.py b/domain/graph/factories/project_factory.py similarity index 65% rename from domain/graph/factories/ProjectFactory.py rename to domain/graph/factories/project_factory.py index 9d5fc09..d8e7e27 100644 --- a/domain/graph/factories/ProjectFactory.py +++ b/domain/graph/factories/project_factory.py @@ -1,4 +1,5 @@ -from domain.graph.core import Project, ProjectId +from domain.graph.core.project import Project +from domain.graph.core.project_id import ProjectId class ProjectFactory: diff --git a/domain/graph/factories/QuestionFactory.py b/domain/graph/factories/question_factory.py similarity index 56% rename from domain/graph/factories/QuestionFactory.py rename to domain/graph/factories/question_factory.py index b89982a..87d742d 100644 --- a/domain/graph/factories/QuestionFactory.py +++ b/domain/graph/factories/question_factory.py @@ -1,8 +1,12 @@ from typing import FrozenSet, Optional -from domain.graph.core import Answer, Question, QuestionId, AnswerId -from domain.graph.core.enum import Action, QuestionType -from domain.graph.factories.AnswerFactory import AnswerFactory +from domain.graph.core.answer import Answer +from domain.graph.core.answer_id import AnswerId +from domain.graph.core.enum.action import Action +from domain.graph.core.enum.question_type import QuestionType +from domain.graph.core.question import Question +from domain.graph.core.question_id import QuestionId +from domain.graph.factories.answer_factory import AnswerFactory class QuestionFactory: @@ -11,13 +15,13 @@ def __init__(self): self._answer_factory = AnswerFactory() def create_question( - self, - question_id: QuestionId, - text: str, - question_type: QuestionType, - available_answers: FrozenSet[Answer], - previous_question_id: Optional[QuestionId] = None, - action_needed: Optional[Action] = None, + self, + question_id: QuestionId, + text: str, + question_type: QuestionType, + available_answers: FrozenSet[Answer], + previous_question_id: Optional[QuestionId] = None, + action_needed: Optional[Action] = None, ) -> Question: return Question( id=question_id, @@ -29,11 +33,11 @@ def create_question( ) def create_boolean_question( - self, - question_id: QuestionId, - text: str, - previous_question_id: Optional[QuestionId] = None, - action_needed: Optional[Action] = None, + self, + question_id: QuestionId, + text: str, + previous_question_id: Optional[QuestionId] = None, + action_needed: Optional[Action] = None, ) -> Question: available_answers: FrozenSet[Answer] = frozenset( { diff --git a/domain/graph/repositories/__init__.py b/domain/graph/repositories/__init__.py index 44f8f6b..2f1f62d 100644 --- a/domain/graph/repositories/__init__.py +++ b/domain/graph/repositories/__init__.py @@ -1 +1 @@ -from domain.graph.repositories.QuestionRepository import QuestionRepository +from domain.graph.repositories.question_repository import QuestionRepository diff --git a/domain/graph/repositories/QuestionRepository.py b/domain/graph/repositories/question_repository.py similarity index 82% rename from domain/graph/repositories/QuestionRepository.py rename to domain/graph/repositories/question_repository.py index 47aa031..e3e443a 100644 --- a/domain/graph/repositories/QuestionRepository.py +++ b/domain/graph/repositories/question_repository.py @@ -1,7 +1,9 @@ from abc import ABC, abstractmethod from typing import List -from domain.graph.core import ProjectId, QuestionId, Question +from domain.graph.core.project_id import ProjectId +from domain.graph.core.question import Question +from domain.graph.core.question_id import QuestionId class QuestionRepository(ABC): diff --git a/domain/project/core/__init__.py b/domain/project/core/__init__.py index 7cc45ad..ded4776 100644 --- a/domain/project/core/__init__.py +++ b/domain/project/core/__init__.py @@ -1 +1 @@ -from domain.project.core.SelectableQuestion import SelectableQuestion +from domain.project.core.selectable_question import SelectableQuestion diff --git a/domain/project/core/SelectableQuestion.py b/domain/project/core/selectable_question.py similarity index 55% rename from domain/project/core/SelectableQuestion.py rename to domain/project/core/selectable_question.py index b32e993..e3fb194 100644 --- a/domain/project/core/SelectableQuestion.py +++ b/domain/project/core/selectable_question.py @@ -3,9 +3,9 @@ from pydantic import field_serializer from typing_extensions import Self -from domain.graph.core import Answer, Question, QuestionId -from domain.project.core.selection import SelectionStrategy, SingleSelectionStrategy -from domain.project.factories import SelectableQuestionFactory +from domain.graph.core.answer import Answer +from domain.graph.core.question import Question +from domain.project.core.selection.selection_strategy import SelectionStrategy class SelectableQuestion(Question): @@ -21,28 +21,30 @@ def select_answer(self, answer: Answer) -> Self: selected_answers = self.selection_strategy.select_answer( answer, self.selected_answers ) - return SelectableQuestionFactory().create_question( - self.id, - self.text, - self.type, - self.available_answers, - self.previous_question_id, - self.action_needed, - selected_answers, + return SelectableQuestion( + id=self.id, + text=self.text, + type=self.type, + available_answers=self.available_answers, + previous_question_id=self.previous_question_id, + action_needed=self.action_needed, + selection_strategy=self.selection_strategy, + selected_answers=selected_answers, ) def deselect_answer(self, answer: Answer) -> Self: selected_answers = self.selection_strategy.deselect_answer( answer, self.selected_answers ) - return SelectableQuestionFactory().create_question( - self.id, - self.text, - self.type, - self.available_answers, - self.previous_question_id, - self.action_needed, - selected_answers, + return SelectableQuestion( + id=self.id, + text=self.text, + type=self.type, + available_answers=self.available_answers, + previous_question_id=self.previous_question_id, + action_needed=self.action_needed, + selection_strategy=self.selection_strategy, + selected_answers=selected_answers, ) @field_serializer("selected_answers", when_used="json") @@ -65,21 +67,3 @@ def __hash__(self): self.action_needed, ) ) - - -if __name__ == "__main__": - print( - SelectableQuestion( - id=QuestionId(code="question_id"), - text="Do you practice TDD?", - type=QuestionType.SINGLE_CHOICE, - available_answers=frozenset( - { - Answer(text="Always", value="always"), - Answer(text="Never", value="never"), - } - ), - action_needed=None, - selection_strategy=SingleSelectionStrategy(), - ) - ) diff --git a/domain/project/core/selection/__init__.py b/domain/project/core/selection/__init__.py index 5fab9e7..6d20cba 100644 --- a/domain/project/core/selection/__init__.py +++ b/domain/project/core/selection/__init__.py @@ -1,7 +1,3 @@ -from domain.project.core.selection.SelectionStrategy import SelectionStrategy -from domain.project.core.selection.MultipleSelectionStrategy import ( - MultipleSelectionStrategy, -) -from domain.project.core.selection.SingleSelectionStrategy import ( - SingleSelectionStrategy, -) +from domain.project.core.selection.selection_strategy import SelectionStrategy +from domain.project.core.selection.multiple_selection_strategy import MultipleSelectionStrategy +from domain.project.core.selection.single_selection_strategy import SingleSelectionStrategy diff --git a/domain/project/core/selection/MultipleSelectionStrategy.py b/domain/project/core/selection/multiple_selection_strategy.py similarity index 91% rename from domain/project/core/selection/MultipleSelectionStrategy.py rename to domain/project/core/selection/multiple_selection_strategy.py index cdf2879..35f32a6 100644 --- a/domain/project/core/selection/MultipleSelectionStrategy.py +++ b/domain/project/core/selection/multiple_selection_strategy.py @@ -1,7 +1,7 @@ from typing_extensions import FrozenSet -from domain.graph.core import Answer -from domain.project.core.selection import SelectionStrategy +from domain.graph.core.answer import Answer +from domain.project.core.selection.selection_strategy import SelectionStrategy class MultipleSelectionStrategy(SelectionStrategy): diff --git a/domain/project/core/selection/SelectionStrategy.py b/domain/project/core/selection/selection_strategy.py similarity index 91% rename from domain/project/core/selection/SelectionStrategy.py rename to domain/project/core/selection/selection_strategy.py index cf31c2f..52ac8d6 100644 --- a/domain/project/core/selection/SelectionStrategy.py +++ b/domain/project/core/selection/selection_strategy.py @@ -3,7 +3,7 @@ from pydantic import BaseModel from typing_extensions import FrozenSet -from domain.graph.core import Answer +from domain.graph.core.answer import Answer class SelectionStrategy(ABC, BaseModel): diff --git a/domain/project/core/selection/SingleSelectionStrategy.py b/domain/project/core/selection/single_selection_strategy.py similarity index 100% rename from domain/project/core/selection/SingleSelectionStrategy.py rename to domain/project/core/selection/single_selection_strategy.py diff --git a/domain/project/factories/__init__.py b/domain/project/factories/__init__.py index df06001..ae1bf61 100644 --- a/domain/project/factories/__init__.py +++ b/domain/project/factories/__init__.py @@ -1 +1 @@ -from domain.project.factories.SelectableQuestionFactory import SelectableQuestionFactory +from domain.project.factories.selectable_question_factory import SelectableQuestionFactory diff --git a/domain/project/factories/SelectableQuestionFactory.py b/domain/project/factories/selectable_question_factory.py similarity index 80% rename from domain/project/factories/SelectableQuestionFactory.py rename to domain/project/factories/selectable_question_factory.py index 8380bfd..5d79187 100644 --- a/domain/project/factories/SelectableQuestionFactory.py +++ b/domain/project/factories/selectable_question_factory.py @@ -1,13 +1,14 @@ from typing import FrozenSet, Optional -from domain.graph.core import Answer, QuestionId, AnswerId -from domain.graph.core.enum import Action, QuestionType -from domain.graph.factories import AnswerFactory -from domain.project.core import SelectableQuestion -from domain.project.core.selection import ( - SingleSelectionStrategy, - MultipleSelectionStrategy, -) +from domain.graph.core.answer import Answer +from domain.graph.core.answer_id import AnswerId +from domain.graph.core.enum.action import Action +from domain.graph.core.enum.question_type import QuestionType +from domain.graph.core.question_id import QuestionId +from domain.graph.factories.answer_factory import AnswerFactory +from domain.project.core.selectable_question import SelectableQuestion +from domain.project.core.selection.multiple_selection_strategy import MultipleSelectionStrategy +from domain.project.core.selection.single_selection_strategy import SingleSelectionStrategy class SelectableQuestionFactory: diff --git a/storage/repositories/__init__.py b/storage/repositories/__init__.py index 82d7f95..880b8fe 100644 --- a/storage/repositories/__init__.py +++ b/storage/repositories/__init__.py @@ -1 +1 @@ -from storage.repositories.GraphQuestionRepository import GraphQuestionRepository +from storage.repositories.graph_question_repository import GraphQuestionRepository diff --git a/storage/repositories/GraphQuestionRepository.py b/storage/repositories/graph_question_repository.py similarity index 100% rename from storage/repositories/GraphQuestionRepository.py rename to storage/repositories/graph_question_repository.py diff --git a/test/unit/domain/test_TestSelectableQuestion.py b/test/unit/domain/test_TestSelectableQuestion.py index 912d527..e47eaf4 100644 --- a/test/unit/domain/test_TestSelectableQuestion.py +++ b/test/unit/domain/test_TestSelectableQuestion.py @@ -2,7 +2,7 @@ from domain.graph.core import Answer, AnswerId, QuestionId from domain.graph.core.enum import QuestionType -from domain.graph.factories.AnswerFactory import AnswerFactory +from domain.graph.factories.answer_factory import AnswerFactory from domain.project.core import SelectableQuestion from domain.project.factories import SelectableQuestionFactory