HATEOAS-first backend framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query.test.ts 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import {describe, expect, it} from 'vitest';
  2. import { convertToDataSourceQueryCollection } from '../../src/backend';
  3. describe('query', () => {
  4. it('returns a data source query collection from search params', () => {
  5. const q = new URLSearchParams();
  6. const collection = convertToDataSourceQueryCollection(q);
  7. expect(collection.expressions).toBeInstanceOf(Array);
  8. });
  9. it('coerces a numeric value', () => {
  10. const q = new URLSearchParams({
  11. attr: '2',
  12. attr2: '2',
  13. });
  14. const collection = convertToDataSourceQueryCollection(
  15. q,
  16. {
  17. processEntries: {
  18. attr: {
  19. type: 'number'
  20. }
  21. },
  22. },
  23. );
  24. expect(collection).toEqual({
  25. type: 'and',
  26. expressions: [
  27. {
  28. type: 'or',
  29. expressions: [
  30. {
  31. lhs: 'attr',
  32. operator: '=',
  33. rhs: 2,
  34. },
  35. ],
  36. },
  37. {
  38. type: 'or',
  39. expressions: [
  40. {
  41. lhs: 'attr2',
  42. operator: '=',
  43. rhs: '2',
  44. },
  45. ],
  46. },
  47. ],
  48. });
  49. });
  50. it('coerces a boolean value', () => {
  51. const q = new URLSearchParams({
  52. attr: 'true',
  53. attr2: 'false',
  54. attr3: 'true',
  55. attr4: 'false',
  56. });
  57. const collection = convertToDataSourceQueryCollection(
  58. q,
  59. {
  60. processEntries: {
  61. attr: {
  62. type: 'boolean',
  63. },
  64. attr2: {
  65. type: 'boolean',
  66. }
  67. },
  68. },
  69. );
  70. expect(collection).toEqual({
  71. type: 'and',
  72. expressions: [
  73. {
  74. type: 'or',
  75. expressions: [
  76. {
  77. lhs: 'attr',
  78. operator: '=',
  79. rhs: true,
  80. },
  81. ],
  82. },
  83. {
  84. type: 'or',
  85. expressions: [
  86. {
  87. lhs: 'attr2',
  88. operator: '=',
  89. rhs: false,
  90. },
  91. ],
  92. },
  93. {
  94. type: 'or',
  95. expressions: [
  96. {
  97. lhs: 'attr3',
  98. operator: '=',
  99. rhs: 'true',
  100. },
  101. ],
  102. },
  103. {
  104. type: 'or',
  105. expressions: [
  106. {
  107. lhs: 'attr4',
  108. operator: '=',
  109. rhs: 'false',
  110. },
  111. ],
  112. },
  113. ],
  114. });
  115. });
  116. it('returns an equal query', () => {
  117. const q = new URLSearchParams({
  118. attr: 'foo'
  119. });
  120. const collection = convertToDataSourceQueryCollection(q);
  121. expect(collection).toEqual({
  122. type: 'and',
  123. expressions: [
  124. {
  125. type: 'or',
  126. expressions: [
  127. {
  128. lhs: 'attr',
  129. operator: '=',
  130. rhs: 'foo',
  131. },
  132. ],
  133. },
  134. ],
  135. });
  136. });
  137. it('returns an AND operator query', () => {
  138. const q = new URLSearchParams([
  139. ['attr', 'foo'],
  140. ['attr2', 'bar'],
  141. ]);
  142. const collection = convertToDataSourceQueryCollection(q);
  143. expect(collection).toEqual({
  144. type: 'and',
  145. expressions: [
  146. {
  147. type: 'or',
  148. expressions: [
  149. {
  150. lhs: 'attr',
  151. operator: '=',
  152. rhs: 'foo',
  153. },
  154. ],
  155. },
  156. {
  157. type: 'or',
  158. expressions: [
  159. {
  160. lhs: 'attr2',
  161. operator: '=',
  162. rhs: 'bar',
  163. },
  164. ],
  165. },
  166. ],
  167. });
  168. });
  169. it('returns an OR operator query', () => {
  170. const q = new URLSearchParams([
  171. ['attr', 'foo'],
  172. ['attr', 'bar'],
  173. ]);
  174. const collection = convertToDataSourceQueryCollection(q);
  175. expect(collection).toEqual({
  176. type: 'and',
  177. expressions: [
  178. {
  179. type: 'or',
  180. expressions: [
  181. {
  182. lhs: 'attr',
  183. operator: '=',
  184. rhs: 'foo',
  185. },
  186. {
  187. lhs: 'attr',
  188. operator: '=',
  189. rhs: 'bar',
  190. }
  191. ]
  192. },
  193. ],
  194. });
  195. });
  196. it('returns an query with appropriate grouping', () => {
  197. const q = new URLSearchParams([
  198. ['attr3', 'quux'],
  199. ['attr', 'foo'],
  200. ['attr4', 'quuux'],
  201. ['attr', 'bar'],
  202. ['attr2', 'baz'],
  203. ]);
  204. const collection = convertToDataSourceQueryCollection(q);
  205. expect(collection).toEqual({
  206. type: 'and',
  207. expressions: [
  208. {
  209. type: 'or',
  210. expressions: [
  211. {
  212. lhs: 'attr3',
  213. operator: '=',
  214. rhs: 'quux',
  215. },
  216. ],
  217. },
  218. {
  219. type: 'or',
  220. expressions: [
  221. {
  222. lhs: 'attr',
  223. operator: '=',
  224. rhs: 'foo',
  225. },
  226. {
  227. lhs: 'attr',
  228. operator: '=',
  229. rhs: 'bar',
  230. }
  231. ]
  232. },
  233. {
  234. type: 'or',
  235. expressions: [
  236. {
  237. lhs: 'attr4',
  238. operator: '=',
  239. rhs: 'quuux',
  240. },
  241. ],
  242. },
  243. {
  244. type: 'or',
  245. expressions: [
  246. {
  247. lhs: 'attr2',
  248. operator: '=',
  249. rhs: 'baz',
  250. },
  251. ],
  252. },
  253. ],
  254. });
  255. });
  256. });