edge_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package data
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestGraphEdge(t *testing.T) {
  16. ge := NewGraphEdge()
  17. ge.SetAttr(NodeKey, "123")
  18. ge.SetAttr(NodeKind, "myedgekind")
  19. ge.SetAttr(EdgeEnd1Key, "456")
  20. ge.SetAttr(EdgeEnd1Kind, "mynodekind1")
  21. ge.SetAttr(EdgeEnd1Role, "role1")
  22. ge.SetAttr(EdgeEnd1Cascading, true)
  23. ge.SetAttr(EdgeEnd1CascadingLast, true)
  24. ge.SetAttr(EdgeEnd2Key, "789")
  25. ge.SetAttr(EdgeEnd2Kind, "mynodekind2")
  26. ge.SetAttr(EdgeEnd2Role, "role2")
  27. ge.SetAttr(EdgeEnd2Cascading, false)
  28. ge.SetAttr(EdgeEnd2CascadingLast, false)
  29. ge.SetAttr("name", "test")
  30. if ge.End1Key() != "456" {
  31. t.Error("Unexpected result")
  32. return
  33. }
  34. if ge.End1Kind() != "mynodekind1" {
  35. t.Error("Unexpected result")
  36. return
  37. }
  38. if ge.End1Role() != "role1" {
  39. t.Error("Unexpected result")
  40. return
  41. }
  42. if ge.End1IsCascading() != true {
  43. t.Error("Unexpected result")
  44. return
  45. }
  46. if ge.End1IsCascadingLast() != true {
  47. t.Error("Unexpected result")
  48. return
  49. }
  50. if ge.End2Key() != "789" {
  51. t.Error("Unexpected result")
  52. return
  53. }
  54. if ge.End2Kind() != "mynodekind2" {
  55. t.Error("Unexpected result")
  56. return
  57. }
  58. if ge.End2Role() != "role2" {
  59. t.Error("Unexpected result")
  60. return
  61. }
  62. if ge.End2IsCascading() != false {
  63. t.Error("Unexpected result")
  64. return
  65. }
  66. if ge.End2IsCascadingLast() != false {
  67. t.Error("Unexpected result")
  68. return
  69. }
  70. if ge.Spec("123") != "" {
  71. t.Error("Unexpected result")
  72. return
  73. }
  74. if ge.Spec("456") != "role1:myedgekind:role2:mynodekind2" {
  75. t.Error("Unexpected result")
  76. return
  77. }
  78. if ge.Spec("789") != "role2:myedgekind:role1:mynodekind1" {
  79. t.Error("Unexpected result")
  80. return
  81. }
  82. if ge.OtherEndKey("123") != "" {
  83. t.Error("Unexpected result")
  84. return
  85. }
  86. if ge.OtherEndKey("456") != "789" {
  87. t.Error("Unexpected result")
  88. return
  89. }
  90. if ge.OtherEndKey("789") != "456" {
  91. t.Error("Unexpected result")
  92. return
  93. }
  94. if ge.OtherEndKind("123") != "" {
  95. t.Error("Unexpected result")
  96. return
  97. }
  98. if ge.OtherEndKind("456") != "mynodekind2" {
  99. t.Error("Unexpected result")
  100. return
  101. }
  102. if ge.OtherEndKind("789") != "mynodekind1" {
  103. t.Error("Unexpected result")
  104. return
  105. }
  106. if res := fmt.Sprint(ge.IndexMap()); res != "map[name:test]" {
  107. t.Error("Unexpected result:", res)
  108. return
  109. }
  110. gn := NewGraphNode()
  111. gn.(*graphNode).data = ge.Data()
  112. newEdge := NewGraphEdgeFromNode(nil)
  113. if newEdge != nil {
  114. t.Error("Unexpected result")
  115. return
  116. }
  117. newEdge = NewGraphEdgeFromNode(gn)
  118. if !NodeCompare(ge, newEdge, nil) {
  119. t.Error("Unexpected result")
  120. return
  121. }
  122. if newEdge.String() != `GraphEdge:
  123. key : 123
  124. kind : myedgekind
  125. end1cascading : true
  126. end1cascadinglast : true
  127. end1key : 456
  128. end1kind : mynodekind1
  129. end1role : role1
  130. end2cascading : false
  131. end2cascadinglast : false
  132. end2key : 789
  133. end2kind : mynodekind2
  134. end2role : role2
  135. name : test
  136. ` {
  137. t.Error("Unexpected edge string output:", newEdge)
  138. return
  139. }
  140. }