import_export_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. "bytes"
  13. "io"
  14. "strings"
  15. "testing"
  16. "devt.de/krotik/eliasdb/graph/data"
  17. "devt.de/krotik/eliasdb/graph/graphstorage"
  18. "devt.de/krotik/eliasdb/storage"
  19. )
  20. func TestImportExportError(t *testing.T) {
  21. var res bytes.Buffer
  22. // Create a memory only storage
  23. gs := graphstorage.NewMemoryGraphStorage("test")
  24. gm := NewGraphManager(gs)
  25. // Test incomplete import data
  26. err := ImportPartition(bytes.NewBufferString(`
  27. {
  28. "nodes" : [
  29. {
  30. "key": "1",
  31. "kind": "X",
  32. `), "main", gm)
  33. if err == nil || err.Error() != "Could not decode file content as object with list of nodes and edges: unexpected EOF" {
  34. t.Error("Unexpected result:", err)
  35. return
  36. }
  37. // Export an empty graph
  38. err = ExportPartition(&res, "aaa", gm)
  39. if err != nil || res.String() != `{
  40. "nodes" : [
  41. ],
  42. "edges" : [
  43. ]
  44. }` {
  45. t.Error("Unexpected result:", res.String(), err)
  46. return
  47. }
  48. // Try exporting nodes with unexportable attibutes
  49. err = gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  50. "key": "123",
  51. "kind": "bla",
  52. "test": data.NewGraphNode,
  53. }))
  54. if err != nil {
  55. t.Error(err)
  56. return
  57. }
  58. res.Reset()
  59. err = ExportPartition(&res, "main", gm)
  60. sortRes := SortDump(res.String())
  61. if err != nil || sortRes != `{
  62. "edges": [],
  63. "nodes": [
  64. {
  65. "key": "123",
  66. "kind": "bla",
  67. "test": null
  68. }
  69. ]
  70. }` {
  71. t.Error("Unexpected result:", sortRes, err)
  72. return
  73. }
  74. // Error when reading a node
  75. msm := gs.StorageManager("main"+"bla"+StorageSuffixNodes, false).(*storage.MemoryStorageManager)
  76. msm.AccessMap[1] = storage.AccessCacheAndFetchSeriousError
  77. res.Reset()
  78. err = ExportPartition(&res, "main", gm)
  79. if !strings.HasPrefix(err.Error(), "GraphError: Failed to access graph storage component") {
  80. t.Error("Unexpected graph error:", err)
  81. return
  82. }
  83. delete(msm.AccessMap, 1)
  84. err = gm.StoreNode("main", data.NewGraphNodeFromMap(map[string]interface{}{
  85. "key": "456",
  86. "kind": "bla",
  87. "test": data.NewGraphNode,
  88. }))
  89. msm = gs.StorageManager("main"+"bla"+StorageSuffixNodes, false).(*storage.MemoryStorageManager)
  90. msm.AccessMap[6] = storage.AccessCacheAndFetchSeriousError
  91. res.Reset()
  92. err = ExportPartition(&res, "main", gm)
  93. if !strings.HasPrefix(err.Error(), "GraphError: Could not read graph information") {
  94. t.Error("Unexpected graph error:", err)
  95. return
  96. }
  97. delete(msm.AccessMap, 6)
  98. msm.AccessMap[5] = storage.AccessCacheAndFetchSeriousError
  99. res.Reset()
  100. err = ExportPartition(&res, "main", gm)
  101. if !strings.HasPrefix(err.Error(), "GraphError: Could not read graph information") {
  102. t.Error("Unexpected graph error:", err)
  103. return
  104. }
  105. delete(msm.AccessMap, 5)
  106. gm.StoreEdge("main", data.NewGraphEdgeFromNode(data.NewGraphNodeFromMap(map[string]interface{}{
  107. "end1cascading": false,
  108. "end1key": "123",
  109. "end1kind": "bla",
  110. "end1role": "node",
  111. "end2cascading": false,
  112. "end2key": "456",
  113. "end2kind": "bla",
  114. "end2role": "node",
  115. "key": "3",
  116. "kind": "xxx",
  117. })))
  118. // Traverse to relationship should fail
  119. msm.AccessMap[7] = storage.AccessCacheAndFetchSeriousError
  120. res.Reset()
  121. err = ExportPartition(&res, "main", gm)
  122. if !strings.HasPrefix(err.Error(), "GraphError: Could not read graph information") {
  123. t.Error("Unexpected graph error:", err)
  124. return
  125. }
  126. delete(msm.AccessMap, 7)
  127. // Lookup of relationship should fail
  128. msm = gs.StorageManager("main"+"xxx"+StorageSuffixEdges, false).(*storage.MemoryStorageManager)
  129. msm.AccessMap[1] = storage.AccessCacheAndFetchSeriousError
  130. res.Reset()
  131. err = ExportPartition(&res, "main", gm)
  132. if !strings.HasPrefix(err.Error(), "GraphError: Failed to access graph storage component") {
  133. t.Error("Unexpected graph error:", err)
  134. return
  135. }
  136. delete(msm.AccessMap, 1)
  137. // Test invalid import data
  138. err = ImportPartition(bytes.NewBufferString(`{
  139. "nodes" : [
  140. {
  141. "key": "1",
  142. "kind": "X"
  143. },
  144. {
  145. "key": "2"
  146. }
  147. ],
  148. "edges" : [
  149. {
  150. "end1cascading": false,
  151. "end1key": "1",
  152. "end1kind": "X",
  153. "end1role": "node",
  154. "end2cascading": false,
  155. "end2key": "2",
  156. "end2kind": "Y",
  157. "end2role": "node",
  158. "key": "4",
  159. "kind": "A"
  160. }
  161. ]
  162. }`), "main", gm)
  163. if err == nil || err.Error() != "GraphError: Invalid data (Node is missing a kind value)" {
  164. t.Error("Unexpected result:", err)
  165. return
  166. }
  167. err = ImportPartition(bytes.NewBufferString(`{
  168. "nodes" : [
  169. {
  170. "key": "1",
  171. "kind": "X"
  172. },
  173. {
  174. "key": "2",
  175. "kind": "Y"
  176. }
  177. ],
  178. "edges" : [
  179. {
  180. "end1cascading": false,
  181. "end1key": "1",
  182. "end1kind": "X",
  183. "end1role": "node",
  184. "end2key": "2",
  185. "end2kind": "Y",
  186. "end2role": "node",
  187. "key": "4",
  188. "kind": "A"
  189. }
  190. ]
  191. }`), "main", gm)
  192. if err == nil || err.Error() != "GraphError: Invalid data (Edge is missing a cascading value for end2)" {
  193. t.Error("Unexpected result:", err)
  194. return
  195. }
  196. // Do actual import and exports
  197. gs = graphstorage.NewMemoryGraphStorage("test")
  198. gm = NewGraphManager(gs)
  199. err = ImportPartition(bytes.NewBufferString(`{
  200. "nodes" : [
  201. {
  202. "key": "1",
  203. "kind": "X"
  204. },
  205. {
  206. "key": "2",
  207. "kind": "Y"
  208. }
  209. ],
  210. "edges" : [
  211. {
  212. "end1cascading": false,
  213. "end1key": "1",
  214. "end1kind": "X",
  215. "end1role": "node",
  216. "end2cascading": false,
  217. "end2key": "2",
  218. "end2kind": "Y",
  219. "end2role": "node",
  220. "key": "4",
  221. "kind": "A"
  222. },
  223. {
  224. "end1cascading": false,
  225. "end1key": "1",
  226. "end1kind": "X",
  227. "end1role": "node",
  228. "end2cascading": false,
  229. "end2key": "2",
  230. "end2kind": "Y",
  231. "end2role": "node",
  232. "key": "5",
  233. "kind": "B"
  234. }
  235. ]
  236. }`), "main", gm)
  237. if err != nil {
  238. t.Error(err)
  239. return
  240. }
  241. err = ImportPartition(bytes.NewBufferString(`{
  242. "nodes" : [
  243. {
  244. "key": "1",
  245. "kind": "Xfoo"
  246. },
  247. {
  248. "key": "2",
  249. "kind": "Yfoo"
  250. }
  251. ],
  252. "edges" : [
  253. {
  254. "end1cascading": false,
  255. "end1key": "1",
  256. "end1kind": "Xfoo",
  257. "end1role": "node",
  258. "end2cascading": false,
  259. "end2key": "2",
  260. "end2kind": "Yfoo",
  261. "end2role": "node",
  262. "key": "4",
  263. "kind": "Afoo"
  264. },
  265. {
  266. "end1cascading": false,
  267. "end1key": "1",
  268. "end1kind": "Xfoo",
  269. "end1role": "node",
  270. "end2cascading": false,
  271. "end2key": "2",
  272. "end2kind": "Yfoo",
  273. "end2role": "node",
  274. "key": "5",
  275. "kind": "Bfoo"
  276. }
  277. ]
  278. }`), "foo", gm)
  279. if err != nil {
  280. t.Error(err)
  281. return
  282. }
  283. res.Reset()
  284. err = ExportPartition(&res, "main", gm)
  285. sortRes = SortDump(res.String())
  286. if err != nil || sortRes != `{
  287. "edges": [
  288. {
  289. "end1cascading": false,
  290. "end1key": "1",
  291. "end1kind": "X",
  292. "end1role": "node",
  293. "end2cascading": false,
  294. "end2key": "2",
  295. "end2kind": "Y",
  296. "end2role": "node",
  297. "key": "4",
  298. "kind": "A"
  299. },
  300. {
  301. "end1cascading": false,
  302. "end1key": "1",
  303. "end1kind": "X",
  304. "end1role": "node",
  305. "end2cascading": false,
  306. "end2key": "2",
  307. "end2kind": "Y",
  308. "end2role": "node",
  309. "key": "5",
  310. "kind": "B"
  311. }
  312. ],
  313. "nodes": [
  314. {
  315. "key": "1",
  316. "kind": "X"
  317. },
  318. {
  319. "key": "2",
  320. "kind": "Y"
  321. }
  322. ]
  323. }` {
  324. t.Error("Unexpected result:", sortRes, err)
  325. return
  326. }
  327. // Do an import with the export data and see that nothing changes
  328. err = ImportPartition(bytes.NewBufferString(sortRes), "main", gm)
  329. if err != nil {
  330. t.Error(err)
  331. return
  332. }
  333. res.Reset()
  334. err = ExportPartition(&res, "main", gm)
  335. if err != nil {
  336. t.Error(err)
  337. return
  338. }
  339. sortRes2 := SortDump(res.String())
  340. if sortRes2 != sortRes {
  341. t.Error("Export data differs from import data:", sortRes2)
  342. return
  343. }
  344. }
  345. type testFactory struct {
  346. readers []string
  347. buf map[string]*bytes.Buffer
  348. }
  349. func (tf *testFactory) CreateWriter(name string) (io.Writer, error) {
  350. var b bytes.Buffer
  351. tf.readers = append(tf.readers, name)
  352. tf.buf[name] = &b
  353. return &b, nil
  354. }
  355. func (tf *testFactory) Readers() ([]string, error) {
  356. return tf.readers, nil
  357. }
  358. func (tf *testFactory) CreateReader(name string) (io.Reader, error) {
  359. return bytes.NewBuffer(tf.buf[name].Bytes()), nil
  360. }
  361. func TestScaleExport(t *testing.T) {
  362. gm, _ := songGraph()
  363. tf := &testFactory{make([]string, 0), make(map[string]*bytes.Buffer)}
  364. if err := LargeScaleExportPartition(tf, gm); err != nil {
  365. t.Error(err)
  366. return
  367. }
  368. var out1 bytes.Buffer
  369. if err := ExportPartition(&out1, "main", gm); err != nil {
  370. t.Error(err)
  371. return
  372. }
  373. res1 := SortDump(out1.String())
  374. mgs2 := graphstorage.NewMemoryGraphStorage("mystorage2")
  375. gm2 := NewGraphManager(mgs2)
  376. if err := LargeScaleImportPartition(tf, gm2); err != nil {
  377. t.Error(err)
  378. return
  379. }
  380. var out2 bytes.Buffer
  381. if err := ExportPartition(&out2, "main", gm2); err != nil {
  382. t.Error(err)
  383. return
  384. }
  385. res2 := SortDump(out2.String())
  386. if res1 != res2 {
  387. t.Error("Unexpected result - results of import/export are different")
  388. return
  389. }
  390. // Test failures
  391. gm, gs := songGraph()
  392. tfImport := tf
  393. tf = &testFactory{make([]string, 0), make(map[string]*bytes.Buffer)}
  394. msm := gs.StorageManager("main"+"Song"+StorageSuffixNodes, false).(*storage.MemoryStorageManager)
  395. msm.AccessMap[5] = storage.AccessCacheAndFetchSeriousError
  396. if err := LargeScaleExportPartition(tf, gm); err == nil {
  397. t.Error("Error was expected")
  398. return
  399. }
  400. delete(msm.AccessMap, 5)
  401. msm = gs.StorageManager("main"+"Wrote"+StorageSuffixEdges, false).(*storage.MemoryStorageManager)
  402. msm.AccessMap[5] = storage.AccessCacheAndFetchSeriousError
  403. if err := LargeScaleExportPartition(tf, gm); err == nil {
  404. t.Error("Error was expected")
  405. return
  406. }
  407. delete(msm.AccessMap, 5)
  408. mgs2 = graphstorage.NewMemoryGraphStorage("mystorage2")
  409. gm2 = NewGraphManager(mgs2)
  410. msm = mgs2.StorageManager("main"+"Song"+StorageSuffixNodes, true).(*storage.MemoryStorageManager)
  411. msm.AccessMap[2] = storage.AccessInsertError
  412. if err := LargeScaleImportPartition(tfImport, gm2); err == nil {
  413. t.Error("Error was expected")
  414. return
  415. }
  416. delete(msm.AccessMap, 2)
  417. }