Skip to content

Commit

Permalink
Merge pull request #8 from ouhammmourachid/update-test
Browse files Browse the repository at this point in the history
add save methode to graph class to save the script in a seperte file …
  • Loading branch information
ouhammmourachid authored Oct 25, 2023
2 parents 94addad + 577bded commit 192394d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mermaid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" a beter docs sttrings
"""
from ._utils import read_file
from ._utils import load
from .mermaid import *

__version__: str = '0.1.5'
2 changes: 1 addition & 1 deletion mermaid/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .graph import Graph


def read_file(path: Path) -> Graph:
def load(path: Path) -> Graph:
script: Optional[str] = None
name: Optional[str] = None

Expand Down
9 changes: 9 additions & 0 deletions mermaid/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Optional


@dataclass
class Graph:
name: str
script: str

def save(self, path: Optional[Path] = None) -> None:
file_path: Path = path if path else Path(f'./{self.name}.txt')

with open(file_path, 'w') as file:
file.write(self.script)
5 changes: 0 additions & 5 deletions tests/test-graph.txt

This file was deleted.

31 changes: 31 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import unittest
from pathlib import Path

from mermaid.graph import Graph


class TestGraph(unittest.TestCase):
def setUp(self) -> None:
script: str = """graph TD;
A-->B;
A-->C;
B-->D;
C-->D;"""
self.graph_test: Graph = Graph('test-graph', script)
return super().setUp()

def test_save_graph_without_path(self) -> None:
self.graph_test.save()
self.assertTrue(Path.exists(Path('./test-graph.txt')))

def test_save_graph_with_path(self) -> None:
self.graph_test.save(Path('./test-graph-file.txt'))
self.assertTrue(Path.exists(Path('./test-graph-file.txt')))

def tearDown(self) -> None:
if os.path.exists('./test-graph.txt'):
os.remove('./test-graph.txt')
if os.path.exists('./test-graph-file.txt'):
os.remove('./test-graph-file.txt')
return super().tearDown()
22 changes: 19 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import os
import unittest

import mermaid as md
from mermaid.graph import Graph


class TestUtils(unittest.TestCase):
def setUp(self) -> None:
self.file_test: str = './tests/test-graph.txt'
with open(self.file_test, 'w') as file:
file.write("""graph TD;
A-->B;
A-->C;
B-->D;
C-->D;""")
return super().setUp()

def test_read_file_should_raise_error(self):
with self.assertRaises(FileNotFoundError):
md.read_file('./file.txt')
md.load('./file.txt')

def test_read_file(self):
graph: Graph = md.read_file('./tests/test-graph.txt')
graph: Graph = md.load('./tests/test-graph.txt')
expect_name: str = 'test-graph'
expect_script: str = """graph TD;
A-->B;
A-->C;
B-->D;
C-->D;\n"""
C-->D;"""
self.assertEqual(graph.name, expect_name)
self.assertEqual(graph.script, expect_script)

def tearDown(self) -> None:
if os.path.exists(self.file_test):
os.remove(self.file_test)
return super().tearDown()

0 comments on commit 192394d

Please sign in to comment.