Skip to content

Commit

Permalink
refactor to handle multipel issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSchepersAA committed Apr 12, 2024
1 parent 4ab9d0e commit 9df0f08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,35 @@ def __init__(self, id: str, title: str):
self.title = title

class IssuePort(ABC):
def get_next(self) -> Issue:
def new_issues(self) -> list[Issue]:
pass
def dispatch_to(self, recipient: str, issue_id: str) -> None:
pass

def wait_for_notification(self):
pass

class ClassifierPort(ABC):
def classify(self, issue: Issue)->str:
pass


class IssueDispatcher():
def __init__(self, issues: IssuePort, classifier: ClassifierPort):
self.issues = issues
def __init__(self, issue_port: IssuePort, classifier: ClassifierPort):
self.issue_port = issue_port
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 dispatch_issues(self):
issues = self.issue_port.new_issues()
for issue in issues:
recipient = self.classifier.classify(issue)
self.issue_port.dispatch_to(recipient, issue.id)

def run(self):
while True:
self.dispatch_next_issue()
self.issue_port.wait_for_notification()
self.dispatch_issues()





Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

from time import sleep
from typing import Sequence

from examples.issue_classification_user_journey.issue_dispatcher.issue_dispatcher import ClassifierPort, Issue, IssueDispatcher, IssuePort
Expand All @@ -9,26 +10,29 @@ 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 new_issues(self) -> list[Issue]:
return self.issues

def dispatch_to(self, recipient: str, issue_id: str) -> None:
self.dispatched_issues[issue_id] = recipient

def wait_for_notification(self):
sleep(1)

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.", "1")])
classifier_stub = AlwaysSameClass(label="IT")
# When
issue_dispatcher = IssueDispatcher(issue_stub, classifier_stub)
issue_dispatcher.dispatch_next_issue()
issue_dispatcher.dispatch_issues()
# Then
assert issue_stub.dispatched_issues["1"] == "IT"

Expand Down

0 comments on commit 9df0f08

Please sign in to comment.