where_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 interpreter
  11. import (
  12. "errors"
  13. "fmt"
  14. "testing"
  15. "devt.de/krotik/eliasdb/eql/parser"
  16. "devt.de/krotik/eliasdb/graph"
  17. "devt.de/krotik/eliasdb/graph/data"
  18. "devt.de/krotik/eliasdb/graph/graphstorage"
  19. )
  20. func TestDataQueries(t *testing.T) {
  21. gm := dataNodes()
  22. rt := NewGetRuntimeProvider("test", "main", gm, NewDefaultNodeInfo(gm))
  23. if err := runSearch("get mynode", `
  24. Labels: Mynode Key, Mynode Name, Nested, Nested.Nest1.Nest2.Atom1, Type
  25. Format: auto, auto, auto, auto, auto
  26. Data: 1:n:key, 1:n:name, 1:n:nested, 1:n:nested.nest1.nest2.atom1, 1:n:type
  27. 000, Node0, <not set>, <not set>, type1
  28. 123, Node1, <not set>, 1.46, type1
  29. 456, Node2, map[nest1:map[nest2:map[atom1:1.45]]], <not set>, type2
  30. `[1:], rt); err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. if err := runSearch("get mynode where attr:nested.nest1.nest2.atom1 != null", `
  35. Labels: Mynode Key, Mynode Name, Nested, Nested.Nest1.Nest2.Atom1, Type
  36. Format: auto, auto, auto, auto, auto
  37. Data: 1:n:key, 1:n:name, 1:n:nested, 1:n:nested.nest1.nest2.atom1, 1:n:type
  38. 123, Node1, <not set>, 1.46, type1
  39. `[1:], rt); err != nil {
  40. t.Error(err)
  41. return
  42. }
  43. if err := runSearch("get mynode where nested.nest1.nest2.atom1 = 1.45", `
  44. Labels: Mynode Key, Mynode Name, Nested, Nested.Nest1.Nest2.Atom1, Type
  45. Format: auto, auto, auto, auto, auto
  46. Data: 1:n:key, 1:n:name, 1:n:nested, 1:n:nested.nest1.nest2.atom1, 1:n:type
  47. 456, Node2, map[nest1:map[nest2:map[atom1:1.45]]], <not set>, type2
  48. `[1:], rt); err != nil {
  49. t.Error(err)
  50. return
  51. }
  52. if err := runSearch("get mynode where nested.nest1.nest2.atom = 1.45", `
  53. Labels: Mynode Key, Mynode Name, Nested, Nested.Nest1.Nest2.Atom1, Type
  54. Format: auto, auto, auto, auto, auto
  55. Data: 1:n:key, 1:n:name, 1:n:nested, 1:n:nested.nest1.nest2.atom1, 1:n:type
  56. `[1:], rt); err != nil {
  57. t.Error(err)
  58. return
  59. }
  60. // Test nested show clause
  61. if err := runSearch("get mynode where nested.nest1.nest2.atom1 = 1.45 show @objget(1, nested, nest1)", `
  62. Labels: Nested.nest1
  63. Format: auto
  64. Data: 1:func:objget()
  65. map[nest2:map[atom1:1.45]]
  66. `[1:], rt); err != nil {
  67. t.Error(err)
  68. return
  69. }
  70. if err := runSearch("get mynode where nested.nest1.nest2.atom1 = 1.45 show @objget(1, nested, nest1.nest2.atom1)", `
  71. Labels: Nested.nest1.nest2.atom1
  72. Format: auto
  73. Data: 1:func:objget()
  74. 1.45
  75. `[1:], rt); err != nil {
  76. t.Error(err)
  77. return
  78. }
  79. if err := runSearch("get mynode show @objget(1, key, nest1.nest2.atom1)", `
  80. Labels: Key.nest1.nest2.atom1
  81. Format: auto
  82. Data: 1:func:objget()
  83. 000
  84. 123
  85. 456
  86. `[1:], rt); err != nil {
  87. t.Error(err)
  88. return
  89. }
  90. if err := runSearch("get mynode where nested.nest1.nest2.atom1 = 1.45 show name", `
  91. Labels: Mynode Name
  92. Format: auto
  93. Data: 1:n:name
  94. Node2
  95. `[1:], rt); err != nil {
  96. t.Error(err)
  97. return
  98. }
  99. if err := runSearch("get mynode where nested.nest1.nest2.atom1 = 1.45 show key, @objget(1, key, nest1.nest2.atom1)", `
  100. Labels: Mynode Key, Key.nest1.nest2.atom1
  101. Format: auto, auto
  102. Data: 1:n:key, 1:func:objget()
  103. 456, 456
  104. `[1:], rt); err != nil {
  105. t.Error(err)
  106. return
  107. }
  108. }
  109. func TestWhere(t *testing.T) {
  110. gm, _ := simpleGraph()
  111. rt := NewGetRuntimeProvider("test", "main", gm, NewDefaultNodeInfo(gm))
  112. if err := runSearch("get mynode", `
  113. Labels: Mynode Key, Name
  114. Format: auto, auto
  115. Data: 1:n:key, 1:n:Name
  116. 000, Node0
  117. 123, Node1
  118. `[1:], rt); err != nil {
  119. t.Error(err)
  120. return
  121. }
  122. // Test simple where clause
  123. if err := runSearch("get mynode where Name = Node1", `
  124. Labels: Mynode Key, Name
  125. Format: auto, auto
  126. Data: 1:n:key, 1:n:Name
  127. 123, Node1
  128. `[1:], rt); err != nil {
  129. t.Error(err)
  130. return
  131. }
  132. if err := runSearch("get mynode where 1", `
  133. Labels: Mynode Key, Name
  134. Format: auto, auto
  135. Data: 1:n:key, 1:n:Name
  136. 000, Node0
  137. 123, Node1
  138. `[1:], rt); err != nil {
  139. t.Error(err)
  140. return
  141. }
  142. if err := runSearch("get mynode where 0", `
  143. Labels: Mynode Key, Name
  144. Format: auto, auto
  145. Data: 1:n:key, 1:n:Name
  146. `[1:], rt); err != nil {
  147. t.Error(err)
  148. return
  149. }
  150. if err := runSearch("get mynode where true", `
  151. Labels: Mynode Key, Name
  152. Format: auto, auto
  153. Data: 1:n:key, 1:n:Name
  154. 000, Node0
  155. 123, Node1
  156. `[1:], rt); err != nil {
  157. t.Error(err)
  158. return
  159. }
  160. if err := runSearch("get mynode where false", `
  161. Labels: Mynode Key, Name
  162. Format: auto, auto
  163. Data: 1:n:key, 1:n:Name
  164. `[1:], rt); err != nil {
  165. t.Error(err)
  166. return
  167. }
  168. if err := runSearch("get mynode where null", `
  169. Labels: Mynode Key, Name
  170. Format: auto, auto
  171. Data: 1:n:key, 1:n:Name
  172. `[1:], rt); err != nil {
  173. t.Error(err)
  174. return
  175. }
  176. if err := runSearch("get mynode where 'a'", `
  177. Labels: Mynode Key, Name
  178. Format: auto, auto
  179. Data: 1:n:key, 1:n:Name
  180. 000, Node0
  181. 123, Node1
  182. `[1:], rt); err != nil {
  183. t.Error(err)
  184. return
  185. }
  186. if err := runSearch("get mynode where ''", `
  187. Labels: Mynode Key, Name
  188. Format: auto, auto
  189. Data: 1:n:key, 1:n:Name
  190. `[1:], rt); err != nil {
  191. t.Error(err)
  192. return
  193. }
  194. if err := testSimpleOperationErrors("get mynode where Name = Node1", rt); err != nil {
  195. t.Error(err)
  196. }
  197. if err := runSearch("get mynode where attr:Name != val:Node1", `
  198. Labels: Mynode Key, Name
  199. Format: auto, auto
  200. Data: 1:n:key, 1:n:Name
  201. 000, Node0
  202. `[1:], rt); err != nil {
  203. t.Error(err)
  204. return
  205. }
  206. if err := testSimpleOperationErrors("get mynode where Name != Node1", rt); err != nil {
  207. t.Error(err)
  208. }
  209. if err := runSearch("get mynode where eattr:Name != Node1", "", rt); err.Error() !=
  210. "EQL error in test: Invalid where clause (No edge data available at this level) (Line:1 Pos:18)" {
  211. t.Error(err)
  212. return
  213. }
  214. if err := runSearch("get mynode traverse :::mynewnode where r'Nam \xe2\x8c\x98 e' = Node4", `
  215. Labels: Mynode Key, Name, Mynewnode Key, Na Me, Nam ⌘ E, Name
  216. Format: auto, auto, auto, auto, auto, auto
  217. Data: 1:n:key, 1:n:Name, 2:n:key, 2:n:Na me, 2:n:Nam ⌘ e, 2:n:Name
  218. 123, Node1, xxx ⌘, <not set>, Node4, <not set>
  219. `[1:], rt); err != nil {
  220. t.Error(err)
  221. return
  222. }
  223. if err := runSearch("get mynode traverse :::mynewnode where eattr:name = Edge1abc99 end", `
  224. Labels: Mynode Key, Name, Mynewnode Key, Na Me, Nam ⌘ E, Name
  225. Format: auto, auto, auto, auto, auto, auto
  226. Data: 1:n:key, 1:n:Name, 2:n:key, 2:n:Na me, 2:n:Nam ⌘ e, 2:n:Name
  227. 123, Node1, xxx ⌘, <not set>, Node4, <not set>
  228. `[1:], rt); err != nil {
  229. t.Error(err)
  230. return
  231. }
  232. gm, _ = simpleList()
  233. rt = NewGetRuntimeProvider("test", "main", gm, NewDefaultNodeInfo(gm))
  234. if err := runSearch("get mynode", `
  235. Labels: Mynode Key, Mynode Name, Ranking
  236. Format: auto, auto, auto
  237. Data: 1:n:key, 1:n:name, 1:n:ranking
  238. 000, Node0, 1
  239. 123, Node1, 2.1
  240. 456, Node1, 3.5
  241. `[1:], rt); err != nil {
  242. t.Error(err)
  243. return
  244. }
  245. if err := runSearch("get mynode where ranking < 2.2", `
  246. Labels: Mynode Key, Mynode Name, Ranking
  247. Format: auto, auto, auto
  248. Data: 1:n:key, 1:n:name, 1:n:ranking
  249. 000, Node0, 1
  250. 123, Node1, 2.1
  251. `[1:], rt); err != nil {
  252. t.Error(err)
  253. return
  254. }
  255. if err := runSearch("get mynode where name > 'Node0'", `
  256. Labels: Mynode Key, Mynode Name, Ranking
  257. Format: auto, auto, auto
  258. Data: 1:n:key, 1:n:name, 1:n:ranking
  259. 123, Node1, 2.1
  260. 456, Node1, 3.5
  261. `[1:], rt); err != nil {
  262. t.Error(err)
  263. return
  264. }
  265. if err := runSearch("get mynode where name < Node1", `
  266. Labels: Mynode Key, Mynode Name, Ranking
  267. Format: auto, auto, auto
  268. Data: 1:n:key, 1:n:name, 1:n:ranking
  269. 000, Node0, 1
  270. `[1:], rt); err != nil {
  271. t.Error(err)
  272. return
  273. }
  274. if err := runSearch("get mynode where name <= Node1", `
  275. Labels: Mynode Key, Mynode Name, Ranking
  276. Format: auto, auto, auto
  277. Data: 1:n:key, 1:n:name, 1:n:ranking
  278. 000, Node0, 1
  279. 123, Node1, 2.1
  280. 456, Node1, 3.5
  281. `[1:], rt); err != nil {
  282. t.Error(err)
  283. return
  284. }
  285. if err := runSearch("get mynode where key > 055", `
  286. Labels: Mynode Key, Mynode Name, Ranking
  287. Format: auto, auto, auto
  288. Data: 1:n:key, 1:n:name, 1:n:ranking
  289. 123, Node1, 2.1
  290. 456, Node1, 3.5
  291. `[1:], rt); err != nil {
  292. t.Error(err)
  293. return
  294. }
  295. if err := runSearch("get mynode where key >= 023test", `
  296. Labels: Mynode Key, Mynode Name, Ranking
  297. Format: auto, auto, auto
  298. Data: 1:n:key, 1:n:name, 1:n:ranking
  299. 123, Node1, 2.1
  300. 456, Node1, 3.5
  301. `[1:], rt); err != nil {
  302. t.Error(err)
  303. return
  304. }
  305. if err := runSearch("get mynode where ranking <= 2.1", `
  306. Labels: Mynode Key, Mynode Name, Ranking
  307. Format: auto, auto, auto
  308. Data: 1:n:key, 1:n:name, 1:n:ranking
  309. 000, Node0, 1
  310. 123, Node1, 2.1
  311. `[1:], rt); err != nil {
  312. t.Error(err)
  313. return
  314. }
  315. if err := runSearch("get mynode where ranking > 2", `
  316. Labels: Mynode Key, Mynode Name, Ranking
  317. Format: auto, auto, auto
  318. Data: 1:n:key, 1:n:name, 1:n:ranking
  319. 123, Node1, 2.1
  320. 456, Node1, 3.5
  321. `[1:], rt); err != nil {
  322. t.Error(err)
  323. return
  324. }
  325. if err := runSearch("get mynode where ranking >= 2.1", `
  326. Labels: Mynode Key, Mynode Name, Ranking
  327. Format: auto, auto, auto
  328. Data: 1:n:key, 1:n:name, 1:n:ranking
  329. 123, Node1, 2.1
  330. 456, Node1, 3.5
  331. `[1:], rt); err != nil {
  332. t.Error(err)
  333. return
  334. }
  335. if err := runSearch("get mynode where ranking >= 2.1 and ranking < 3", `
  336. Labels: Mynode Key, Mynode Name, Ranking
  337. Format: auto, auto, auto
  338. Data: 1:n:key, 1:n:name, 1:n:ranking
  339. 123, Node1, 2.1
  340. `[1:], rt); err != nil {
  341. t.Error(err)
  342. return
  343. }
  344. if err := testSimpleOperationErrors("get mynode where Name + Node1", rt); err != nil {
  345. t.Error(err)
  346. return
  347. }
  348. if err := runSearch("get mynode where ranking + x", "", rt); err.Error() !=
  349. "EQL error in test: Value of operand is not a number (x) (Line:1 Pos:29)" {
  350. t.Error(err)
  351. return
  352. }
  353. if err := runSearch("get mynode where name + ranking", "", rt); err.Error() !=
  354. "EQL error in test: Value of operand is not a number (name=Node1) (Line:1 Pos:18)" {
  355. t.Error(err)
  356. return
  357. }
  358. if err := testSimpleOperationErrors("get mynode where 1 and 2", rt); err != nil {
  359. t.Error(err)
  360. }
  361. if err := runSearch("get mynode where ranking = 02.100 or ranking = 3.5", `
  362. Labels: Mynode Key, Mynode Name, Ranking
  363. Format: auto, auto, auto
  364. Data: 1:n:key, 1:n:name, 1:n:ranking
  365. 123, Node1, 2.1
  366. 456, Node1, 3.5
  367. `[1:], rt); err != nil {
  368. t.Error(err)
  369. return
  370. }
  371. if err := runSearch("get mynode where not ranking = 02.100", `
  372. Labels: Mynode Key, Mynode Name, Ranking
  373. Format: auto, auto, auto
  374. Data: 1:n:key, 1:n:name, 1:n:ranking
  375. 000, Node0, 1
  376. 456, Node1, 3.5
  377. `[1:], rt); err != nil {
  378. t.Error(err)
  379. return
  380. }
  381. if err := runSearch("get mynode where ranking = 10.7 % 4.1 + 0.1", `
  382. Labels: Mynode Key, Mynode Name, Ranking
  383. Format: auto, auto, auto
  384. Data: 1:n:key, 1:n:name, 1:n:ranking
  385. 123, Node1, 2.1
  386. `[1:], rt); err != nil {
  387. t.Error(err)
  388. return
  389. }
  390. if err := runSearch("get mynode where ranking = 8.7 // 4.1 + 0.1", `
  391. Labels: Mynode Key, Mynode Name, Ranking
  392. Format: auto, auto, auto
  393. Data: 1:n:key, 1:n:name, 1:n:ranking
  394. 123, Node1, 2.1
  395. `[1:], rt); err != nil {
  396. t.Error(err)
  397. return
  398. }
  399. if err := runSearch("get mynode where ranking = (3 * 4 - 10 + 0.1) / 2.1", `
  400. Labels: Mynode Key, Mynode Name, Ranking
  401. Format: auto, auto, auto
  402. Data: 1:n:key, 1:n:name, 1:n:ranking
  403. 000, Node0, 1
  404. `[1:], rt); err != nil {
  405. t.Error(err)
  406. return
  407. }
  408. if err := runSearch("get mynode where (3 * 4 - 10 + 0.1) - 2.1", `
  409. Labels: Mynode Key, Mynode Name, Ranking
  410. Format: auto, auto, auto
  411. Data: 1:n:key, 1:n:name, 1:n:ranking
  412. `[1:], rt); err != nil {
  413. t.Error(err)
  414. return
  415. }
  416. if err := runSearch("get mynode where (3 * 4 - 10 + 0.1) - 2", `
  417. Labels: Mynode Key, Mynode Name, Ranking
  418. Format: auto, auto, auto
  419. Data: 1:n:key, 1:n:name, 1:n:ranking
  420. 000, Node0, 1
  421. 123, Node1, 2.1
  422. 456, Node1, 3.5
  423. `[1:], rt); err != nil {
  424. t.Error(err)
  425. return
  426. }
  427. if err := runSearch("get mynode where [1,2] in [1,2,[1,2],3]", `
  428. Labels: Mynode Key, Mynode Name, Ranking
  429. Format: auto, auto, auto
  430. Data: 1:n:key, 1:n:name, 1:n:ranking
  431. 000, Node0, 1
  432. 123, Node1, 2.1
  433. 456, Node1, 3.5
  434. `[1:], rt); err != nil {
  435. t.Error(err)
  436. return
  437. }
  438. if err := runSearch("get mynode where ranking in [1,2,[1,2],3]", `
  439. Labels: Mynode Key, Mynode Name, Ranking
  440. Format: auto, auto, auto
  441. Data: 1:n:key, 1:n:name, 1:n:ranking
  442. 000, Node0, 1
  443. `[1:], rt); err != nil {
  444. t.Error(err)
  445. return
  446. }
  447. if err := testSimpleOperationErrors("get mynode where 0 in 2", rt); err != nil {
  448. t.Error(err)
  449. }
  450. if err := runSearch("get mynode where [1,2] in ranking", "", rt); err.Error() !=
  451. "EQL error in test: Value of operand is not a list (ranking=2.1) (Line:1 Pos:27)" {
  452. t.Error(err)
  453. return
  454. }
  455. if err := runSearch("get mynode where [1,2] in 1", "", rt); err.Error() !=
  456. "EQL error in test: Value of operand is not a list (1) (Line:1 Pos:27)" {
  457. t.Error(err)
  458. return
  459. }
  460. if err := runSearch("get mynode where ranking notin [1,2.1,3]", `
  461. Labels: Mynode Key, Mynode Name, Ranking
  462. Format: auto, auto, auto
  463. Data: 1:n:key, 1:n:name, 1:n:ranking
  464. 456, Node1, 3.5
  465. `[1:], rt); err != nil {
  466. t.Error(err)
  467. return
  468. }
  469. if err := runSearch("get mynode where name contains 1", `
  470. Labels: Mynode Key, Mynode Name, Ranking
  471. Format: auto, auto, auto
  472. Data: 1:n:key, 1:n:name, 1:n:ranking
  473. 123, Node1, 2.1
  474. 456, Node1, 3.5
  475. `[1:], rt); err != nil {
  476. t.Error(err)
  477. return
  478. }
  479. if err := testSimpleOperationErrors("get mynode where 0 contains 2", rt); err != nil {
  480. t.Error(err)
  481. }
  482. if err := runSearch("get mynode where name containsnot 1", `
  483. Labels: Mynode Key, Mynode Name, Ranking
  484. Format: auto, auto, auto
  485. Data: 1:n:key, 1:n:name, 1:n:ranking
  486. 000, Node0, 1
  487. `[1:], rt); err != nil {
  488. t.Error(err)
  489. return
  490. }
  491. if err := testSimpleOperationErrors("get mynode where 0 containsnot 2", rt); err != nil {
  492. t.Error(err)
  493. }
  494. if err := runSearch("get mynode where name beginswith Nod", `
  495. Labels: Mynode Key, Mynode Name, Ranking
  496. Format: auto, auto, auto
  497. Data: 1:n:key, 1:n:name, 1:n:ranking
  498. 000, Node0, 1
  499. 123, Node1, 2.1
  500. 456, Node1, 3.5
  501. `[1:], rt); err != nil {
  502. t.Error(err)
  503. return
  504. }
  505. if err := testSimpleOperationErrors("get mynode where 0 beginswith 2", rt); err != nil {
  506. t.Error(err)
  507. }
  508. if err := runSearch("get mynode where name endswith de0", `
  509. Labels: Mynode Key, Mynode Name, Ranking
  510. Format: auto, auto, auto
  511. Data: 1:n:key, 1:n:name, 1:n:ranking
  512. 000, Node0, 1
  513. `[1:], rt); err != nil {
  514. t.Error(err)
  515. return
  516. }
  517. if err := testSimpleOperationErrors("get mynode where 0 endswith 2", rt); err != nil {
  518. t.Error(err)
  519. }
  520. // Test regex
  521. if err := runSearch("get mynode where name like 'Node?'", `
  522. Labels: Mynode Key, Mynode Name, Ranking
  523. Format: auto, auto, auto
  524. Data: 1:n:key, 1:n:name, 1:n:ranking
  525. 000, Node0, 1
  526. 123, Node1, 2.1
  527. 456, Node1, 3.5
  528. `[1:], rt); err != nil {
  529. t.Error(err)
  530. return
  531. }
  532. if err := testSimpleOperationErrors("get mynode where 0 like 2", rt); err != nil {
  533. t.Error(err)
  534. }
  535. if err := runSearch("get mynode where name like '[1'", "", rt); err.Error() !=
  536. "EQL error in test: Value of operand is not a valid regex (\"[1\" - error parsing regexp: missing closing ]: `[1`) (Line:1 Pos:28)" {
  537. t.Error(err)
  538. return
  539. }
  540. gm, _ = regexList()
  541. rt = NewGetRuntimeProvider("test", "main", gm, NewDefaultNodeInfo(gm))
  542. if err := runSearch("get mynode where name like regex", "", rt); err.Error() !=
  543. "EQL error in test: Value of operand is not a valid regex (\"[1\" - error parsing regexp: missing closing ]: `[1`) (Line:1 Pos:28)" {
  544. t.Error(err)
  545. return
  546. }
  547. if err := runSearch("get mynode where name = node0 and name like regex", `
  548. Labels: Mynode Key, Mynode Name, Ranking, Regex
  549. Format: auto, auto, auto, auto
  550. Data: 1:n:key, 1:n:name, 1:n:ranking, 1:n:regex
  551. 000, node0, 1, ^[a-z]+[0-9]$
  552. `[1:], rt); err != nil {
  553. t.Error(err)
  554. return
  555. }
  556. if err := testSimpleOperationErrors("get mynode where name like regex", rt); err != nil {
  557. t.Error(err)
  558. }
  559. }
  560. func TestWhereErrors(t *testing.T) {
  561. gm, _ := simpleGraph()
  562. rt := NewGetRuntimeProvider("test", "main", gm, NewDefaultNodeInfo(gm))
  563. ast, err := parser.ParseWithRuntime("test", "get mynode where Name = Node1", rt)
  564. if err != nil {
  565. t.Error(err)
  566. return
  567. }
  568. val := ast.Children[1]
  569. // Test that normal eval of whereRuntime returns an error
  570. if _, err := val.Runtime.Eval(); err.Error() != "EQL error in test: Invalid construct (where) (Line:1 Pos:12)" {
  571. t.Error("Unexpected error return:", err)
  572. }
  573. val = ast.Children[1].Children[0]
  574. // Test that normal validate and eval of whereItemRuntime return errors
  575. if _, err := val.Runtime.Eval(); err.Error() != "EQL error in test: Invalid construct (=) (Line:1 Pos:23)" {
  576. t.Error("Unexpected error return:", err)
  577. }
  578. if err := val.Runtime.Validate(); err.Error() != "EQL error in test: Invalid construct (=) (Line:1 Pos:23)" {
  579. t.Error("Unexpected error return:", err)
  580. }
  581. // Test where validation error
  582. ast, err = parser.ParseWithRuntime("test", "get mynode show x where 1 = 2", rt)
  583. if _, err := ast.Runtime.Eval(); err.Error() !=
  584. "EQL error in test: Invalid construct (condition must be before show clause and traversals) (Line:1 Pos:19)" {
  585. t.Error(err)
  586. return
  587. }
  588. ast, err = parser.ParseWithRuntime("test", "get mynode where 1 = 2", rt)
  589. if err != nil {
  590. t.Error(err)
  591. return
  592. }
  593. vnode := ast.Children[1].Children[0].Children[0]
  594. tr := testRuntimeInst(rt.eqlRuntimeProvider, vnode)
  595. tr.retValidate = errors.New("TestError0")
  596. vnode.Runtime = tr
  597. err = ast.Runtime.Validate()
  598. if err == nil || err.Error() != "TestError0" {
  599. t.Error(err)
  600. return
  601. }
  602. // Test traversal where validation and eval error
  603. ast, err = parser.ParseWithRuntime("test", "get mynode traverse ::: where 1 = 2", rt)
  604. if err != nil {
  605. t.Error(err)
  606. return
  607. }
  608. vnode = ast.Children[1].Children[1].Children[0].Children[0]
  609. tr = testRuntimeInst(rt.eqlRuntimeProvider, vnode)
  610. tr.retCondEvalErr = errors.New("TestError1")
  611. vnode.Runtime = tr
  612. _, err = ast.Runtime.Eval()
  613. if err == nil || err.Error() != "TestError1" {
  614. t.Error(err)
  615. return
  616. }
  617. tr.retValidate = errors.New("TestError2")
  618. err = ast.Runtime.Validate()
  619. if err == nil || err.Error() != "TestError2" {
  620. t.Error(err)
  621. return
  622. }
  623. }
  624. func testSimpleOperationErrors(query string, rt *GetRuntimeProvider) error {
  625. ast, err := parser.ParseWithRuntime("test", query, rt)
  626. if err != nil {
  627. return err
  628. }
  629. insertTestRuntime := func(child int) error {
  630. val := ast.Children[1].Children[0].Children[child]
  631. tr := testRuntimeInst(rt.eqlRuntimeProvider, val)
  632. tr.retCondEvalErr = errors.New("TestError")
  633. oldRuntime := val.Runtime
  634. val.Runtime = tr
  635. _, err = ast.Runtime.Eval()
  636. if err == nil || err.Error() != "TestError" {
  637. return errors.New(fmt.Sprint("Unexpected error return:", err))
  638. }
  639. val.Runtime = oldRuntime
  640. return nil
  641. }
  642. if err := insertTestRuntime(0); err != nil {
  643. return err
  644. }
  645. return insertTestRuntime(1)
  646. }
  647. /*
  648. TestRuntime for values
  649. */
  650. type testRuntime struct {
  651. rtp *eqlRuntimeProvider
  652. node *parser.ASTNode
  653. retValidate error
  654. retEvalVal interface{}
  655. retEvalErr error
  656. retCondEvalVal interface{}
  657. retCondEvalErr error
  658. }
  659. /*
  660. invalidRuntimeInst returns a new runtime component instance.
  661. */
  662. func testRuntimeInst(rtp *eqlRuntimeProvider, node *parser.ASTNode) *testRuntime {
  663. return &testRuntime{rtp, node, nil, nil, nil, nil, nil}
  664. }
  665. /*
  666. Validate this node and all its child nodes.
  667. */
  668. func (rt *testRuntime) Validate() error {
  669. return rt.retValidate
  670. }
  671. /*
  672. Eval evaluate this runtime component.
  673. */
  674. func (rt *testRuntime) Eval() (interface{}, error) {
  675. return rt.retEvalVal, rt.retCondEvalErr
  676. }
  677. /*
  678. Evaluate the value as a condition component.
  679. */
  680. func (rt *testRuntime) CondEval(node data.Node, edge data.Edge) (interface{}, error) {
  681. return rt.retCondEvalVal, rt.retCondEvalErr
  682. }
  683. func simpleList() (*graph.Manager, *graphstorage.MemoryGraphStorage) {
  684. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  685. gm := graph.NewGraphManager(mgs)
  686. node0 := data.NewGraphNode()
  687. node0.SetAttr("key", "000")
  688. node0.SetAttr("kind", "mynode")
  689. node0.SetAttr("name", "Node0")
  690. node0.SetAttr("ranking", 1)
  691. gm.StoreNode("main", node0)
  692. node1 := data.NewGraphNode()
  693. node1.SetAttr("key", "123")
  694. node1.SetAttr("kind", "mynode")
  695. node1.SetAttr("name", "Node1")
  696. node1.SetAttr("ranking", 2.1)
  697. gm.StoreNode("main", node1)
  698. node2 := data.NewGraphNode()
  699. node2.SetAttr("key", "456")
  700. node2.SetAttr("kind", "mynode")
  701. node2.SetAttr("name", "Node1")
  702. node2.SetAttr("ranking", 3.5)
  703. gm.StoreNode("main", node2)
  704. return gm, mgs.(*graphstorage.MemoryGraphStorage)
  705. }
  706. func regexList() (*graph.Manager, *graphstorage.MemoryGraphStorage) {
  707. mgs := graphstorage.NewMemoryGraphStorage("mystorage")
  708. gm := graph.NewGraphManager(mgs)
  709. node0 := data.NewGraphNode()
  710. node0.SetAttr("key", "000")
  711. node0.SetAttr("kind", "mynode")
  712. node0.SetAttr("name", "node0")
  713. node0.SetAttr("ranking", 1)
  714. node0.SetAttr("regex", "^[a-z]+[0-9]$")
  715. gm.StoreNode("main", node0)
  716. node2 := data.NewGraphNode()
  717. node2.SetAttr("key", "456")
  718. node2.SetAttr("kind", "mynode")
  719. node2.SetAttr("name", "Node1")
  720. node2.SetAttr("regex", "[1")
  721. node2.SetAttr("ranking", 3.5)
  722. gm.StoreNode("main", node2)
  723. return gm, mgs.(*graphstorage.MemoryGraphStorage)
  724. }