util_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 TestNodeCompare(t *testing.T) {
  16. gn1 := NewGraphNode()
  17. gn1.SetAttr("Test1", "test")
  18. gn2 := NewGraphNode()
  19. gn1.SetAttr("test1", "test")
  20. if NodeCompare(gn1, gn2, []string{"Test1"}) {
  21. t.Error("Unexpected compare result")
  22. return
  23. }
  24. gn2.SetAttr("Test1", "test")
  25. if !NodeCompare(gn1, gn2, []string{"Test1"}) {
  26. t.Error("Unexpected compare result")
  27. return
  28. }
  29. if NodeCompare(gn1, gn2, nil) {
  30. t.Error("Unexpected compare result")
  31. return
  32. }
  33. gn1.SetAttr("test1", nil)
  34. if !NodeCompare(gn1, gn2, nil) {
  35. t.Error("Unexpected compare result")
  36. return
  37. }
  38. }
  39. func TestNodeClone(t *testing.T) {
  40. gn1 := NewGraphNode()
  41. gn1.SetAttr("Test1", "test")
  42. gn2 := NodeClone(gn1)
  43. if !NodeCompare(gn1, gn2, nil) {
  44. t.Error("Node should be a clone")
  45. return
  46. }
  47. gn1.SetAttr("Test1", "test2")
  48. if NodeCompare(gn1, gn2, nil) {
  49. t.Error("Node should be different now")
  50. return
  51. }
  52. }
  53. func TestNodeMerge(t *testing.T) {
  54. gn1 := NewGraphNode()
  55. gn1.SetAttr("Test1", "test1")
  56. gn2 := NewGraphNode()
  57. gn2.SetAttr("Test2", "test2")
  58. gn3 := NodeMerge(gn1, gn2)
  59. if gn1.Attr("Test1") != gn3.Attr("Test1") {
  60. t.Error("Nodes should have been merged")
  61. return
  62. }
  63. if gn2.Attr("Test2") != gn3.Attr("Test2") {
  64. t.Error("Nodes should have been merged")
  65. return
  66. }
  67. }
  68. func TestNodeSort(t *testing.T) {
  69. nodes := []Node{
  70. NewGraphNodeFromMap(map[string]interface{}{
  71. "key": "aaa",
  72. "kind": "bbb",
  73. }),
  74. NewGraphNodeFromMap(map[string]interface{}{
  75. "key": "ddd",
  76. "kind": "bbb",
  77. }),
  78. NewGraphNodeFromMap(map[string]interface{}{
  79. "key": "ggg",
  80. "kind": "bbb",
  81. }),
  82. NewGraphNodeFromMap(map[string]interface{}{
  83. "key": "aaa",
  84. "kind": "ccc",
  85. }),
  86. NewGraphNodeFromMap(map[string]interface{}{
  87. "key": "aaa",
  88. "kind": "000",
  89. }),
  90. }
  91. NodeSort(nodes)
  92. if res := fmt.Sprint(nodes); res != `
  93. [GraphNode:
  94. key : aaa
  95. kind : 000
  96. GraphNode:
  97. key : aaa
  98. kind : bbb
  99. GraphNode:
  100. key : ddd
  101. kind : bbb
  102. GraphNode:
  103. key : ggg
  104. kind : bbb
  105. GraphNode:
  106. key : aaa
  107. kind : ccc
  108. ]`[1:] {
  109. t.Error("Unexpected result: ", res)
  110. return
  111. }
  112. }