node_test.go 8.3 KB

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