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.

264 lines
4.2 KiB

  1. import {describe, expect, it} from 'vitest';
  2. import { convertToDataSourceQuery } 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 = convertToDataSourceQuery(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 = convertToDataSourceQuery(
  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 = convertToDataSourceQuery(
  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 = convertToDataSourceQuery(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 = convertToDataSourceQuery(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 = convertToDataSourceQuery(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 = convertToDataSourceQuery(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. });