Skip to content

Commit

Permalink
cli: initial mind map construction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Mar 24, 2024
1 parent ec5b2ad commit d3f0cc3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion scripts/py_cli/py_cli/scratch_scripts/mind_map/mind_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,13 @@ class JsonGraphNode(BaseModel, extra="forbid"):
id: str


class JsonGraphEdgeMeta(BaseModel, extra="forbid"):
kind: str
out_index: Optional[int] = None


class JsonGraphEdge(BaseModel, extra="forbid"):
metadata: Dict = Field(default_factory=dict)
metadata: JsonGraphEdgeMeta
source: str
target: str

Expand Down Expand Up @@ -399,7 +404,9 @@ def toJsonGraphNode(self, idx: int) -> JsonGraphNode:
return res

def toJsonGraphEdge(self, idx: int) -> JsonGraphEdge:
edge = self.getEdgeObj(idx)
res = JsonGraphEdge(
metadata=JsonGraphEdgeMeta(kind=type(edge.data).__name__),
source=self.getStrId(self.getNodeObj(self.getSource(idx))),
target=self.getStrId(self.getNodeObj(self.getTarget(idx))),
)
Expand Down
67 changes: 67 additions & 0 deletions tests/python/test_mind_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from py_cli.scratch_scripts.mind_map import mind_map
import py_haxorg.pyhaxorg_wrap as org
from beartype.typing import Any, Dict, Optional
from beartype import beartype
from py_scriptutils.rich_utils import render_rich_pprint

def getJsonGraph(str: str) -> mind_map.JsonGraph:
node = org.parseString(str)
graph = mind_map.getGraph([node])
return graph.toJsonGraph()


def test_empty_file():
getJsonGraph("")

def dbg(map: mind_map.JsonGraph) -> str:
return render_rich_pprint(map.model_dump(), width=200, color=False)

def get_edge(map: mind_map.JsonGraph, source: str, target: str) -> Optional[mind_map.JsonGraphEdge]:
for edge in map.edges:
if edge.source == source and edge.target == target:
return edge

def test_single_subtree():
map = getJsonGraph("* Subtree")

assert len(map.edges) == 1, dbg(map)
assert len(map.nodes) == 2, dbg(map)
assert map.nodes["0"].metadata.title == None
assert map.nodes["1"].metadata.title == "Subtree"
assert map.edges[0].metadata.kind == "NestedIn"

def test_nested_subtree():
map = getJsonGraph("""
* Top
** Nest
""")

assert len(map.edges) == 2, dbg(map)
assert len(map.nodes) == 3, dbg(map)
assert map.nodes["1"].metadata.title == "Top"
assert map.nodes["2"].metadata.title == "Nest"
assert map.edges[0].metadata.kind == "NestedIn"
assert map.edges[1].metadata.kind == "NestedIn"

def test_link_to_subtree():
map = getJsonGraph("""
* Node
:properties:
:id: tree-id
:end:
[[id:tree-id][description]]
""")

assert len(map.edges) == 4, dbg(map)
assert len(map.nodes) == 3, dbg(map)

assert map.nodes["tree-id"].metadata.title == "Node"

entry_to_tree = get_edge(map, "1", "tree-id")
assert entry_to_tree, dbg(map)
assert entry_to_tree.metadata.kind == "RefersTo", dbg(map)

self_link = get_edge(map, "tree-id", "tree-id")
assert self_link, dbg(map)
assert self_link.metadata.kind == "InternallyRefers", dbg(map)

0 comments on commit d3f0cc3

Please sign in to comment.