node_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "bytes"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "testing"
  17. )
  18. func TestGraphNode(t *testing.T) {
  19. gn := NewGraphNode()
  20. if res := gn.Key(); res != "" {
  21. t.Error("Unexpected key:", res)
  22. return
  23. }
  24. if res := gn.Name(); res != "" {
  25. t.Error("Unexpected name:", res)
  26. return
  27. }
  28. if res := gn.Kind(); res != "" {
  29. t.Error("Unexpected kind:", res)
  30. return
  31. }
  32. if res := gn.Attr("a"); res != nil {
  33. t.Error("Unexpected result:", res)
  34. return
  35. }
  36. gn.SetAttr(NodeKey, "123")
  37. if res := gn.Attr(NodeKey); res != "123" {
  38. t.Error("Unexpected key:", res)
  39. return
  40. }
  41. if res := gn.Key(); res != "123" {
  42. t.Error("Unexpected key:", res)
  43. return
  44. }
  45. gn.SetAttr(NodeKind, "mykind")
  46. if res := gn.Attr(NodeKind); res != "mykind" {
  47. t.Error("Unexpected kind:", res)
  48. return
  49. }
  50. if res := gn.Kind(); res != "mykind" {
  51. t.Error("Unexpected kind:", res)
  52. return
  53. }
  54. gn.SetAttr("myattr", 123)
  55. gn.SetAttr("myattr2", bytes.NewBuffer([]byte("abba")))
  56. gn.SetAttr("myattr3", "test123")
  57. if res := gn.Attr("myattr"); res != 123 {
  58. t.Error("Unexpected attr:", res)
  59. return
  60. }
  61. if res := gn.(*graphNode).stringAttr("myattr2"); res != "abba" {
  62. t.Error("Unexpected attr:", res)
  63. return
  64. }
  65. im := gn.IndexMap()
  66. if im["myattr"] != "123" || im["myattr2"] != "abba" ||
  67. im["myattr3"] != "test123" || len(im) != 3 {
  68. t.Error("Unexpected indexmap result:", gn.IndexMap())
  69. return
  70. }
  71. gn.SetAttr("myattr", nil)
  72. if res := gn.Attr("myattr"); res != nil {
  73. t.Error("Unexpected attr:", res)
  74. return
  75. }
  76. gn.SetAttr("amyattr", "another test")
  77. if res := gn.String(); res != "GraphNode:\n"+
  78. " key : 123\n"+
  79. " kind : mykind\n"+
  80. " amyattr : another test\n"+
  81. " myattr2 : abba\n"+
  82. " myattr3 : test123\n" {
  83. t.Error("Unexpected string output:", res)
  84. return
  85. }
  86. nodedata := gn.Data()
  87. if nodedata["key"] != gn.(*graphNode).data["key"] {
  88. t.Error("Unexpected data reference")
  89. return
  90. }
  91. nnode := NewGraphNodeFromMap(gn.Data())
  92. if nnode.Data()["key"] != gn.(*graphNode).data["key"] {
  93. t.Error("Unexpected data reference")
  94. return
  95. }
  96. gn = CopyNode(gn)
  97. gn.SetAttr("key", []int{1, 2, 3})
  98. if res := gn.String(); res != "GraphNode:\n"+
  99. " key : [1 2 3]\n"+
  100. " kind : mykind\n"+
  101. " amyattr : another test\n"+
  102. " myattr2 : abba\n"+
  103. " myattr3 : test123\n" {
  104. t.Error("Unexpected string output:", res)
  105. return
  106. }
  107. if gn.Key() != "[1 2 3]" {
  108. t.Error("Unexpected key as string:", gn.Key())
  109. return
  110. }
  111. if fmt.Sprintf("%T", gn.Attr("key")) != "[]int" {
  112. t.Errorf("Unexpected ckey type: %T", gn.Attr("key"))
  113. return
  114. }
  115. }
  116. func TestNestedNode(t *testing.T) {
  117. gn := NewGraphNode()
  118. gn.SetAttr("key", "456")
  119. gn.SetAttr("kind", "mynode")
  120. gn.SetAttr("name", "Node2")
  121. gn.SetAttr("type", "type2")
  122. gn.SetAttr("err", errors.New("bla"))
  123. gn.SetAttr("nested", map[string]interface{}{
  124. "nest1": map[string]interface{}{
  125. "nest2": map[string]interface{}{
  126. "atom1": 1.45,
  127. },
  128. },
  129. })
  130. im := gn.IndexMap()
  131. jsonString, err := json.MarshalIndent(im, "", " ")
  132. if err != nil {
  133. t.Error(err)
  134. return
  135. }
  136. if string(jsonString) != `
  137. {
  138. "err": "bla",
  139. "name": "Node2",
  140. "nested": "{\"nest1\":{\"nest2\":{\"atom1\":1.45}}}",
  141. "nested.nest1": "{\"nest2\":{\"atom1\":1.45}}",
  142. "nested.nest1.nest2": "{\"atom1\":1.45}",
  143. "nested.nest1.nest2.atom1": "1.45",
  144. "type": "type2"
  145. }`[1:] {
  146. t.Error("Unexpected result: ", string(jsonString))
  147. return
  148. }
  149. }