_graphviz_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # yamify - define family trees in YAML
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import datetime
  18. from yamily import Person, PersonCollection
  19. from yamily._graphviz import digraph
  20. def test_digraph_person_details():
  21. collection = PersonCollection()
  22. person = Person("bob")
  23. person.name = "Bob Test"
  24. person.birth_date = datetime.date(1923, 4, 5)
  25. person.death_date = datetime.date(2012, 3, 4)
  26. collection.add_person(person)
  27. graph = digraph(collection)
  28. expected_graph = r"""digraph yamily {
  29. subgraph cluster_bob {
  30. rank=same style=invisible
  31. bob [label="Bob Test\n*1923-04-05\n†2012-03-04" shape=box]
  32. }
  33. }"""
  34. assert graph.source == expected_graph
  35. def test_digraph_single_family():
  36. collection = PersonCollection()
  37. child = Person("child")
  38. child.father = Person("father")
  39. child.mother = Person("mother")
  40. collection.add_person(child)
  41. graph = digraph(collection)
  42. expected_graph = """digraph yamily {
  43. subgraph cluster_mother {
  44. rank=same style=invisible
  45. mother [label=mother shape=box]
  46. "relation-father-mother" [shape=point width=0]
  47. father -> "relation-father-mother" [arrowhead=none constraint=False]
  48. mother -> "relation-father-mother" [arrowhead=none constraint=False]
  49. father [label=father shape=box]
  50. }
  51. subgraph cluster_child {
  52. rank=same style=invisible
  53. child [label=child shape=box]
  54. }
  55. "relation-father-mother" -> child
  56. }"""
  57. assert graph.source == expected_graph
  58. def test_digraph_grandparents():
  59. collection = PersonCollection()
  60. father = Person("father")
  61. father.father = Person("grandfather")
  62. father.mother = Person("grandmother")
  63. child_a = Person("child-a")
  64. child_a.father = father
  65. child_a.mother = Person("mother")
  66. collection.add_person(child_a)
  67. child_b = Person("child-b")
  68. child_b.father = father
  69. child_b.mother = Person("mother")
  70. collection.add_person(child_b)
  71. graph = digraph(collection)
  72. expected_graph = """digraph yamily {
  73. subgraph cluster_mother {
  74. rank=same style=invisible
  75. mother [label=mother shape=box]
  76. "relation-father-mother" [shape=point width=0]
  77. father -> "relation-father-mother" [arrowhead=none constraint=False]
  78. mother -> "relation-father-mother" [arrowhead=none constraint=False]
  79. father [label=father shape=box]
  80. }
  81. subgraph cluster_grandmother {
  82. rank=same style=invisible
  83. grandmother [label=grandmother shape=box]
  84. "relation-grandfather-grandmother" [shape=point width=0]
  85. grandfather -> "relation-grandfather-grandmother" [arrowhead=none constraint=False]
  86. grandmother -> "relation-grandfather-grandmother" [arrowhead=none constraint=False]
  87. grandfather [label=grandfather shape=box]
  88. }
  89. subgraph "cluster_child-a" {
  90. rank=same style=invisible
  91. "child-a" [label="child-a" shape=box]
  92. }
  93. subgraph "cluster_child-b" {
  94. rank=same style=invisible
  95. "child-b" [label="child-b" shape=box]
  96. }
  97. "relation-grandfather-grandmother" -> father
  98. "relation-father-mother" -> "child-a"
  99. "relation-father-mother" -> "child-b"
  100. }"""
  101. assert graph.source == expected_graph