rules_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/graph/data"
  15. "devt.de/krotik/eliasdb/graph/graphstorage"
  16. "devt.de/krotik/eliasdb/graph/util"
  17. )
  18. type TestRule struct {
  19. handledError bool
  20. processingError bool
  21. commitError bool
  22. handles []int
  23. }
  24. func (r *TestRule) Name() string {
  25. return "testrule"
  26. }
  27. func (r *TestRule) Handles() []int {
  28. return r.handles
  29. }
  30. func (r *TestRule) Handle(gm *Manager, trans Trans, event int, ed ...interface{}) error {
  31. if r.handledError {
  32. return ErrEventHandled
  33. }
  34. if r.processingError {
  35. return &util.GraphError{Type: util.ErrAccessComponent, Detail: "Test error"}
  36. }
  37. if r.commitError {
  38. node := data.NewGraphNode()
  39. node.SetAttr("key", "123")
  40. node.SetAttr("kind", "bla")
  41. edge := data.NewGraphEdge()
  42. edge.SetAttr("key", "123")
  43. edge.SetAttr("kind", "myedge")
  44. edge.SetAttr(data.EdgeEnd1Key, node.Key())
  45. edge.SetAttr(data.EdgeEnd1Kind, node.Kind())
  46. edge.SetAttr(data.EdgeEnd1Role, "node1")
  47. edge.SetAttr(data.EdgeEnd1Cascading, false)
  48. edge.SetAttr(data.EdgeEnd2Key, node.Key())
  49. edge.SetAttr(data.EdgeEnd2Kind, node.Kind())
  50. edge.SetAttr(data.EdgeEnd2Role, "node2")
  51. edge.SetAttr(data.EdgeEnd2Cascading, false)
  52. trans.StoreEdge("test", edge)
  53. }
  54. return nil
  55. }
  56. func TestCascadingLastRules(t *testing.T) {
  57. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  58. gm := NewGraphManager(mgs)
  59. constructEdge := func(key string, node1 data.Node, node2 data.Node) data.Edge {
  60. edge := data.NewGraphEdge()
  61. edge.SetAttr("key", key)
  62. edge.SetAttr("kind", "myedge")
  63. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  64. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  65. edge.SetAttr(data.EdgeEnd1Role, "node1")
  66. edge.SetAttr(data.EdgeEnd1Cascading, true)
  67. edge.SetAttr(data.EdgeEnd1CascadingLast, true)
  68. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  69. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  70. edge.SetAttr(data.EdgeEnd2Role, "node2")
  71. edge.SetAttr(data.EdgeEnd2Cascading, false)
  72. edge.SetAttr(data.NodeName, "Edge1"+key)
  73. return edge
  74. }
  75. node1 := data.NewGraphNode()
  76. node1.SetAttr("key", "123")
  77. node1.SetAttr("kind", "Artist")
  78. node1.SetAttr("Name", "Artist1")
  79. gm.StoreNode("main", node1)
  80. node2 := data.NewGraphNode()
  81. node2.SetAttr("key", "456")
  82. node2.SetAttr("kind", "Song")
  83. node2.SetAttr("Name", "Song1")
  84. gm.StoreNode("main", node2)
  85. node3 := data.NewGraphNode()
  86. node3.SetAttr("key", "789")
  87. node3.SetAttr("kind", "Song")
  88. node3.SetAttr("Name", "Song2")
  89. gm.StoreNode("main", node3)
  90. gm.StoreEdge("main", constructEdge("abc1", node2, node1))
  91. gm.StoreEdge("main", constructEdge("abc2", node3, node1))
  92. gm.RemoveNode("main", "456", "Song")
  93. n, _ := gm.FetchNode("main", "123", "Artist")
  94. if n == nil {
  95. t.Error("Artist node should have not been deleted")
  96. return
  97. }
  98. gm.RemoveNode("main", "789", "Song")
  99. n, _ = gm.FetchNode("main", "123", "Artist")
  100. if n != nil {
  101. t.Error("Last removed Song node should have deleted the Artist node")
  102. return
  103. }
  104. // Now again but with multiple relationships
  105. mgs = graphstorage.NewMemoryGraphStorage("mystorage")
  106. gm = NewGraphManager(mgs)
  107. node1 = data.NewGraphNode()
  108. node1.SetAttr("key", "123")
  109. node1.SetAttr("kind", "Artist")
  110. node1.SetAttr("Name", "Artist1")
  111. gm.StoreNode("main", node1)
  112. node2 = data.NewGraphNode()
  113. node2.SetAttr("key", "456")
  114. node2.SetAttr("kind", "Song")
  115. node2.SetAttr("Name", "Song1")
  116. gm.StoreNode("main", node2)
  117. gm.StoreEdge("main", constructEdge("abc1", node2, node1))
  118. gm.StoreEdge("main", constructEdge("abc2", node2, node1))
  119. gm.RemoveNode("main", "456", "Song")
  120. n, _ = gm.FetchNode("main", "123", "Artist")
  121. if n != nil {
  122. t.Error("Artist node should have been deleted")
  123. return
  124. }
  125. }
  126. func TestRules(t *testing.T) {
  127. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  128. gm := NewGraphManager(mgs)
  129. constructEdge := func(key string, node1 data.Node, node2 data.Node) data.Edge {
  130. edge := data.NewGraphEdge()
  131. edge.SetAttr("key", key)
  132. edge.SetAttr("kind", "myedge")
  133. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  134. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  135. edge.SetAttr(data.EdgeEnd1Role, "node1")
  136. edge.SetAttr(data.EdgeEnd1Cascading, true)
  137. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  138. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  139. edge.SetAttr(data.EdgeEnd2Role, "node2")
  140. edge.SetAttr(data.EdgeEnd2Cascading, false)
  141. edge.SetAttr(data.NodeName, "Edge1"+key)
  142. return edge
  143. }
  144. constructGraph := func() {
  145. mgs = graphstorage.NewMemoryGraphStorage("mystorage")
  146. gm = NewGraphManager(mgs)
  147. node1 := data.NewGraphNode()
  148. node1.SetAttr("key", "123")
  149. node1.SetAttr("kind", "mynode")
  150. node1.SetAttr("Name", "Node1")
  151. gm.StoreNode("main", node1)
  152. node2 := data.NewGraphNode()
  153. node2.SetAttr("key", "456")
  154. node2.SetAttr("kind", "mynewnode")
  155. node2.SetAttr("Name", "Node2")
  156. gm.StoreNode("main", node2)
  157. node3 := data.NewGraphNode()
  158. node3.SetAttr("key", "789")
  159. node3.SetAttr("kind", "mynewnode")
  160. node3.SetAttr("Name", "Node3")
  161. gm.StoreNode("main", node3)
  162. node4 := data.NewGraphNode()
  163. node4.SetAttr("key", "xxx")
  164. node4.SetAttr("kind", "mynewnode")
  165. node4.SetAttr("Name", "Node4")
  166. gm.StoreNode("main", node4)
  167. gm.StoreEdge("main", constructEdge("abc2", node1, node2))
  168. gm.StoreEdge("main", constructEdge("abc3", node1, node2))
  169. gm.StoreEdge("main", constructEdge("abc1", node2, node3))
  170. gm.StoreEdge("main", constructEdge("abc0", node3, node4))
  171. gm.StoreEdge("main", constructEdge("abc99", node4, node1))
  172. }
  173. // Check rules on empty graph
  174. if len(gm.NodeKinds()) != 0 {
  175. t.Error("Unexpected node kinds result")
  176. return
  177. }
  178. if len(gm.Partitions()) != 0 {
  179. t.Error("Unexpected node kinds result")
  180. return
  181. }
  182. if len(gm.EdgeKinds()) != 0 {
  183. t.Error("Unexpected node kinds result")
  184. return
  185. }
  186. if len(gm.NodeAttrs("test")) != 0 {
  187. t.Error("Unexpected node attributes result")
  188. return
  189. }
  190. if _, err := gm.RemoveNode("test", "test", "test"); err != nil {
  191. t.Error(err)
  192. return
  193. }
  194. constructGraph()
  195. if res := fmt.Sprint(gm.NodeKinds()); res != "[mynewnode mynode]" {
  196. t.Error("Unexpected node kinds result:", res)
  197. return
  198. }
  199. if res := fmt.Sprint(gm.EdgeKinds()); res != "[myedge]" {
  200. t.Error("Unexpected edge kinds result:", res)
  201. return
  202. }
  203. if res := fmt.Sprint(gm.Partitions()); res != "[main]" {
  204. t.Error("Unexpected partitions result:", res)
  205. return
  206. }
  207. if res := fmt.Sprint(gm.NodeAttrs("mynode")); res != "[Name key kind]" {
  208. t.Error("Unexpected node attributes result:", res)
  209. return
  210. }
  211. // Test updating a node
  212. node1 := data.NewGraphNode()
  213. node1.SetAttr("key", "123")
  214. node1.SetAttr("kind", "mynode")
  215. node1.SetAttr("Name2", "Node1Name2")
  216. gm.StoreNode("main", node1)
  217. if res := fmt.Sprint(gm.NodeAttrs("mynode")); res != "[Name Name2 key kind]" {
  218. t.Error("Unexpected node attributes result:", res)
  219. return
  220. }
  221. // Test edge specs
  222. if res := fmt.Sprint(gm.NodeEdges("mynode")); res != "[node1:myedge:node2:mynewnode node2:myedge:node1:mynewnode]" {
  223. t.Error("Unexpected node attributes result:", res)
  224. return
  225. }
  226. if res := fmt.Sprint(gm.EdgeAttrs("myedge")); res != "[end1cascading end1key end1kind end1role end2cascading end2key end2kind end2role key kind name]" {
  227. t.Error("Unexpected node attributes result:", res)
  228. return
  229. }
  230. if c := gm.NodeCount("mynode"); c != 1 {
  231. t.Error("Unexpected node count:", c)
  232. return
  233. } else if c := gm.NodeCount("mynewnode"); c != 3 {
  234. t.Error("Unexpected node count:", c)
  235. return
  236. } else if c := gm.EdgeCount("myedge"); c != 5 {
  237. t.Error("Unexpected edge count:", c)
  238. return
  239. }
  240. gm.RemoveNode("main", "123", "mynode")
  241. if c := gm.NodeCount("mynode"); c != 0 {
  242. t.Error("Unexpected node count:", c)
  243. return
  244. } else if c := gm.NodeCount("mynewnode"); c != 0 {
  245. t.Error("Unexpected node count:", c)
  246. return
  247. } else if c := gm.EdgeCount("myedge"); c != 0 {
  248. t.Error("Unexpected edge count:", c)
  249. return
  250. }
  251. // Finally test a rather esotheric error case
  252. trans := NewConcurrentGraphTrans(gm)
  253. err := gm.gr.graphEvent(trans, EventNodeDeleted, "test 1", node1)
  254. if err.Error() !=
  255. "GraphError: Graph rule error (GraphError: Invalid data (Partition name "+
  256. "test 1 is not alphanumeric - can only contain [a-zA-Z0-9_]))" {
  257. t.Error(err)
  258. return
  259. }
  260. }
  261. func TestRulesTrans(t *testing.T) {
  262. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  263. gm := NewGraphManager(mgs)
  264. constructEdge := func(key string, node1 data.Node, node2 data.Node) data.Edge {
  265. edge := data.NewGraphEdge()
  266. edge.SetAttr("key", key)
  267. edge.SetAttr("kind", "myedge")
  268. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  269. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  270. edge.SetAttr(data.EdgeEnd1Role, "node1")
  271. edge.SetAttr(data.EdgeEnd1Cascading, true)
  272. edge.SetAttr(data.EdgeEnd2Key, node2.Key())
  273. edge.SetAttr(data.EdgeEnd2Kind, node2.Kind())
  274. edge.SetAttr(data.EdgeEnd2Role, "node2")
  275. edge.SetAttr(data.EdgeEnd2Cascading, false)
  276. edge.SetAttr(data.NodeName, "Edge1"+key)
  277. return edge
  278. }
  279. constructGraph := func() {
  280. mgs = graphstorage.NewMemoryGraphStorage("mystorage")
  281. gm = NewGraphManager(mgs)
  282. node1 := data.NewGraphNode()
  283. node1.SetAttr("key", "123")
  284. node1.SetAttr("kind", "mynode")
  285. node1.SetAttr("Name", "Node1")
  286. gm.StoreNode("main", node1)
  287. node2 := data.NewGraphNode()
  288. node2.SetAttr("key", "456")
  289. node2.SetAttr("kind", "mynewnode")
  290. node2.SetAttr("Name", "Node2")
  291. gm.StoreNode("main", node2)
  292. node3 := data.NewGraphNode()
  293. node3.SetAttr("key", "789")
  294. node3.SetAttr("kind", "mynewnode")
  295. node3.SetAttr("Name", "Node3")
  296. gm.StoreNode("main", node3)
  297. node4 := data.NewGraphNode()
  298. node4.SetAttr("key", "xxx")
  299. node4.SetAttr("kind", "mynewnode")
  300. node4.SetAttr("Name", "Node4")
  301. gm.StoreNode("main", node4)
  302. gm.StoreEdge("main", constructEdge("abc2", node1, node2))
  303. gm.StoreEdge("main", constructEdge("abc3", node1, node2))
  304. gm.StoreEdge("main", constructEdge("abc1", node2, node3))
  305. gm.StoreEdge("main", constructEdge("abc0", node3, node4))
  306. gm.StoreEdge("main", constructEdge("abc99", node4, node1))
  307. }
  308. // Check rules on empty graph
  309. if _, err := gm.RemoveNode("test", "test", "test"); err != nil {
  310. t.Error(err)
  311. return
  312. }
  313. constructGraph()
  314. if c := gm.NodeCount("mynode"); c != 1 {
  315. t.Error("Unexpected node count:", c)
  316. return
  317. } else if c := gm.NodeCount("mynewnode"); c != 3 {
  318. t.Error("Unexpected node count:", c)
  319. return
  320. } else if c := gm.EdgeCount("myedge"); c != 5 {
  321. t.Error("Unexpected edge count:", c)
  322. return
  323. }
  324. trans := NewConcurrentGraphTrans(gm)
  325. trans.RemoveNode("main", "123", "mynode")
  326. if err := trans.Commit(); err != nil {
  327. t.Error(err)
  328. return
  329. }
  330. if c := gm.NodeCount("mynode"); c != 0 {
  331. t.Error("Unexpected node count:", c)
  332. return
  333. } else if c := gm.NodeCount("mynewnode"); c != 0 {
  334. t.Error("Unexpected node count:", c)
  335. return
  336. } else if c := gm.EdgeCount("myedge"); c != 0 {
  337. t.Error("Unexpected edge count:", c)
  338. return
  339. }
  340. }
  341. func TestRulesErrors(t *testing.T) {
  342. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  343. gm := NewGraphManager(mgs)
  344. tr := &TestRule{false, false, false, []int{EventNodeCreated, EventNodeUpdated, EventNodeDeleted,
  345. EventEdgeCreated, EventEdgeUpdated, EventEdgeDeleted}}
  346. gm.SetGraphRule(tr)
  347. // Check that the test rule was added
  348. if rules := fmt.Sprint(gm.GraphRules()); rules !=
  349. "[system.deletenodeedges system.updatenodestats testrule]" {
  350. t.Error("unexpected graph rule list:", rules)
  351. return
  352. }
  353. node1 := data.NewGraphNode()
  354. node1.SetAttr("key", "456")
  355. node1.SetAttr("kind", "mynode")
  356. node1.SetAttr("Name", "Node1")
  357. tr.processingError = true
  358. tr.commitError = false
  359. if err := gm.StoreNode("main", node1); err.Error() !=
  360. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  361. t.Error(err)
  362. return
  363. }
  364. // Make sure the node was still added
  365. if res, err := gm.FetchNode("main", "456", "mynode"); res == nil || err != nil {
  366. t.Error("Unexpeccted result:", res, err)
  367. return
  368. }
  369. tr.processingError = false
  370. tr.commitError = true
  371. if err := gm.StoreNode("main", node1); err.Error() !=
  372. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  373. t.Error(err)
  374. return
  375. }
  376. tr.processingError = false
  377. tr.commitError = false
  378. if err := gm.StoreNode("main", node1); err != nil {
  379. t.Error(err)
  380. return
  381. }
  382. tr.processingError = true
  383. tr.commitError = false
  384. if _, err := gm.RemoveNode("main", node1.Key(), node1.Kind()); err.Error() !=
  385. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  386. t.Error(err)
  387. return
  388. }
  389. tr.processingError = false
  390. tr.commitError = false
  391. if err := gm.StoreNode("main", node1); err != nil {
  392. t.Error(err)
  393. return
  394. }
  395. tr.processingError = false
  396. tr.commitError = true
  397. if _, err := gm.RemoveNode("main", node1.Key(), node1.Kind()); err.Error() !=
  398. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  399. t.Error(err)
  400. return
  401. }
  402. tr.processingError = false
  403. tr.commitError = false
  404. if err := gm.StoreNode("main", node1); err != nil {
  405. t.Error(err)
  406. return
  407. }
  408. tr.processingError = true
  409. tr.commitError = false
  410. edge := data.NewGraphEdge()
  411. edge.SetAttr("key", "123")
  412. edge.SetAttr("kind", "myedge")
  413. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  414. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  415. edge.SetAttr(data.EdgeEnd1Role, "node1")
  416. edge.SetAttr(data.EdgeEnd1Cascading, false)
  417. edge.SetAttr(data.EdgeEnd2Key, node1.Key())
  418. edge.SetAttr(data.EdgeEnd2Kind, node1.Kind())
  419. edge.SetAttr(data.EdgeEnd2Role, "node2")
  420. edge.SetAttr(data.EdgeEnd2Cascading, false)
  421. if err := gm.StoreEdge("main", edge); err.Error() !=
  422. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  423. t.Error(err)
  424. return
  425. }
  426. tr.processingError = false
  427. tr.commitError = true
  428. if err := gm.StoreEdge("main", edge); err.Error() !=
  429. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  430. t.Error(err)
  431. return
  432. }
  433. tr.processingError = false
  434. tr.commitError = false
  435. if err := gm.StoreEdge("main", edge); err != nil {
  436. t.Error(err)
  437. return
  438. }
  439. tr.processingError = false
  440. tr.commitError = true
  441. if _, err := gm.RemoveEdge("main", edge.Key(), edge.Kind()); err.Error() !=
  442. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  443. t.Error(err)
  444. return
  445. }
  446. tr.processingError = false
  447. tr.commitError = false
  448. if err := gm.StoreEdge("main", edge); err != nil {
  449. t.Error(err)
  450. return
  451. }
  452. tr.processingError = true
  453. tr.commitError = false
  454. if _, err := gm.RemoveEdge("main", edge.Key(), edge.Kind()); err.Error() !=
  455. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  456. t.Error(err)
  457. return
  458. }
  459. tr.processingError = false
  460. tr.commitError = false
  461. if err := gm.StoreEdge("main", edge); err != nil {
  462. t.Error(err)
  463. return
  464. }
  465. // Test transaction errors
  466. trans := NewConcurrentGraphTrans(gm)
  467. tr.processingError = true
  468. tr.commitError = false
  469. trans.StoreNode("main", node1)
  470. if err := trans.Commit(); err.Error() !=
  471. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  472. t.Error(err)
  473. return
  474. }
  475. tr.processingError = false
  476. tr.commitError = true
  477. trans.StoreNode("main", node1)
  478. if err := trans.Commit(); err.Error() !=
  479. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  480. t.Error(err)
  481. return
  482. }
  483. tr.processingError = true
  484. tr.commitError = false
  485. trans.RemoveNode("main", node1.Key(), node1.Kind())
  486. if err := trans.Commit(); err.Error() !=
  487. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  488. t.Error(err)
  489. return
  490. }
  491. tr.processingError = false
  492. tr.commitError = false
  493. if err := gm.StoreNode("main", node1); err != nil {
  494. t.Error(err)
  495. return
  496. }
  497. tr.processingError = false
  498. tr.commitError = true
  499. trans.RemoveNode("main", node1.Key(), node1.Kind())
  500. if err := trans.Commit(); err.Error() !=
  501. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  502. t.Error(err)
  503. return
  504. }
  505. tr.processingError = false
  506. tr.commitError = false
  507. if err := gm.StoreNode("main", node1); err != nil {
  508. t.Error(err)
  509. return
  510. }
  511. tr.processingError = true
  512. tr.commitError = false
  513. trans.StoreEdge("main", edge)
  514. if err := trans.Commit(); err.Error() !=
  515. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  516. t.Error(err)
  517. return
  518. }
  519. tr.processingError = false
  520. tr.commitError = true
  521. trans.StoreEdge("main", edge)
  522. if err := trans.Commit(); err.Error() !=
  523. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  524. t.Error(err)
  525. return
  526. }
  527. tr.processingError = false
  528. tr.commitError = false
  529. if err := gm.StoreEdge("main", edge); err != nil {
  530. t.Error(err)
  531. return
  532. }
  533. tr.processingError = false
  534. tr.commitError = true
  535. trans.RemoveEdge("main", edge.Key(), edge.Kind())
  536. if err := trans.Commit(); err.Error() !=
  537. "GraphError: Invalid data (Can't store edge to non-existing node kind: bla)" {
  538. t.Error(err)
  539. return
  540. }
  541. tr.processingError = false
  542. tr.commitError = false
  543. if err := gm.StoreEdge("main", edge); err != nil {
  544. t.Error(err)
  545. return
  546. }
  547. tr.processingError = true
  548. tr.commitError = false
  549. trans.RemoveEdge("main", edge.Key(), edge.Kind())
  550. if err := trans.Commit(); err.Error() !=
  551. "GraphError: Graph rule error (GraphError: Failed to access graph storage component (Test error))" {
  552. t.Error(err)
  553. return
  554. }
  555. }
  556. func TestRulesErrorsPreChange(t *testing.T) {
  557. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  558. gm := NewGraphManager(mgs)
  559. tr := &TestRule{false, false, false, []int{EventNodeStore, EventNodeUpdate, EventNodeDelete,
  560. EventEdgeStore, EventEdgeDelete}}
  561. gm.SetGraphRule(tr)
  562. // Check that the test rule was added
  563. if rules := fmt.Sprint(gm.GraphRules()); rules !=
  564. "[system.deletenodeedges system.updatenodestats testrule]" {
  565. t.Error("unexpected graph rule list:", rules)
  566. return
  567. }
  568. // Test that we can stop the storage of a node with custom processing
  569. node1 := data.NewGraphNode()
  570. node1.SetAttr("key", "456")
  571. node1.SetAttr("kind", "mynode")
  572. node1.SetAttr("Name", "Node1")
  573. tr.handledError = true
  574. if err := gm.StoreNode("main", node1); err != nil {
  575. t.Error(err)
  576. return
  577. }
  578. // Make sure the node was not added
  579. if res, err := gm.FetchNode("main", "456", "mynode"); res != nil || err != nil {
  580. t.Error("Unexpeccted result:", res, err)
  581. return
  582. }
  583. if err := gm.UpdateNode("main", node1); err != nil {
  584. t.Error(err)
  585. return
  586. }
  587. if _, err := gm.RemoveNode("main", node1.Key(), node1.Kind()); err != nil {
  588. t.Error(err)
  589. return
  590. }
  591. edge := data.NewGraphEdge()
  592. edge.SetAttr("key", "123")
  593. edge.SetAttr("kind", "myedge")
  594. edge.SetAttr(data.EdgeEnd1Key, node1.Key())
  595. edge.SetAttr(data.EdgeEnd1Kind, node1.Kind())
  596. edge.SetAttr(data.EdgeEnd1Role, "node1")
  597. edge.SetAttr(data.EdgeEnd1Cascading, false)
  598. edge.SetAttr(data.EdgeEnd2Key, node1.Key())
  599. edge.SetAttr(data.EdgeEnd2Kind, node1.Kind())
  600. edge.SetAttr(data.EdgeEnd2Role, "node2")
  601. edge.SetAttr(data.EdgeEnd2Cascading, false)
  602. if err := gm.StoreEdge("main", edge); err != nil {
  603. t.Error(err)
  604. return
  605. }
  606. if _, err := gm.RemoveEdge("main", edge.Key(), edge.Kind()); err != nil {
  607. t.Error(err)
  608. return
  609. }
  610. }