eql_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 v1
  11. import (
  12. "bytes"
  13. "encoding/json"
  14. "testing"
  15. )
  16. func TestEql(t *testing.T) {
  17. queryURL := "http://localhost" + TESTPORT + EndpointEql
  18. // Test error messages
  19. _, _, res := sendTestRequest(queryURL, "POST", nil)
  20. if res != "Could not decode request body: EOF" {
  21. t.Error("Unexpected response:", res)
  22. return
  23. }
  24. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  25. {
  26. "foo" : "bar"
  27. }
  28. `[1:]))
  29. if res != "Need either a query or an ast parameter" {
  30. t.Error("Unexpected response:", res)
  31. return
  32. }
  33. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  34. {
  35. "query" : "get =bla where foo = 'bar'"
  36. }
  37. `[1:]))
  38. if res != "Parse error in request: Lexical error (Invalid node kind '=bla' - can only contain [a-zA-Z0-9_]) (Line:1 Pos:5)" {
  39. t.Error("Unexpected response:", res)
  40. return
  41. }
  42. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  43. {
  44. "ast" : "foobar"
  45. }
  46. `[1:]))
  47. if res != "Plain AST object expected as 'ast' value" {
  48. t.Error("Unexpected response:", res)
  49. return
  50. }
  51. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  52. {
  53. "ast" : {
  54. "foo" : "bar"
  55. }
  56. }
  57. `[1:]))
  58. if res != "Found plain ast node without a name: map[foo:bar]" {
  59. t.Error("Unexpected response:", res)
  60. return
  61. }
  62. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  63. {
  64. "ast" : {
  65. "name" : "foo",
  66. "value" : "bar"
  67. }
  68. }
  69. `[1:]))
  70. if res != "Could not find template for foo (tempkey: foo)" {
  71. t.Error("Unexpected response:", res)
  72. return
  73. }
  74. // Test parsing and pretty printing
  75. _, _, res = sendTestRequest(queryURL+"main/n", "POST", []byte(`
  76. {
  77. "query": "get bla where foo = bar \nwith\n ordering(ascending name)"
  78. }
  79. `[1:]))
  80. if res != `
  81. {
  82. "ast": {
  83. "children": [
  84. {
  85. "name": "value",
  86. "value": "bla"
  87. },
  88. {
  89. "children": [
  90. {
  91. "children": [
  92. {
  93. "name": "value",
  94. "value": "foo"
  95. },
  96. {
  97. "name": "value",
  98. "value": "bar"
  99. }
  100. ],
  101. "name": "=",
  102. "value": "="
  103. }
  104. ],
  105. "name": "where",
  106. "value": "where"
  107. },
  108. {
  109. "children": [
  110. {
  111. "children": [
  112. {
  113. "children": [
  114. {
  115. "name": "value",
  116. "value": "name"
  117. }
  118. ],
  119. "name": "asc",
  120. "value": "ascending"
  121. }
  122. ],
  123. "name": "ordering",
  124. "value": "ordering"
  125. }
  126. ],
  127. "name": "with",
  128. "value": "with"
  129. }
  130. ],
  131. "name": "get",
  132. "value": "get"
  133. }
  134. }`[1:] {
  135. t.Error("Unexpected response:", res)
  136. return
  137. }
  138. var astInput map[string]interface{}
  139. var astText bytes.Buffer
  140. json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
  141. json.NewEncoder(&astText).Encode(astInput)
  142. _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
  143. if res != `
  144. {
  145. "query": "get bla where foo = bar \nwith\n ordering(ascending name)"
  146. }`[1:] {
  147. t.Error("Unexpected result:", res)
  148. return
  149. }
  150. }
  151. func TestEqlSpecial(t *testing.T) {
  152. queryURL := "http://localhost" + TESTPORT + EndpointEql
  153. // And and or AST nodes might have more than 2 children
  154. res := `
  155. {
  156. "ast": {
  157. "children": [
  158. {
  159. "name": "value",
  160. "value": "bla"
  161. },
  162. {
  163. "children": [
  164. {
  165. "name": "and",
  166. "value": "and",
  167. "children": [
  168. {
  169. "children": [
  170. {
  171. "name": "value",
  172. "value": "foo1"
  173. },
  174. {
  175. "name": "value",
  176. "value": "bar1"
  177. }
  178. ],
  179. "name": "=",
  180. "value": "="
  181. }, {
  182. "children": [
  183. {
  184. "name": "value",
  185. "value": "foo2"
  186. },
  187. {
  188. "name": "value",
  189. "value": "bar2"
  190. }
  191. ],
  192. "name": "=",
  193. "value": "="
  194. }, {
  195. "children": [
  196. {
  197. "name": "value",
  198. "value": "foo3"
  199. },
  200. {
  201. "name": "value",
  202. "value": "bar3"
  203. }
  204. ],
  205. "name": "=",
  206. "value": "="
  207. }
  208. ]
  209. }
  210. ],
  211. "name": "where",
  212. "value": "where"
  213. }
  214. ],
  215. "name": "get",
  216. "value": "get"
  217. }
  218. }`[1:]
  219. var astInput map[string]interface{}
  220. var astText bytes.Buffer
  221. err := json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
  222. if err != nil {
  223. t.Error(err)
  224. return
  225. }
  226. json.NewEncoder(&astText).Encode(astInput)
  227. _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
  228. if res != `
  229. {
  230. "query": "get bla where foo1 = bar1 and foo2 = bar2 and foo3 = bar3"
  231. }`[1:] {
  232. t.Error("Unexpected result:", res)
  233. return
  234. }
  235. res = `
  236. {
  237. "ast": {
  238. "children": [
  239. {
  240. "name": "value",
  241. "value": "bla"
  242. },
  243. {
  244. "children": [
  245. {
  246. "name": "or",
  247. "value": "or",
  248. "children": [
  249. {
  250. "children": [
  251. {
  252. "name": "value",
  253. "value": "foo1"
  254. },
  255. {
  256. "name": "value",
  257. "value": "bar1"
  258. }
  259. ],
  260. "name": "=",
  261. "value": "="
  262. }, {
  263. "children": [
  264. {
  265. "name": "value",
  266. "value": "foo2"
  267. },
  268. {
  269. "name": "value",
  270. "value": "bar2"
  271. }
  272. ],
  273. "name": "=",
  274. "value": "="
  275. }, {
  276. "children": [
  277. {
  278. "name": "value",
  279. "value": "foo3"
  280. },
  281. {
  282. "name": "value",
  283. "value": "bar3"
  284. }
  285. ],
  286. "name": "=",
  287. "value": "="
  288. }
  289. ]
  290. }
  291. ],
  292. "name": "where",
  293. "value": "where"
  294. }
  295. ],
  296. "name": "get",
  297. "value": "get"
  298. }
  299. }`[1:]
  300. err = json.NewDecoder(bytes.NewBufferString(res)).Decode(&astInput)
  301. if err != nil {
  302. t.Error(err)
  303. return
  304. }
  305. astText = bytes.Buffer{}
  306. json.NewEncoder(&astText).Encode(astInput)
  307. _, _, res = sendTestRequest(queryURL, "POST", astText.Bytes())
  308. if res != `
  309. {
  310. "query": "get bla where foo1 = bar1 or foo2 = bar2 or foo3 = bar3"
  311. }`[1:] {
  312. t.Error("Unexpected result:", res)
  313. return
  314. }
  315. }