Browse Source

graph: move doctest to tests/

Fabian Peter Hammerle 4 years ago
parent
commit
4ad732f77a
2 changed files with 48 additions and 39 deletions
  1. 46 0
      tests/_graphviz_test.py
  2. 2 39
      yamily/_graphviz.py

+ 46 - 0
tests/_graphviz_test.py

@@ -42,3 +42,49 @@ def test_digraph_single_family():
 	"relation-father-mother" -> child
 }"""
     assert graph.source == expected_graph
+
+
+def test_digraph_grandparents():
+    collection = PersonCollection()
+    father = Person("father")
+    father.father = Person("grandfather")
+    father.mother = Person("grandmother")
+    child_a = Person("child-a")
+    child_a.father = father
+    child_a.mother = Person("mother")
+    collection.add_person(child_a)
+    child_b = Person("child-b")
+    child_b.father = father
+    child_b.mother = Person("mother")
+    collection.add_person(child_b)
+    graph = digraph(collection)
+    expected_graph = """digraph yamily {
+	subgraph cluster_mother {
+		rank=same style=invisible
+		mother [label=mother shape=box]
+		"relation-father-mother" [shape=point width=0]
+		father -> "relation-father-mother" [arrowhead=none constraint=False]
+		mother -> "relation-father-mother" [arrowhead=none constraint=False]
+		father [label=father shape=box]
+	}
+	subgraph cluster_grandmother {
+		rank=same style=invisible
+		grandmother [label=grandmother shape=box]
+		"relation-grandfather-grandmother" [shape=point width=0]
+		grandfather -> "relation-grandfather-grandmother" [arrowhead=none constraint=False]
+		grandmother -> "relation-grandfather-grandmother" [arrowhead=none constraint=False]
+		grandfather [label=grandfather shape=box]
+	}
+	subgraph "cluster_child-a" {
+		rank=same style=invisible
+		"child-a" [label="child-a" shape=box]
+	}
+	subgraph "cluster_child-b" {
+		rank=same style=invisible
+		"child-b" [label="child-b" shape=box]
+	}
+	"relation-grandfather-grandmother" -> father
+	"relation-father-mother" -> "child-a"
+	"relation-father-mother" -> "child-b"
+}"""
+    assert graph.source == expected_graph

+ 2 - 39
yamily/_graphviz.py

@@ -53,49 +53,12 @@ def digraph(collection: PersonCollection) -> graphviz.dot.Digraph:
     >>> alice.mother = carol
     >>> alice.father = bob
 
-    >>> david = Person('david')
-    >>> david.mother = carol
-    >>> david.father = bob
-
     >>> collection = PersonCollection()
     >>> collection.add_person(alice)
     Person(alice)
-    >>> collection.add_person(david)
-    Person(david)
     >>> graph = digraph(collection)
-    >>> print(graph.source)
-    digraph yamily {
-    	subgraph cluster_grace {
-    		rank=same style=invisible
-    		grace [label=grace shape=box]
-    	}
-    	subgraph cluster_carol {
-    		rank=same style=invisible
-    		carol [label=carol shape=box]
-    		"relation-bob-carol" [shape=point width=0]
-    		bob -> "relation-bob-carol" [arrowhead=none constraint=False]
-    		carol -> "relation-bob-carol" [arrowhead=none constraint=False]
-    		bob [label=bob shape=box]
-    	}
-    	subgraph cluster_frank {
-    		rank=same style=invisible
-    		frank [label=frank shape=box]
-    	}
-    	subgraph cluster_alice {
-    		rank=same style=invisible
-    		alice [label=alice shape=box]
-    	}
-    	subgraph cluster_david {
-    		rank=same style=invisible
-    		david [label=david shape=box]
-    	}
-    	grace -> carol
-    	frank -> bob
-    	"relation-bob-carol" -> alice
-    	"relation-bob-carol" -> david
-    }
-    >>> graph.render('/tmp/yamily.gv')
-    '/tmp/yamily.gv.pdf'
+    >>> graph.render("/tmp/tree.dot")
+    '/tmp/tree.dot.pdf'
     """
     graph = graphviz.Digraph("yamily")
     nodes: typing.Set[Person] = set()