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.

183 lines
3.5 KiB

  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).toBeInstanceOf(Array);
  8. });
  9. it('returns an equal query', () => {
  10. const q = new URLSearchParams({
  11. attr: 'foo'
  12. });
  13. const collection = convertToDataSourceQueryCollection(q);
  14. expect(collection).toEqual([
  15. {
  16. lhs: 'attr',
  17. operator: '=',
  18. rhs: '"foo"',
  19. },
  20. ]);
  21. });
  22. it('returns an equal expression query', () => {
  23. const q = new URLSearchParams({
  24. attr: '.eq.foo'
  25. });
  26. const collection = convertToDataSourceQueryCollection(q);
  27. expect(collection).toEqual([
  28. {
  29. lhs: 'attr',
  30. operator: '=',
  31. rhs: '"foo"',
  32. },
  33. ]);
  34. });
  35. it('returns a not equal expression query', () => {
  36. const q = new URLSearchParams({
  37. attr: '.neq.foo'
  38. });
  39. const collection = convertToDataSourceQueryCollection(q);
  40. expect(collection).toEqual([
  41. {
  42. lhs: 'attr',
  43. operator: '!=',
  44. rhs: '"foo"',
  45. },
  46. ]);
  47. });
  48. it('returns a greater than expression query', () => {
  49. const q = new URLSearchParams({
  50. attr: '.gt.foo'
  51. });
  52. const collection = convertToDataSourceQueryCollection(q);
  53. expect(collection).toEqual([
  54. {
  55. lhs: 'attr',
  56. operator: '>',
  57. rhs: '"foo"',
  58. },
  59. ]);
  60. });
  61. it('returns a less than expression query', () => {
  62. const q = new URLSearchParams({
  63. attr: '.lt.foo'
  64. });
  65. const collection = convertToDataSourceQueryCollection(q);
  66. expect(collection).toEqual([
  67. {
  68. lhs: 'attr',
  69. operator: '<',
  70. rhs: '"foo"',
  71. },
  72. ]);
  73. });
  74. it.only('returns a greater than or equal expression query', () => {
  75. // expect(
  76. // convertToDataSourceQueryCollection(
  77. // new URLSearchParams({
  78. // attr: '.gt.eq.foo'
  79. // })
  80. // )
  81. // ).toEqual([
  82. // {
  83. // lhs: 'attr',
  84. // operator: '>=',
  85. // rhs: '"foo"',
  86. // },
  87. // ]);
  88. expect(
  89. convertToDataSourceQueryCollection(
  90. new URLSearchParams({
  91. attr: '.gte.foo'
  92. })
  93. )
  94. ).toEqual([
  95. {
  96. lhs: 'attr',
  97. operator: '>=',
  98. rhs: '"foo"',
  99. },
  100. ]);
  101. expect(
  102. convertToDataSourceQueryCollection(
  103. new URLSearchParams({
  104. attr: '.neq.test'
  105. })
  106. )
  107. ).toEqual([
  108. {
  109. lhs: 'attr',
  110. operator: '!=',
  111. rhs: '"test"',
  112. },
  113. ]);
  114. // expect(
  115. // convertToDataSourceQueryCollection(
  116. // new URLSearchParams({
  117. // attr: '.gte.foo..test'
  118. // })
  119. // )
  120. // ).toEqual([
  121. // {
  122. // lhs: 'attr',
  123. // operator: '>=',
  124. // rhs: '"foo.test"',
  125. // },
  126. // ]);
  127. });
  128. it('returns a less than or equal expression query', () => {
  129. const q = new URLSearchParams({
  130. attr: '.lt.eq.foo'
  131. });
  132. const collection = convertToDataSourceQueryCollection(q);
  133. expect(collection).toEqual([
  134. {
  135. lhs: 'attr',
  136. operator: '<=',
  137. rhs: '"foo"',
  138. },
  139. ]);
  140. });
  141. it('returns an AND operator query', () => {
  142. const q = new URLSearchParams([
  143. ['attr', 'foo'],
  144. ['attr', 'bar'],
  145. ]);
  146. const collection = convertToDataSourceQueryCollection(q);
  147. expect(collection).toEqual([
  148. {
  149. lhs: 'attr',
  150. operator: '=',
  151. rhs: '"foo"',
  152. },
  153. {
  154. lhs: 'attr',
  155. operator: '=',
  156. rhs: '"bar"',
  157. },
  158. ]);
  159. });
  160. it.skip('returns a query', () => {
  161. // * is not percent-encoded
  162. // . is not percent-encoded (used for form data such as input text dir...)
  163. //
  164. const q = new URLSearchParams({
  165. '.foo': 'foo',
  166. });
  167. console.log(q.toString());
  168. });
  169. });