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 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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('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. });
  102. it('returns a less than or equal expression query', () => {
  103. const q = new URLSearchParams({
  104. attr: '.lt.eq.foo'
  105. });
  106. const collection = convertToDataSourceQueryCollection(q);
  107. expect(collection).toEqual([
  108. {
  109. lhs: 'attr',
  110. operator: '<=',
  111. rhs: '"foo"',
  112. },
  113. ]);
  114. });
  115. it('returns an AND operator query', () => {
  116. const q = new URLSearchParams([
  117. ['attr', 'foo'],
  118. ['attr', 'bar'],
  119. ]);
  120. const collection = convertToDataSourceQueryCollection(q);
  121. expect(collection).toEqual([
  122. {
  123. lhs: 'attr',
  124. operator: '=',
  125. rhs: '"foo"',
  126. },
  127. {
  128. lhs: 'attr',
  129. operator: '=',
  130. rhs: '"bar"',
  131. },
  132. ]);
  133. });
  134. it.skip('returns a query', () => {
  135. // * is not percent-encoded
  136. // . is not percent-encoded (used for form data such as input text dir...)
  137. //
  138. const q = new URLSearchParams({
  139. '.foo': 'foo',
  140. });
  141. console.log(q.toString());
  142. });
  143. });