graphmanager_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "flag"
  13. "fmt"
  14. "os"
  15. "testing"
  16. "devt.de/krotik/common/fileutil"
  17. "devt.de/krotik/eliasdb/graph/data"
  18. "devt.de/krotik/eliasdb/graph/graphstorage"
  19. )
  20. /*
  21. Flag to enable / disable tests which use actual disk storage.
  22. (Only used for test development - should never be false)
  23. */
  24. const RunDiskStorageTests = true
  25. const GraphManagerTestDBDir1 = "gmtest1"
  26. const GraphManagerTestDBDir2 = "gmtest2"
  27. const GraphManagerTestDBDir3 = "gmtest3"
  28. const GraphManagerTestDBDir4 = "gmtest4"
  29. const GraphManagerTestDBDir5 = "gmtest5"
  30. const GraphManagerTestDBDir6 = "gmtest6"
  31. var DBDIRS = []string{GraphManagerTestDBDir1, GraphManagerTestDBDir2,
  32. GraphManagerTestDBDir3, GraphManagerTestDBDir4, GraphManagerTestDBDir5,
  33. GraphManagerTestDBDir6}
  34. const InvlaidFileName = "**" + "\x00"
  35. // Main function for all tests in this package
  36. func TestMain(m *testing.M) {
  37. flag.Parse()
  38. for _, dbdir := range DBDIRS {
  39. if res, _ := fileutil.PathExists(dbdir); res {
  40. if err := os.RemoveAll(dbdir); err != nil {
  41. fmt.Print("Could not remove test directory:", err.Error())
  42. }
  43. }
  44. }
  45. // Run the tests
  46. res := m.Run()
  47. // Teardown
  48. for _, dbdir := range DBDIRS {
  49. if res, _ := fileutil.PathExists(dbdir); res {
  50. if err := os.RemoveAll(dbdir); err != nil {
  51. fmt.Print("Could not remove test directory:", err.Error())
  52. }
  53. }
  54. }
  55. os.Exit(res)
  56. }
  57. /*
  58. NewGraphManager returns a new GraphManager instance without loading rules.
  59. */
  60. func newGraphManagerNoRules(gs graphstorage.Storage) *Manager {
  61. return createGraphManager(gs)
  62. }
  63. /*
  64. Create a GraphManager which has some prefilled data.
  65. */
  66. func songGraph() (*Manager, *graphstorage.MemoryGraphStorage) {
  67. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  68. gm := NewGraphManager(mgs)
  69. constructEdge := func(key string, node1 data.Node, node2 data.Node, number int) data.Edge {
  70. edge := data.NewGraphEdge()
  71. edge.SetAttr("key", key)
  72. edge.SetAttr("kind", "Wrote")
  73. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  74. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  75. edge.SetAttr(data.EdgeEnd1Role, "Author")
  76. edge.SetAttr(data.EdgeEnd1Cascading, true)
  77. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  78. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  79. edge.SetAttr(data.EdgeEnd2Role, "Song")
  80. edge.SetAttr(data.EdgeEnd2Cascading, false)
  81. edge.SetAttr("number", number)
  82. return edge
  83. }
  84. storeSong := func(node data.Node, name string, ranking int, number int) {
  85. node3 := data.NewGraphNode()
  86. node3.SetAttr("key", name)
  87. node3.SetAttr("kind", "Song")
  88. node3.SetAttr("name", name)
  89. node3.SetAttr("ranking", ranking)
  90. gm.StoreNode("main", node3)
  91. gm.StoreEdge("main", constructEdge(name+"-edge", node, node3, number))
  92. }
  93. node0 := data.NewGraphNode()
  94. node0.SetAttr("key", "000")
  95. node0.SetAttr("kind", "Author")
  96. node0.SetAttr("name", "John")
  97. gm.StoreNode("main", node0)
  98. storeSong(node0, "Aria1", 8, 1)
  99. storeSong(node0, "Aria2", 2, 2)
  100. storeSong(node0, "Aria3", 4, 3)
  101. storeSong(node0, "Aria4", 18, 4)
  102. node1 := data.NewGraphNode()
  103. node1.SetAttr("key", "123")
  104. node1.SetAttr("kind", "Author")
  105. node1.SetAttr("name", "Mike")
  106. gm.StoreNode("main", node1)
  107. storeSong(node1, "LoveSong3", 1, 3)
  108. storeSong(node1, "FightSong4", 3, 4)
  109. storeSong(node1, "DeadSong2", 6, 2)
  110. storeSong(node1, "StrangeSong1", 5, 1)
  111. node2 := data.NewGraphNode()
  112. node2.SetAttr("key", "456")
  113. node2.SetAttr("kind", "Author")
  114. node2.SetAttr("name", "Hans")
  115. gm.StoreNode("main", node2)
  116. storeSong(node2, "MyOnlySong3", 19, 3)
  117. return gm, mgs.(*graphstorage.MemoryGraphStorage)
  118. }