-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.test.js
243 lines (223 loc) · 7.56 KB
/
filter.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const { describe, it, expect } = require('@jest/globals');
const { buildMeiliSearchFilter } = require('../src');
describe('filter', () => {
it('should return a meilisearch filter string', () => {
const date = new Date('2000-01-01');
const timestamp = date.getTime();
// equal
expect(buildMeiliSearchFilter({ name: 'John' })).toBe('name = "John"');
expect(buildMeiliSearchFilter({ name: { $eq: 'John' } })).toBe(
'name = "John"',
);
expect(buildMeiliSearchFilter({ age: 18 })).toBe('age = 18');
expect(buildMeiliSearchFilter({ age: { $eq: 18 } })).toBe('age = 18');
expect(buildMeiliSearchFilter({ isStudent: true })).toBe(
'isStudent = true',
);
expect(buildMeiliSearchFilter({ isStudent: { $eq: true } })).toBe(
'isStudent = true',
);
expect(buildMeiliSearchFilter({ birth: date })).toBe(
`birth = ${timestamp}`,
);
expect(buildMeiliSearchFilter({ birth: { $eq: date } })).toBe(
`birth = ${timestamp}`,
);
expect(buildMeiliSearchFilter({ company: null })).toBe('company IS NULL');
// not equal
expect(buildMeiliSearchFilter({ name: { $ne: 'John' } })).toBe(
'name != "John"',
);
expect(buildMeiliSearchFilter({ age: { $ne: 18 } })).toBe('age != 18');
expect(buildMeiliSearchFilter({ isStudent: { $ne: true } })).toBe(
'isStudent != true',
);
expect(buildMeiliSearchFilter({ birth: { $ne: date } })).toBe(
`birth != ${timestamp}`,
);
expect(buildMeiliSearchFilter({ company: { $ne: null } })).toBe(
'company IS NOT NULL',
);
// greater than
expect(buildMeiliSearchFilter({ age: { $gt: 18 } })).toBe('age > 18');
expect(buildMeiliSearchFilter({ birth: { $gt: date } })).toBe(
`birth > ${timestamp}`,
);
expect(
buildMeiliSearchFilter({
age: {
$gt: 18,
$lt: undefined, // should be ignored
},
}),
).toBe('age > 18');
// greater than or equal
expect(buildMeiliSearchFilter({ age: { $gte: 18 } })).toBe('age >= 18');
expect(buildMeiliSearchFilter({ birth: { $gte: date } })).toBe(
`birth >= ${timestamp}`,
);
// less than
expect(buildMeiliSearchFilter({ age: { $lt: 18 } })).toBe('age < 18');
expect(buildMeiliSearchFilter({ birth: { $lt: date } })).toBe(
`birth < ${timestamp}`,
);
// less than or equal
expect(buildMeiliSearchFilter({ age: { $lte: 18 } })).toBe('age <= 18');
expect(buildMeiliSearchFilter({ birth: { $lte: date } })).toBe(
`birth <= ${timestamp}`,
);
// in
expect(buildMeiliSearchFilter({ name: { $in: ['John', 'Doe'] } })).toBe(
'name IN ["John", "Doe"]',
);
expect(buildMeiliSearchFilter({ name: ['John', 'Doe'] })).toBe(
'name IN ["John", "Doe"]',
);
// not in
expect(buildMeiliSearchFilter({ name: { $nin: ['John', 'Doe'] } })).toBe(
'name NOT IN ["John", "Doe"]',
);
// exists
expect(buildMeiliSearchFilter({ name: { $exists: true } })).toBe(
'name EXISTS',
);
expect(buildMeiliSearchFilter({ name: { $exists: false } })).toBe(
'name NOT EXISTS',
);
// empty
expect(buildMeiliSearchFilter({ name: { $empty: true } })).toBe(
'name IS EMPTY',
);
expect(buildMeiliSearchFilter({ name: { $empty: false } })).toBe(
'name IS NOT EMPTY',
);
// and
expect(
buildMeiliSearchFilter({ $and: [{ name: 'John' }, { age: 18 }] }),
).toBe('name = "John" AND age = 18');
expect(buildMeiliSearchFilter([{ name: 'John' }, { age: 18 }])).toBe(
'name = "John" AND age = 18',
);
// between
expect(buildMeiliSearchFilter({ age: { $between: [18, 30] } })).toBe(
'age 18 TO 30',
);
// contains
expect(buildMeiliSearchFilter({ name: { $contains: 'John' } })).toBe(
'name CONTAINS "John"',
);
expect(buildMeiliSearchFilter({ name: { $notContains: 'John' } })).toBe(
'name NOT CONTAINS "John"',
);
// startsWith
expect(buildMeiliSearchFilter({ name: { $startsWith: 'John' } })).toBe(
'name STARTS WITH "John"',
);
expect(buildMeiliSearchFilter({ name: { $notStartsWith: 'John' } })).toBe(
'name NOT STARTS WITH "John"',
);
// or
expect(
buildMeiliSearchFilter({ $or: [{ name: 'John' }, { age: 18 }] }),
).toBe('(name = "John" OR age = 18)');
// geo
expect(
buildMeiliSearchFilter({
$geoRadius: {
lat: 45.472735,
lng: 9.184019,
distanceInMeters: 2000,
},
}),
).toBe('_geoRadius(45.472735, 9.184019, 2000)');
expect(
buildMeiliSearchFilter({
$geoRoundingBox: {
topRight: {
lat: 45.494181,
lng: 9.214024,
},
bottomLeft: {
lat: 45.449484,
lng: 9.179175,
},
},
}),
).toBe('_geoBoundingBox([45.494181, 9.214024], [45.449484, 9.179175])');
// complex
expect(
buildMeiliSearchFilter({
$or: [
{ $or: [{ name: 'John' }, { name: 'Doe' }] },
{ $and: [{ age: { $gt: 18 } }, { isStudent: true }] },
],
age: { $lt: 30 },
$geoRadius: {
lat: 45.472735,
lng: 9.184019,
distanceInMeters: 2000,
},
}),
).toBe(
'((name = "John" OR name = "Doe") OR age > 18 AND isStudent = true) AND age < 30 AND _geoRadius(45.472735, 9.184019, 2000)',
);
});
it('some special cases', () => {
expect(buildMeiliSearchFilter({ name: undefined, age: { $gt: 18 } })).toBe(
'age > 18',
);
});
it('should throw an error', () => {
expect(() =>
buildMeiliSearchFilter({ name: { $unsupported: 'John' } }),
).toThrow('Unsupported operator: $unsupported');
expect(() => buildMeiliSearchFilter({ age: { $gt: 'a' } })).toThrow(
'$gt must be a number or a date',
);
expect(() => buildMeiliSearchFilter(undefined)).toThrow(
'Expected an object',
);
expect(() => buildMeiliSearchFilter({ age: { $in: 'a' } })).toThrow(
'$in must be an array',
);
expect(() => buildMeiliSearchFilter({ age: { $nin: 'a' } })).toThrow(
'$nin must be an array',
);
expect(() => buildMeiliSearchFilter({ age: { $between: 1 } })).toThrow(
'$between must be an array',
);
expect(() => buildMeiliSearchFilter({ age: { $between: [1] } })).toThrow(
'$between must have two elements',
);
expect(() =>
buildMeiliSearchFilter({ age: { $between: [1, 'a'] } }),
).toThrow('$between must be an array of numbers or dates');
expect(() => buildMeiliSearchFilter({ name: { $contains: 1 } })).toThrow(
'$contains must be a string',
);
expect(() => buildMeiliSearchFilter({ name: { $notContains: 1 } })).toThrow(
'$notContains must be a string',
);
expect(() => buildMeiliSearchFilter({ name: { $startsWith: 1 } })).toThrow(
'$startsWith must be a string',
);
expect(() =>
buildMeiliSearchFilter({ name: { $notStartsWith: 1 } }),
).toThrow('$notStartsWith must be a string');
expect(() => buildMeiliSearchFilter({ $or: 'a' })).toThrow(
'$or must be an array',
);
expect(() => buildMeiliSearchFilter({ $and: 'a' })).toThrow(
'$and must be an array',
);
expect(() => buildMeiliSearchFilter({ $geoRadius: 'a' })).toThrow(
'$geoRadius must be an object',
);
expect(() => buildMeiliSearchFilter({ $geoRoundingBox: 'a' })).toThrow(
'$geoBoundingBox must be an object',
);
expect(() => buildMeiliSearchFilter({ $geoRadius: { lat: 'a' } })).toThrow(
'$geoRadius.lat must be a number',
);
});
});