query_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. "testing"
  13. )
  14. func TestQueryPagination(t *testing.T) {
  15. queryURL := "http://localhost" + TESTPORT + EndpointQuery
  16. st, _, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=p&limit=2", "GET", nil)
  17. if st != "400 Bad Request" || res != "Invalid parameter value: offset should be a positive integer number" {
  18. t.Error("Unexpected response:", st, res)
  19. return
  20. }
  21. st, _, res = sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=p", "GET", nil)
  22. if st != "400 Bad Request" || res != "Invalid parameter value: limit should be a positive integer number" {
  23. t.Error("Unexpected response:", st, res)
  24. return
  25. }
  26. st, h, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=3", "GET", nil)
  27. if st != "200 OK" || res != `
  28. {
  29. "header": {
  30. "data": [
  31. "1:n:key",
  32. "1:n:name",
  33. "1:n:ranking"
  34. ],
  35. "format": [
  36. "auto",
  37. "auto",
  38. "auto"
  39. ],
  40. "labels": [
  41. "Song Key",
  42. "Song Name",
  43. "Ranking"
  44. ],
  45. "primary_kind": "Song"
  46. },
  47. "rows": [
  48. [
  49. "Aria3",
  50. "Aria3",
  51. 4
  52. ],
  53. [
  54. "Aria4",
  55. "Aria4",
  56. 18
  57. ],
  58. [
  59. "DeadSong2",
  60. "DeadSong2",
  61. 6
  62. ]
  63. ],
  64. "selections": [
  65. false,
  66. false,
  67. false
  68. ],
  69. "sources": [
  70. [
  71. "n:Song:Aria3",
  72. "n:Song:Aria3",
  73. "n:Song:Aria3"
  74. ],
  75. [
  76. "n:Song:Aria4",
  77. "n:Song:Aria4",
  78. "n:Song:Aria4"
  79. ],
  80. [
  81. "n:Song:DeadSong2",
  82. "n:Song:DeadSong2",
  83. "n:Song:DeadSong2"
  84. ]
  85. ],
  86. "total_selections": 0
  87. }`[1:] {
  88. t.Error("Unexpected response:", st, res)
  89. return
  90. }
  91. // Check header values
  92. if tc := h.Get(HTTPHeaderTotalCount); tc != "9" {
  93. t.Error("Unexpected total count:", tc)
  94. return
  95. }
  96. rid := h.Get(HTTPHeaderCacheID)
  97. if _, ok := ResultCache.Get(rid); !ok {
  98. t.Error("Given result id should be in the cache")
  99. return
  100. }
  101. st, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=5&limit=0", "GET", nil)
  102. if st != "200 OK" || res != `
  103. {
  104. "header": {
  105. "data": [
  106. "1:n:key",
  107. "1:n:name",
  108. "1:n:ranking"
  109. ],
  110. "format": [
  111. "auto",
  112. "auto",
  113. "auto"
  114. ],
  115. "labels": [
  116. "Song Key",
  117. "Song Name",
  118. "Ranking"
  119. ],
  120. "primary_kind": "Song"
  121. },
  122. "rows": [],
  123. "selections": [],
  124. "sources": [],
  125. "total_selections": 0
  126. }`[1:] {
  127. t.Error("Unexpected response:", st, res)
  128. return
  129. }
  130. st, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=5&limit=1", "GET", nil)
  131. if st != "200 OK" || res != `
  132. {
  133. "header": {
  134. "data": [
  135. "1:n:key",
  136. "1:n:name",
  137. "1:n:ranking"
  138. ],
  139. "format": [
  140. "auto",
  141. "auto",
  142. "auto"
  143. ],
  144. "labels": [
  145. "Song Key",
  146. "Song Name",
  147. "Ranking"
  148. ],
  149. "primary_kind": "Song"
  150. },
  151. "rows": [
  152. [
  153. "FightSong4",
  154. "FightSong4",
  155. 3
  156. ]
  157. ],
  158. "selections": [
  159. false
  160. ],
  161. "sources": [
  162. [
  163. "n:Song:FightSong4",
  164. "n:Song:FightSong4",
  165. "n:Song:FightSong4"
  166. ]
  167. ],
  168. "total_selections": 0
  169. }`[1:] {
  170. t.Error("Unexpected response:", st, res)
  171. return
  172. }
  173. st, _, res = sendTestRequest(queryURL+"//main?rid="+rid, "GET", nil)
  174. if st != "200 OK" || res != `
  175. {
  176. "header": {
  177. "data": [
  178. "1:n:key",
  179. "1:n:name",
  180. "1:n:ranking"
  181. ],
  182. "format": [
  183. "auto",
  184. "auto",
  185. "auto"
  186. ],
  187. "labels": [
  188. "Song Key",
  189. "Song Name",
  190. "Ranking"
  191. ],
  192. "primary_kind": "Song"
  193. },
  194. "rows": [
  195. [
  196. "Aria1",
  197. "Aria1",
  198. 8
  199. ],
  200. [
  201. "Aria2",
  202. "Aria2",
  203. 2
  204. ],
  205. [
  206. "Aria3",
  207. "Aria3",
  208. 4
  209. ],
  210. [
  211. "Aria4",
  212. "Aria4",
  213. 18
  214. ],
  215. [
  216. "DeadSong2",
  217. "DeadSong2",
  218. 6
  219. ],
  220. [
  221. "FightSong4",
  222. "FightSong4",
  223. 3
  224. ],
  225. [
  226. "LoveSong3",
  227. "LoveSong3",
  228. 1
  229. ],
  230. [
  231. "MyOnlySong3",
  232. "MyOnlySong3",
  233. 19
  234. ],
  235. [
  236. "StrangeSong1",
  237. "StrangeSong1",
  238. 5
  239. ]
  240. ],
  241. "selections": [
  242. false,
  243. false,
  244. false,
  245. false,
  246. false,
  247. false,
  248. false,
  249. false,
  250. false
  251. ],
  252. "sources": [
  253. [
  254. "n:Song:Aria1",
  255. "n:Song:Aria1",
  256. "n:Song:Aria1"
  257. ],
  258. [
  259. "n:Song:Aria2",
  260. "n:Song:Aria2",
  261. "n:Song:Aria2"
  262. ],
  263. [
  264. "n:Song:Aria3",
  265. "n:Song:Aria3",
  266. "n:Song:Aria3"
  267. ],
  268. [
  269. "n:Song:Aria4",
  270. "n:Song:Aria4",
  271. "n:Song:Aria4"
  272. ],
  273. [
  274. "n:Song:DeadSong2",
  275. "n:Song:DeadSong2",
  276. "n:Song:DeadSong2"
  277. ],
  278. [
  279. "n:Song:FightSong4",
  280. "n:Song:FightSong4",
  281. "n:Song:FightSong4"
  282. ],
  283. [
  284. "n:Song:LoveSong3",
  285. "n:Song:LoveSong3",
  286. "n:Song:LoveSong3"
  287. ],
  288. [
  289. "n:Song:MyOnlySong3",
  290. "n:Song:MyOnlySong3",
  291. "n:Song:MyOnlySong3"
  292. ],
  293. [
  294. "n:Song:StrangeSong1",
  295. "n:Song:StrangeSong1",
  296. "n:Song:StrangeSong1"
  297. ]
  298. ],
  299. "total_selections": 0
  300. }`[1:] {
  301. t.Error("Unexpected response:", st, res)
  302. return
  303. }
  304. _, _, res = sendTestRequest(queryURL+"//main?rid="+rid+"&offset=500&limit=1", "GET", nil)
  305. if res != "Offset exceeds available rows" {
  306. t.Error("Unexpected response:", res)
  307. return
  308. }
  309. _, _, res = sendTestRequest(queryURL+"//main?rid=abc&offset=5&limit=1", "GET", nil)
  310. if res != "Unknown result ID (rid parameter)" {
  311. t.Error("Unexpected response:", res)
  312. return
  313. }
  314. }
  315. func TestQuery(t *testing.T) {
  316. queryURL := "http://localhost" + TESTPORT + EndpointQuery
  317. // POST requests should not be allowed
  318. _, _, res := sendTestRequest(queryURL, "POST",
  319. []byte(`{"msg":"Hello!"}`))
  320. if res != "Method Not Allowed" {
  321. t.Error("Unexpected response:", res)
  322. return
  323. }
  324. // Test error message
  325. _, _, res = sendTestRequest(queryURL+"main", "GET", nil)
  326. if res != "Missing query (q parameter)" {
  327. t.Error("Unexpected response:", res)
  328. return
  329. }
  330. _, _, res = sendTestRequest(queryURL+"main/bla/bla?q=get+Song", "GET", nil)
  331. if res != "Invalid resource specification: bla/bla" {
  332. t.Error("Unexpected response:", res)
  333. return
  334. }
  335. _, _, res = sendTestRequest(queryURL+"/?q=get+Song", "GET", nil)
  336. if res != "Need a partition" {
  337. t.Error("Unexpected response:", res)
  338. return
  339. }
  340. _, _, res = sendTestRequest(queryURL+"main/?q=get+BLA", "GET", nil)
  341. if res != "EQL error in Main query: Unknown node kind (BLA) (Line:1 Pos:5)" {
  342. t.Error("Unexpected response:", res)
  343. return
  344. }
  345. // Test first real query
  346. _, _, res = sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)", "GET", nil)
  347. st, _, res2 := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=0&limit=9", "GET", nil)
  348. if st != "200 OK" || res2 != res || res != `
  349. {
  350. "header": {
  351. "data": [
  352. "1:n:key",
  353. "1:n:name",
  354. "1:n:ranking"
  355. ],
  356. "format": [
  357. "auto",
  358. "auto",
  359. "auto"
  360. ],
  361. "labels": [
  362. "Song Key",
  363. "Song Name",
  364. "Ranking"
  365. ],
  366. "primary_kind": "Song"
  367. },
  368. "rows": [
  369. [
  370. "Aria1",
  371. "Aria1",
  372. 8
  373. ],
  374. [
  375. "Aria2",
  376. "Aria2",
  377. 2
  378. ],
  379. [
  380. "Aria3",
  381. "Aria3",
  382. 4
  383. ],
  384. [
  385. "Aria4",
  386. "Aria4",
  387. 18
  388. ],
  389. [
  390. "DeadSong2",
  391. "DeadSong2",
  392. 6
  393. ],
  394. [
  395. "FightSong4",
  396. "FightSong4",
  397. 3
  398. ],
  399. [
  400. "LoveSong3",
  401. "LoveSong3",
  402. 1
  403. ],
  404. [
  405. "MyOnlySong3",
  406. "MyOnlySong3",
  407. 19
  408. ],
  409. [
  410. "StrangeSong1",
  411. "StrangeSong1",
  412. 5
  413. ]
  414. ],
  415. "selections": [
  416. false,
  417. false,
  418. false,
  419. false,
  420. false,
  421. false,
  422. false,
  423. false,
  424. false
  425. ],
  426. "sources": [
  427. [
  428. "n:Song:Aria1",
  429. "n:Song:Aria1",
  430. "n:Song:Aria1"
  431. ],
  432. [
  433. "n:Song:Aria2",
  434. "n:Song:Aria2",
  435. "n:Song:Aria2"
  436. ],
  437. [
  438. "n:Song:Aria3",
  439. "n:Song:Aria3",
  440. "n:Song:Aria3"
  441. ],
  442. [
  443. "n:Song:Aria4",
  444. "n:Song:Aria4",
  445. "n:Song:Aria4"
  446. ],
  447. [
  448. "n:Song:DeadSong2",
  449. "n:Song:DeadSong2",
  450. "n:Song:DeadSong2"
  451. ],
  452. [
  453. "n:Song:FightSong4",
  454. "n:Song:FightSong4",
  455. "n:Song:FightSong4"
  456. ],
  457. [
  458. "n:Song:LoveSong3",
  459. "n:Song:LoveSong3",
  460. "n:Song:LoveSong3"
  461. ],
  462. [
  463. "n:Song:MyOnlySong3",
  464. "n:Song:MyOnlySong3",
  465. "n:Song:MyOnlySong3"
  466. ],
  467. [
  468. "n:Song:StrangeSong1",
  469. "n:Song:StrangeSong1",
  470. "n:Song:StrangeSong1"
  471. ]
  472. ],
  473. "total_selections": 0
  474. }`[1:] {
  475. t.Error("Unexpected response:", st, res)
  476. return
  477. }
  478. }
  479. func TestGroupingInfo(t *testing.T) {
  480. queryURL := "http://localhost" + TESTPORT + EndpointQuery
  481. st, _, res := sendTestRequest(queryURL+"//main?q=get+Song+with+ordering(ascending+key)&offset=2&limit=3&groups=1", "GET", nil)
  482. if st != "200 OK" || res != `
  483. {
  484. "groups": [
  485. [
  486. "Best"
  487. ],
  488. [],
  489. []
  490. ],
  491. "header": {
  492. "data": [
  493. "1:n:key",
  494. "1:n:name",
  495. "1:n:ranking"
  496. ],
  497. "format": [
  498. "auto",
  499. "auto",
  500. "auto"
  501. ],
  502. "labels": [
  503. "Song Key",
  504. "Song Name",
  505. "Ranking"
  506. ],
  507. "primary_kind": "Song"
  508. },
  509. "rows": [
  510. [
  511. "Aria3",
  512. "Aria3",
  513. 4
  514. ],
  515. [
  516. "Aria4",
  517. "Aria4",
  518. 18
  519. ],
  520. [
  521. "DeadSong2",
  522. "DeadSong2",
  523. 6
  524. ]
  525. ],
  526. "selections": [
  527. false,
  528. false,
  529. false
  530. ],
  531. "sources": [
  532. [
  533. "n:Song:Aria3",
  534. "n:Song:Aria3",
  535. "n:Song:Aria3"
  536. ],
  537. [
  538. "n:Song:Aria4",
  539. "n:Song:Aria4",
  540. "n:Song:Aria4"
  541. ],
  542. [
  543. "n:Song:DeadSong2",
  544. "n:Song:DeadSong2",
  545. "n:Song:DeadSong2"
  546. ]
  547. ],
  548. "total_selections": 0
  549. }`[1:] {
  550. t.Error("Unexpected response:", st, res)
  551. return
  552. }
  553. st, _, res = sendTestRequest(queryURL+"//main?q=get+Song+primary+'group'+with+ordering(ascending+key)&offset=2&limit=3&groups=1", "GET", nil)
  554. if st != "400 Bad Request" || res != "Could not determine key of primary node - query needs a primary expression" {
  555. t.Error("Unexpected response:", st, res)
  556. return
  557. }
  558. }