import_export_test.go 9.2 KB

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