From 4ab9d0e1760b4d4d1213cfd67dbd616d14f0d855 Mon Sep 17 00:00:00 2001 From: Florian Schepers Date: Thu, 11 Apr 2024 15:41:54 +0200 Subject: [PATCH] WIP: dispatches new issues --- .../issue_dispatcher/issue_dispatcher.py | 32 +++++++++++++++++-- .../test/test_issue_dispatching.py | 29 ++++++++++++++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/examples/issue_classification_user_journey/issue_dispatcher/issue_dispatcher.py b/src/examples/issue_classification_user_journey/issue_dispatcher/issue_dispatcher.py index 74353750f..fa03a5c35 100644 --- a/src/examples/issue_classification_user_journey/issue_dispatcher/issue_dispatcher.py +++ b/src/examples/issue_classification_user_journey/issue_dispatcher/issue_dispatcher.py @@ -1,10 +1,36 @@ from abc import ABC +class Issue(): + def __init__(self, id: str, title: str): + self.id = id + self.title = title + class IssuePort(ABC): - pass + def get_next(self) -> Issue: + pass + def dispatch_to(self, recipient: str, issue_id: str) -> None: + pass + -class IssueDispatcher(): - def __init__(self, issue_port: IssuePort): + +class ClassifierPort(ABC): + def classify(self, issue: Issue)->str: pass +class IssueDispatcher(): + def __init__(self, issues: IssuePort, classifier: ClassifierPort): + self.issues = issues + self.classifier = classifier + + def dispatch_next_issue(self): + issue = self.issues.get_next() + recipient = self.classifier.classify(issue) + self.issues.dispatch_to(recipient, issue.id) + + def run(self): + while True: + self.dispatch_next_issue() + + + diff --git a/src/examples/issue_classification_user_journey/test/test_issue_dispatching.py b/src/examples/issue_classification_user_journey/test/test_issue_dispatching.py index e8755b4ee..d763ad6b3 100644 --- a/src/examples/issue_classification_user_journey/test/test_issue_dispatching.py +++ b/src/examples/issue_classification_user_journey/test/test_issue_dispatching.py @@ -1,18 +1,37 @@ from typing import Sequence -from examples.issue_classification_user_journey.issue_dispatcher.issue_dispatcher import IssueDispatcher, IssuePort + +from examples.issue_classification_user_journey.issue_dispatcher.issue_dispatcher import ClassifierPort, Issue, IssueDispatcher, IssuePort + class IssueAdapterFake(IssuePort): - def __init__(self, issues: Sequence[str]): - self.issues = issues + def __init__(self, issues: Sequence[tuple[str, str]]): + self.issues = [Issue(id=issue[1], title=issue[0]) for issue in issues] + self.dispatched_issues = {} + + def get_next(self) -> Issue: + return self.issues[0] + + def dispatch_to(self, recipient: str, issue_id: str) -> None: + self.dispatched_issues[issue_id] = recipient +class AlwaysSameClass(ClassifierPort): + def __init__(self, label: str): + self.label = label + + def classify(self, _issue: Issue) -> str: + return self.label def test_should_dispatch_new_issue() -> None: # Given new issues - issue_stub = IssueAdapterFake(["My Computer does not work."]) + issue_stub = IssueAdapterFake([("My Computer does not work.", "1")]) + classifier_stub = AlwaysSameClass(label="IT") # When - issue_dispatcher = IssueDispatcher(issue_stub) + issue_dispatcher = IssueDispatcher(issue_stub, classifier_stub) issue_dispatcher.dispatch_next_issue() + # Then + assert issue_stub.dispatched_issues["1"] == "IT" +