regression_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 eql
  11. import (
  12. "bytes"
  13. "fmt"
  14. "testing"
  15. "devt.de/krotik/eliasdb/graph"
  16. "devt.de/krotik/eliasdb/graph/graphstorage"
  17. )
  18. /*
  19. runTestQuery runs a query against a given graph manager and checks against an expected result.
  20. */
  21. func runTestQuery(gm *graph.Manager, query string, expectedResult string) error {
  22. sr, err := RunQuery("", "main", query, gm)
  23. if err != nil {
  24. return err
  25. }
  26. sr.(*queryResult).SearchResult.StableSort()
  27. if sr.String() != expectedResult {
  28. return fmt.Errorf("Unexpected result: %s", sr)
  29. }
  30. return nil
  31. }
  32. /*
  33. Issue 43 Query not returning expected result
  34. (https://github.com/krotik/eliasdb/issues/43)
  35. I have a graph where 4 nodes (T1 through T4) are connected like this:
  36. [T1] <-(E2)- [T2] <-(E3)- [T3] -(E4)-> [T4]
  37. The goal of my query is:
  38. Given a T1, find the T4 items.
  39. The kind for T1, T2 and T3 are known but the type for T4 is not. For some reason the follwing query does not work (traversing from the left to right: T1 to T2 to T3 to T4).
  40. get T1 where key = "%s"
  41. traverse in:E2:out:T2
  42. traverse in:E3:out:T3
  43. traverse out:E4:in:
  44. end
  45. end
  46. end
  47. */
  48. var issue43data = `
  49. {
  50. "nodes" : [
  51. {
  52. "key" : "t1",
  53. "kind" : "T1",
  54. "name" : "T1 node"
  55. },
  56. {
  57. "key" : "t2",
  58. "kind" : "T2",
  59. "name" : "T2 node"
  60. },
  61. {
  62. "key" : "t3",
  63. "kind" : "T3",
  64. "name" : "T3 node"
  65. },
  66. {
  67. "key" : "t4",
  68. "kind" : "T4",
  69. "name" : "T4 node"
  70. }
  71. ],
  72. "edges" : [
  73. {
  74. "key" : "e2",
  75. "kind" : "E2",
  76. "end1key" : "t1",
  77. "end1cascading": false,
  78. "end1kind": "T1",
  79. "end1role": "in",
  80. "end2key" : "t2",
  81. "end2cascading": false,
  82. "end2kind": "T2",
  83. "end2role": "out"
  84. },
  85. {
  86. "key" : "e3",
  87. "kind" : "E3",
  88. "end1key" : "t2",
  89. "end1cascading": false,
  90. "end1kind": "T2",
  91. "end1role": "in",
  92. "end2cascading": false,
  93. "end2key" : "t3",
  94. "end2cascading": false,
  95. "end2kind": "T3",
  96. "end2role": "out"
  97. },
  98. {
  99. "key" : "e4",
  100. "kind" : "E4",
  101. "end1key" : "t3",
  102. "end1cascading": false,
  103. "end1kind": "T3",
  104. "end1role": "out",
  105. "end2key" : "t4",
  106. "end2cascading": false,
  107. "end2kind": "T4",
  108. "end2role": "in"
  109. }
  110. ]
  111. }
  112. `
  113. func TestIssue43Regression(t *testing.T) {
  114. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  115. gm := graph.NewGraphManager(mgs)
  116. err := graph.ImportPartition(bytes.NewBufferString(issue43data), "main", gm)
  117. if err != nil {
  118. t.Error(err)
  119. return
  120. }
  121. if err := runTestQuery(gm, `
  122. get T1 where key = "t1"
  123. traverse in:E2:out:T2
  124. traverse in:E3:out:T3
  125. traverse out:E4:in:
  126. end
  127. end
  128. end
  129. `, `
  130. Labels: T1 Key, T1 Name, T2 Key, T2 Name, T3 Key, T3 Name, Key, Kind, Name
  131. Format: auto, auto, auto, auto, auto, auto, auto, auto, auto
  132. Data: 1:n:key, 1:n:name, 2:n:key, 2:n:name, 3:n:key, 3:n:name, 4:n:key, 4:n:kind, 4:n:name
  133. t1, T1 node, t2, T2 node, t3, T3 node, t4, T4, T4 node
  134. `[1:]); err != nil {
  135. t.Error(err)
  136. return
  137. }
  138. }