node_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. "data": "123",
  51. "data2": "1234",
  52. }}); err != nil {
  53. t.Error(err)
  54. return
  55. }
  56. if res := gm.NodeCount("bar"); res != 1 {
  57. t.Error("Unexpected result:", res)
  58. return
  59. }
  60. un := &UpdateNodeFunc{gm}
  61. if _, err := un.DocString(); err != nil {
  62. t.Error(err)
  63. return
  64. }
  65. if _, err := un.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  66. err.Error() != "Function requires 2 or 3 parameters: partition, node map and optionally a transaction" {
  67. t.Error(err)
  68. return
  69. }
  70. if _, err := un.Run("", nil, nil, 0, []interface{}{"", "bla"}); err == nil ||
  71. err.Error() != "Second parameter must be a map" {
  72. t.Error(err)
  73. return
  74. }
  75. if _, err := un.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{}, "bla"}); err == nil ||
  76. err.Error() != "Third parameter must be a transaction" {
  77. t.Error(err)
  78. return
  79. }
  80. if _, err := un.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  81. "key": "foo",
  82. }}); err == nil ||
  83. err.Error() != "GraphError: Invalid data (Node is missing a kind value)" {
  84. t.Error(err)
  85. return
  86. }
  87. if _, err := un.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  88. "key": "foo",
  89. "kind": "bar",
  90. "data": "1234",
  91. }}); err != nil {
  92. t.Error(err)
  93. return
  94. }
  95. if res := gm.NodeCount("bar"); res != 1 {
  96. t.Error("Unexpected result:", res)
  97. return
  98. }
  99. fn := &FetchNodeFunc{gm}
  100. if _, err := fn.DocString(); err != nil {
  101. t.Error(err)
  102. return
  103. }
  104. if _, err := fn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  105. err.Error() != "Function requires 3 parameters: partition, node key node kind" {
  106. t.Error(err)
  107. return
  108. }
  109. if _, err := fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "ba r"}); err == nil ||
  110. err.Error() != "GraphError: Invalid data (Node kind ba r is not alphanumeric - can only contain [a-zA-Z0-9_])" {
  111. t.Error(err)
  112. return
  113. }
  114. res, err := fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  115. if fmt.Sprint(NewGraphNodeFromECALMap(res.(map[interface{}]interface{}))) != `
  116. GraphNode:
  117. key : foo
  118. kind : bar
  119. data : 1234
  120. data2 : 1234
  121. `[1:] || err != nil {
  122. t.Error("Unexpected result:\n", res, err)
  123. return
  124. }
  125. rn := &RemoveNodeFunc{gm}
  126. if _, err := rn.DocString(); err != nil {
  127. t.Error(err)
  128. return
  129. }
  130. if _, err := rn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  131. err.Error() != "Function requires 3 or 4 parameters: partition, node key node kind and optionally a transaction" {
  132. t.Error(err)
  133. return
  134. }
  135. if _, err := rn.Run("", nil, nil, 0, []interface{}{"mai n", "foo", "bar"}); err == nil ||
  136. err.Error() != "GraphError: Invalid data (Partition name mai n is not alphanumeric - can only contain [a-zA-Z0-9_])" {
  137. t.Error(err)
  138. return
  139. }
  140. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  141. if err != nil {
  142. t.Error(err)
  143. return
  144. }
  145. res, err = fn.Run("", nil, nil, 0, []interface{}{"main", "foo", "bar"})
  146. if res != nil || err != nil {
  147. t.Error("Unexpected result:", res, err)
  148. return
  149. }
  150. }
  151. func TestStoreNodeTrans(t *testing.T) {
  152. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  153. gm := graph.NewGraphManager(mgs)
  154. tn := &NewTransFunc{gm}
  155. if _, err := tn.DocString(); err != nil {
  156. t.Error(err)
  157. return
  158. }
  159. tn2 := &NewRollingTransFunc{gm}
  160. if _, err := tn2.DocString(); err != nil {
  161. t.Error(err)
  162. return
  163. }
  164. tc := &CommitTransFunc{gm}
  165. if _, err := tc.DocString(); err != nil {
  166. t.Error(err)
  167. return
  168. }
  169. if _, err := tn.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  170. err.Error() != "Function does not require any parameters" {
  171. t.Error(err)
  172. return
  173. }
  174. if _, err := tn2.Run("", nil, nil, 0, []interface{}{"", ""}); err == nil ||
  175. err.Error() != "Function requires the rolling threshold (number of operations before rolling)" {
  176. t.Error(err)
  177. return
  178. }
  179. if _, err := tc.Run("", nil, nil, 0, []interface{}{"", ""}); err == nil ||
  180. err.Error() != "Function requires the transaction to commit as parameter" {
  181. t.Error(err)
  182. return
  183. }
  184. if _, err := tc.Run("", nil, nil, 0, []interface{}{""}); err == nil ||
  185. err.Error() != "Parameter must be a transaction" {
  186. t.Error(err)
  187. return
  188. }
  189. trans, err := tn.Run("", nil, nil, 0, []interface{}{})
  190. if err != nil {
  191. t.Error(err)
  192. return
  193. }
  194. _, err = tn2.Run("", nil, nil, 0, []interface{}{"foo"})
  195. if err == nil || err.Error() != "Rolling threshold must be a number not: foo" {
  196. t.Error(err)
  197. return
  198. }
  199. _, err = tn2.Run("", nil, nil, 0, []interface{}{1})
  200. if err != nil {
  201. t.Error(err)
  202. return
  203. }
  204. sn := &StoreNodeFunc{gm}
  205. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  206. "key": "foo1",
  207. "kind": "bar",
  208. }, trans}); err != nil {
  209. t.Error(err)
  210. return
  211. }
  212. if _, err := sn.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  213. "key": "foo2",
  214. "kind": "bar",
  215. }, trans}); err != nil {
  216. t.Error(err)
  217. return
  218. }
  219. un := &UpdateNodeFunc{gm}
  220. if _, err := un.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  221. "key": "foo3",
  222. "kind": "bar",
  223. }, trans}); err != nil {
  224. t.Error(err)
  225. return
  226. }
  227. // Check that the nodes are in the transaction
  228. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "3 0 0 0" {
  229. t.Error("Unexpected result:", res)
  230. return
  231. }
  232. if res := gm.NodeCount("bar"); res != 0 {
  233. t.Error("Unexpected result:", res)
  234. return
  235. }
  236. // Commit the nodes
  237. if _, err := tc.Run("", nil, nil, 0, []interface{}{"main", map[interface{}]interface{}{
  238. "key": "foo3",
  239. "kind": "bar",
  240. }, trans}); err == nil || err.Error() != "Function requires the transaction to commit as parameter" {
  241. t.Error(err)
  242. return
  243. }
  244. if _, err := tc.Run("", nil, nil, 0, []interface{}{trans}); err != nil {
  245. t.Error(err)
  246. return
  247. }
  248. // Check that the nodes have been committed
  249. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 0 0" {
  250. t.Error("Unexpected result:", res)
  251. return
  252. }
  253. if res := gm.NodeCount("bar"); res != 3 {
  254. t.Error("Unexpected result:", res)
  255. return
  256. }
  257. // Remove the nodes
  258. rn := &RemoveNodeFunc{gm}
  259. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo1", "bar", nil})
  260. if err == nil || err.Error() != "Fourth parameter must be a transaction" {
  261. t.Error(err)
  262. return
  263. }
  264. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo1", "bar", trans})
  265. if err != nil {
  266. t.Error(err)
  267. return
  268. }
  269. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo2", "bar", trans})
  270. if err != nil {
  271. t.Error(err)
  272. return
  273. }
  274. _, err = rn.Run("", nil, nil, 0, []interface{}{"main", "foo3", "bar", trans})
  275. if err != nil {
  276. t.Error(err)
  277. return
  278. }
  279. // Check that the nodes are in the transaction
  280. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 3 0" {
  281. t.Error("Unexpected result:", res)
  282. return
  283. }
  284. if res := gm.NodeCount("bar"); res != 3 {
  285. t.Error("Unexpected result:", res)
  286. return
  287. }
  288. // Commit the nodes
  289. if _, err := tc.Run("", nil, nil, 0, []interface{}{trans}); err != nil {
  290. t.Error(err)
  291. return
  292. }
  293. // Check that the nodes have been committed
  294. if res := fmt.Sprint(trans.(graph.Trans).Counts()); res != "0 0 0 0" {
  295. t.Error("Unexpected result:", res)
  296. return
  297. }
  298. if res := gm.NodeCount("bar"); res != 0 {
  299. t.Error("Unexpected result:", res)
  300. return
  301. }
  302. }