iterator_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 graph
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/graph/data"
  14. "devt.de/krotik/eliasdb/graph/graphstorage"
  15. "devt.de/krotik/eliasdb/storage"
  16. )
  17. func TestNodeKeyIterator(t *testing.T) {
  18. mgs := graphstorage.NewMemoryGraphStorage("iterator test")
  19. gm := newGraphManagerNoRules(mgs)
  20. node1 := data.NewGraphNode()
  21. node1.SetAttr("key", "123")
  22. node1.SetAttr("kind", "mykind")
  23. node1.SetAttr("Name", "Node1")
  24. gm.StoreNode("main", node1)
  25. node2 := data.NewGraphNode()
  26. node2.SetAttr("key", "456")
  27. node2.SetAttr("kind", "mykind2")
  28. node2.SetAttr("Name", "Node2")
  29. gm.StoreNode("main", node2)
  30. node3 := data.NewGraphNode()
  31. node3.SetAttr("key", "789")
  32. node3.SetAttr("kind", "mykind")
  33. node3.SetAttr("Name", "Node3")
  34. gm.StoreNode("main", node3)
  35. edge := data.NewGraphEdge()
  36. edge.SetAttr("key", "abc")
  37. edge.SetAttr("kind", "myedge")
  38. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  39. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  40. edge.SetAttr(data.EdgeEnd1Role, "node1")
  41. edge.SetAttr(data.EdgeEnd1Cascading, true)
  42. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  43. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  44. edge.SetAttr(data.EdgeEnd2Role, "node2")
  45. edge.SetAttr(data.EdgeEnd2Cascading, false)
  46. gm.StoreEdge("main", edge)
  47. ni, err := gm.NodeKeyIterator("main", "mykind")
  48. if err != nil {
  49. t.Error(err)
  50. return
  51. }
  52. expectedKeys := []string{"123", "789"}
  53. i := 0
  54. for ni.HasNext() {
  55. key := ni.Next()
  56. if key != expectedKeys[i] {
  57. t.Error("Unexpected key:", key, "expected", expectedKeys[i])
  58. return
  59. }
  60. if ni.LastError != nil {
  61. t.Error(ni.LastError)
  62. return
  63. }
  64. i++
  65. }
  66. ni, err = gm.NodeKeyIterator("main", "mykind")
  67. if err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. msm := mgs.StorageManager("main"+"mykind"+StorageSuffixNodes, false)
  72. tree, _, _ := gm.getNodeStorageHTree("main", "mykind", false)
  73. _, loc, _ := tree.GetValueAndLocation([]byte(PrefixNSAttrs + "123"))
  74. msm.(*storage.MemoryStorageManager).AccessMap[loc] = storage.AccessCacheAndFetchSeriousError
  75. ni.Next()
  76. if ni.LastError == nil {
  77. t.Error("Expected an error to occur")
  78. return
  79. }
  80. delete(msm.(*storage.MemoryStorageManager).AccessMap, loc)
  81. msm.(*storage.MemoryStorageManager).AccessMap[1] = storage.AccessCacheAndFetchError
  82. ni, err = gm.NodeKeyIterator("main", "mykind")
  83. if ni != nil || err == nil {
  84. t.Error("Key iterator should not be created at this point")
  85. return
  86. }
  87. delete(msm.(*storage.MemoryStorageManager).AccessMap, 1)
  88. msm.(*storage.MemoryStorageManager).AccessMap[loc] = storage.AccessCacheAndFetchSeriousError
  89. ni, err = gm.NodeKeyIterator("main", "mykind")
  90. if ni != nil || err == nil {
  91. t.Error("Key iterator should not be created at this point")
  92. return
  93. }
  94. delete(msm.(*storage.MemoryStorageManager).AccessMap, loc)
  95. // Test iterator running out of items
  96. ni, err = gm.NodeKeyIterator("main", "mykind")
  97. if err != nil {
  98. t.Error(err)
  99. return
  100. }
  101. ni.Next()
  102. ni.Next()
  103. ni.Next()
  104. if ni.Next() != "" || ni.Error() != nil {
  105. t.Error("Expected iterator to run out of items:", ni.Error())
  106. return
  107. }
  108. }