Skip to content

Commit

Permalink
Merge pull request #58 from ouhammmourachid/userJourney
Browse files Browse the repository at this point in the history
add  UserJourney diagram to mermaid-py.
  • Loading branch information
ouhammmourachid authored Nov 12, 2023
2 parents 0f7e9e3 + 7c6d08e commit 894a62d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ If you'd like to contribute to this open source project folow this steps:
- [ ] [Class Diagram](https://mermaid.js.org/syntax/classDiagram.html)
- [ ] [State Diagram](https://mermaid.js.org/syntax/stateDiagram.html)
- [x] [Entity Relationship Diagram](https://mermaid.js.org/syntax/entityRelationshipDiagram.html)
- [ ] [User Journey](https://mermaid.js.org/syntax/userJourney.html)
- [x] [User Journey](https://mermaid.js.org/syntax/userJourney.html)
- [ ] [Gantt](https://mermaid.js.org/syntax/gantt.html)
- [x] [Pie Chart](https://mermaid.js.org/syntax/pie.html)
- [ ] [Quadrant Chart](https://mermaid.js.org/syntax/quadrantChart.html)
Expand Down
80 changes: 80 additions & 0 deletions mermaid/tests/test_user_journey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import unittest
from unittest.mock import MagicMock

from mermaid.userjourney import Actor, Section, Task, UserJourney


class TestTask(unittest.TestCase):
def test_str_task_with_one_actor(self):
actors = Actor('Me')
task = Task('Make tea', 5, actors)
expect_string = '\t\tMake tea: 5 : Me'
self.assertEqual(expect_string, str(task))

def test_str_task_with_actors(self):
actors = [Actor('Me'), Actor('Cat')]
task = Task('Make tea', 5, actors)
expect_string = '\t\tMake tea: 5 : Me, Cat'
self.assertEqual(expect_string, str(task))


class TestSection(unittest.TestCase):
def test_string_section(self):
task_mock_1 = MagicMock(spec=Task)
task_mock_2 = MagicMock(spec=Task)
task_mock_1.__str__.return_value = '\t\tMake tea1: 5 : Me, Cat'
task_mock_2.__str__.return_value = '\t\tMake tea2: 5 : Me, Cat'
section = Section('My working day', [task_mock_1, task_mock_2])
expect_string = """\tsection My working day
\t\tMake tea1: 5 : Me, Cat
\t\tMake tea2: 5 : Me, Cat
"""
self.assertEqual(expect_string, str(section))


class TestUserJourney(unittest.TestCase):
def test_script_with_task(self):
task_mock_1 = MagicMock(spec=Task)
task_mock_2 = MagicMock(spec=Task)
task_mock_1.__str__.return_value = '\tMake tea1: 5 : Me, Cat'
task_mock_2.__str__.return_value = '\tMake tea2: 5 : Me, Cat'
userjourney = UserJourney('simple user journey',
[task_mock_1, task_mock_2])
expect_string = """---
title: simple user journey
---
journey
\ttitle simple user journey
\tMake tea1: 5 : Me, Cat
\tMake tea2: 5 : Me, Cat
"""
self.assertEqual(expect_string, userjourney.script)

def test_script_with_section(self):
section_mock_1 = MagicMock(spec=Section)
section_mock_2 = MagicMock(spec=Section)
section_mock_1.__str__.return_value = """\tsection section-1
\t\tMake tea1: 5 : Me, Cat
\t\tMake tea2: 5 : Me, Cat
"""
section_mock_2.__str__.return_value = """\tsection section-2
\t\tMake tea1: 5 : Me, Cat
\t\tMake tea2: 5 : Me, Cat
"""
userjourney = UserJourney('simple user journey',
[section_mock_1, section_mock_2])
expect_string = """---
title: simple user journey
---
journey
\ttitle simple user journey
\tsection section-1
\t\tMake tea1: 5 : Me, Cat
\t\tMake tea2: 5 : Me, Cat
\tsection section-2
\t\tMake tea1: 5 : Me, Cat
\t\tMake tea2: 5 : Me, Cat
"""
self.assertEqual(expect_string, userjourney.script)
23 changes: 23 additions & 0 deletions mermaid/userjourney/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Union

from mermaid.graph import Graph

from .actor import Actor
from .section import Section
from .task import Task


class UserJourney(Graph):
def __init__(self, title: str, sections: list[Union[Section,
Task]]) -> None:
super().__init__(title, '')
self.title: str = title
self.sections: list[Union[Section, Task]] = sections
self._build_script()

def _build_script(self) -> None:
super()._build_script()
script: str = f'\njourney\n\ttitle {self.title}\n'
for section in self.sections:
script += f'{section}\n'
self.script += script
6 changes: 6 additions & 0 deletions mermaid/userjourney/actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dataclasses import dataclass


@dataclass
class Actor:
name: str
13 changes: 13 additions & 0 deletions mermaid/userjourney/section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .task import Task


class Section:
def __init__(self, name: str, tasks: list[Task]) -> None:
self.name: str = name
self.tasks: list[Task] = tasks

def __str__(self) -> str:
string: str = f'\tsection {self.name}\n'
for task in self.tasks:
string += f'{task}\n'
return string
16 changes: 16 additions & 0 deletions mermaid/userjourney/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Union

from .actor import Actor


class Task:
def __init__(self, name: str, score: int, actors: Union[list[Actor],
Actor]) -> None:
self.name: str = name
self.score: int = score
self.actors: list[Actor] = actors if isinstance(actors,
list) else [actors]

def __str__(self) -> str:
actor_string: str = ', '.join([actor.name for actor in self.actors])
return f'\t\t{self.name}: {self.score} : {actor_string}'

0 comments on commit 894a62d

Please sign in to comment.