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

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