node_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 dbfunc
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/graph"
  15. "devt.de/krotik/eliasdb/graph/graphstorage"
  16. )
  17. func TestStoreAndRemoveNode(t *testing.T) {
  18. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  19. gm := graph.NewGraphManager(mgs)
  20. sn := &StoreNodeFunc{gm}
  21. if _, err := sn.DocString(); err != nil {
  22. t.Error(err)
  23. return
  24. }
  25. if _, err := sn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  26. err.Error() != "Function requires 2 or 3 parameters: partition, node map and optionally a transaction" {
  27. t.Error(err)
  28. return
  29. }
  30. if _, err := sn.Run("", nil, nil, 0, []interface{}{"", "bla"}); err == nil ||
  31. err.Error() != "Second parameter must be a map" {
  32. t.Error(err)
  33. return
  34. }
  35. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{}, "bla"}); err == nil ||
  36. err.Error() != "Third parameter must be a transaction" {
  37. t.Error(err)
  38. return
  39. }
  40. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  41. "key": "foo",
  42. }}); err == nil ||
  43. err.Error() != "GraphError: Invalid data (Node is missing a kind value)" {
  44. t.Error(err)
  45. return
  46. }
  47. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  48. "key": "foo",
  49. "kind": "bar",
  50. }}); err != nil {
  51. t.Error(err)
  52. return
  53. }
  54. if res := gm.NodeCount("bar"); res != 1 {
  55. t.Error("Unexpected result:", res)
  56. return
  57. }
  58. fn := &FetchNodeFunc{gm}
  59. if _, err := fn.DocString(); err != nil {
  60. t.Error(err)
  61. return
  62. }
  63. if _, err := fn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  64. err.Error() != "Function requires 3 parameters: partition, node key node kind" {
  65. t.Error(err)
  66. return
  67. }
  68. if _, err := fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "ba r"}); err == nil ||
  69. err.Error() != "GraphError: Invalid data (Node kind ba r is not alphanumeric - can only contain [a-zA-Z0-9_])" {
  70. t.Error(err)
  71. return
  72. }
  73. res, err := fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  74. if fmt.Sprint(NewGraphNodeFromECALMap(res.(map[interface{}]interface{}))) != `
  75. GraphNode:
  76. key : foo
  77. kind : bar
  78. `[1:] || err != nil {
  79. t.Error("Unexpected result:", res, err)
  80. return
  81. }
  82. rn := &RemoveNodeFunc{gm}
  83. if _, err := rn.DocString(); err != nil {
  84. t.Error(err)
  85. return
  86. }
  87. if _, err := rn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  88. err.Error() != "Function requires 3 or 4 parameters: partition, node key node kind and optionally a transaction" {
  89. t.Error(err)
  90. return
  91. }
  92. if _, err := rn.Run("", nil, nil, 0, []interface{}{"mai n", "foo", "bar"}); err == nil ||
  93. err.Error() != "GraphError: Invalid data (Partition name mai n is not alphanumeric - can only contain [a-zA-Z0-9_])" {
  94. t.Error(err)
  95. return
  96. }
  97. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  98. if err != nil {
  99. t.Error(err)
  100. return
  101. }
  102. res, err = fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  103. if res != nil || err != nil {
  104. t.Error("Unexpected result:", res, err)
  105. return
  106. }
  107. }
  108. func TestStoreNodeTrans(t *testing.T) {
  109. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  110. gm := graph.NewGraphManager(mgs)
  111. tn := &NewTransFunc{gm}
  112. if _, err := tn.DocString(); err != nil {
  113. t.Error(err)
  114. return
  115. }
  116. tn2 := &NewRollingTransFunc{gm}
  117. if _, err := tn2.DocString(); err != nil {
  118. t.Error(err)
  119. return
  120. }
  121. tc := &CommitTransFunc{gm}
  122. if _, err := tc.DocString(); err != nil {
  123. t.Error(err)
  124. return
  125. }
  126. if _, err := tn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  127. err.Error() != "Function does not require any parameters" {
  128. t.Error(err)
  129. return
  130. }
  131. if _, err := tn2.Run("", nil, nil, 0, []interface{}{"", ""}); err == nil ||
  132. err.Error() != "Function requires the rolling threshold (number of operations before rolling)" {
  133. t.Error(err)
  134. return
  135. }
  136. if _, err := tc.Run("", nil, nil, 0, []interface{}{"", ""}); err == nil ||
  137. err.Error() != "Function requires the transaction to commit as parameter" {
  138. t.Error(err)
  139. return
  140. }
  141. if _, err := tc.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  142. err.Error() != "Parameter must be a transaction" {
  143. t.Error(err)
  144. return
  145. }
  146. trans, err := tn.Run("", nil, nil, 0, []interface{}{})
  147. if err != nil {
  148. t.Error(err)
  149. return
  150. }
  151. _, err = tn2.Run("", nil, nil, 0, []interface{}{"foo"})
  152. if err == nil || err.Error() != "Rolling threshold must be a number not: foo" {
  153. t.Error(err)
  154. return
  155. }
  156. _, err = tn2.Run("", nil, nil, 0, []interface{}{1})
  157. if err != nil {
  158. t.Error(err)
  159. return
  160. }
  161. sn := &StoreNodeFunc{gm}
  162. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  163. "key": "foo1",
  164. "kind": "bar",
  165. }, trans}); err != nil {
  166. t.Error(err)
  167. return
  168. }
  169. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  170. "key": "foo2",
  171. "kind": "bar",
  172. }, trans}); err != nil {
  173. t.Error(err)
  174. return
  175. }
  176. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  177. "key": "foo3",
  178. "kind": "bar",
  179. }, trans}); err != nil {
  180. t.Error(err)
  181. return
  182. }
  183. // Check that the nodes are in the transaction
  184. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "3 0 0 0" {
  185. t.Error("Unexpected result:", res)
  186. return
  187. }
  188. if res := gm.NodeCount("bar"); res != 0 {
  189. t.Error("Unexpected result:", res)
  190. return
  191. }
  192. // Commit the nodes
  193. if _, err := tc.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  194. "key": "foo3",
  195. "kind": "bar",
  196. }, trans}); err == nil || err.Error() != "Function requires the transaction to commit as parameter" {
  197. t.Error(err)
  198. return
  199. }
  200. if _, err := tc.Run("", nil, nil, 0, []interface{}{trans}); err != nil {
  201. t.Error(err)
  202. return
  203. }
  204. // Check that the nodes have been committed
  205. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 0 0" {
  206. t.Error("Unexpected result:", res)
  207. return
  208. }
  209. if res := gm.NodeCount("bar"); res != 3 {
  210. t.Error("Unexpected result:", res)
  211. return
  212. }
  213. // Remove the nodes
  214. rn := &RemoveNodeFunc{gm}
  215. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo1", "bar", nil})
  216. if err == nil || err.Error() != "Fourth parameter must be a transaction" {
  217. t.Error(err)
  218. return
  219. }
  220. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo1", "bar", trans})
  221. if err != nil {
  222. t.Error(err)
  223. return
  224. }
  225. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo2", "bar", trans})
  226. if err != nil {
  227. t.Error(err)
  228. return
  229. }
  230. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo3", "bar", trans})
  231. if err != nil {
  232. t.Error(err)
  233. return
  234. }
  235. // Check that the nodes are in the transaction
  236. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 3 0" {
  237. t.Error("Unexpected result:", res)
  238. return
  239. }
  240. if res := gm.NodeCount("bar"); res != 3 {
  241. t.Error("Unexpected result:", res)
  242. return
  243. }
  244. // Commit the nodes
  245. if _, err := tc.Run("", nil, nil, 0, []interface{}{trans}); err != nil {
  246. t.Error(err)
  247. return
  248. }
  249. // Check that the nodes have been committed
  250. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 0 0" {
  251. t.Error("Unexpected result:", res)
  252. return
  253. }
  254. if res := gm.NodeCount("bar"); res != 0 {
  255. t.Error("Unexpected result:", res)
  256. return
  257. }
  258. }