-
Notifications
You must be signed in to change notification settings - Fork 140
/
comparison.builder.spec.ts
253 lines (224 loc) · 7.67 KB
/
comparison.builder.spec.ts
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
244
245
246
247
248
249
250
251
252
253
import { CommonFieldComparisonBetweenType } from '@nestjs-query/core';
import { model, Types } from 'mongoose';
import { TestEntity, TestEntitySchema } from '../__fixtures__/test.entity';
import { ComparisonBuilder } from '../../src/query';
describe('ComparisonBuilder', (): void => {
const createComparisonBuilder = () => new ComparisonBuilder<TestEntity>(model('TestEntity', TestEntitySchema));
it('should throw an error for an invalid comparison type', () => {
// @ts-ignore
expect(() => createComparisonBuilder().build('stringType', 'bad', 'foo')).toThrow('unknown operator "bad"');
});
describe('eq comparisons', () => {
it('should build an unqualified eq sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'eq', 'foo')).toEqual({
stringType: {
$eq: 'foo',
},
});
});
it('should convert query fields to objectIds if the field is an objectId', (): void => {
expect(createComparisonBuilder().build('testReference', 'eq', '5f74af112fae2b251510e3ad')).toEqual({
testReference: {
$eq: Types.ObjectId('5f74af112fae2b251510e3ad'),
},
});
});
});
it('should build neq sql fragment', (): void => {
expect(createComparisonBuilder().build('numberType', 'neq', 1)).toEqual({
numberType: {
$ne: 1,
},
});
});
it('should build gt sql fragment', (): void => {
expect(createComparisonBuilder().build('numberType', 'gt', 1)).toEqual({
numberType: {
$gt: 1,
},
});
});
it('should build gte sql fragment', (): void => {
expect(createComparisonBuilder().build('numberType', 'gte', 1)).toEqual({
numberType: {
$gte: 1,
},
});
});
it('should build lt sql fragment', (): void => {
expect(createComparisonBuilder().build('numberType', 'lt', 1)).toEqual({
numberType: {
$lt: 1,
},
});
});
it('should build lte sql fragment', (): void => {
expect(createComparisonBuilder().build('numberType', 'lte', 1)).toEqual({
numberType: {
$lte: 1,
},
});
});
it('should build like sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'like', '%hello%')).toEqual({
stringType: {
$regex: /.*hello.*/,
},
});
});
it('should build notLike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'notLike', '%hello%')).toEqual({
stringType: {
$not: { $regex: /.*hello.*/ },
},
});
});
it('should build iLike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'iLike', '%hello%')).toEqual({
stringType: {
$regex: /.*hello.*/i,
},
});
});
it('should build notILike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'notILike', '%hello%')).toEqual({
stringType: {
$not: { $regex: /.*hello.*/i },
},
});
});
describe('is comparisons', () => {
it('should build is true', (): void => {
expect(createComparisonBuilder().build('boolType', 'is', true)).toEqual({
boolType: {
$eq: true,
},
});
});
it('should build is false', (): void => {
expect(createComparisonBuilder().build('boolType', 'is', false)).toEqual({
boolType: {
$eq: false,
},
});
});
it('should build is null', (): void => {
expect(createComparisonBuilder().build('boolType', 'is', null)).toEqual({
boolType: {
$eq: null,
},
});
});
});
describe('isNot comparisons', () => {
it('should build is true', (): void => {
expect(createComparisonBuilder().build('boolType', 'isNot', true)).toEqual({
boolType: {
$ne: true,
},
});
});
it('should build is false', (): void => {
expect(createComparisonBuilder().build('boolType', 'isNot', false)).toEqual({
boolType: {
$ne: false,
},
});
});
it('should build is null', (): void => {
expect(createComparisonBuilder().build('boolType', 'isNot', null)).toEqual({
boolType: {
$ne: null,
},
});
});
});
describe('in comparisons', () => {
it('should build in comparisons', (): void => {
const arr = [1, 2, 3];
expect(createComparisonBuilder().build('numberType', 'in', arr)).toEqual({
numberType: {
$in: arr,
},
});
});
it('should convert query fields to objectIds if the field is an objectId', (): void => {
expect(createComparisonBuilder().build('testReference', 'in', ['5f74af112fae2b251510e3ad'])).toEqual({
testReference: {
$in: [Types.ObjectId('5f74af112fae2b251510e3ad')],
},
});
});
});
describe('notIn comparisons', () => {
it('should build notIn comparisons', (): void => {
const arr = ['a', 'b', 'c'];
expect(createComparisonBuilder().build('stringType', 'notIn', arr)).toEqual({
stringType: {
$nin: arr,
},
});
});
it('should convert query fields to objectIds if the field is an objectId', (): void => {
expect(createComparisonBuilder().build('testReference', 'notIn', ['5f74af112fae2b251510e3ad'])).toEqual({
testReference: {
$nin: [Types.ObjectId('5f74af112fae2b251510e3ad')],
},
});
});
});
describe('between comparisons', () => {
it('should build between comparisons', (): void => {
const between: CommonFieldComparisonBetweenType<number> = { lower: 1, upper: 10 };
expect(createComparisonBuilder().build('numberType', 'between', between)).toEqual({
numberType: { $gte: between.lower, $lte: between.upper },
});
});
it('should convert query fields to objectIds if the field is an objectId', (): void => {
expect(
createComparisonBuilder().build('testReference', 'between', {
lower: '5f74af112fae2b251510e3ad',
upper: '5f74af112fae2b251510e3ad',
}),
).toEqual({
testReference: {
$gte: Types.ObjectId('5f74af112fae2b251510e3ad'),
$lte: Types.ObjectId('5f74af112fae2b251510e3ad'),
},
});
});
it('should throw an error if the comparison is not a between comparison', (): void => {
const between = [1, 10];
expect(() => createComparisonBuilder().build('numberType', 'between', between)).toThrow(
'Invalid value for between expected {lower: val, upper: val} got [1,10]',
);
});
});
describe('notBetween comparisons', () => {
it('should build not between comparisons', (): void => {
const between: CommonFieldComparisonBetweenType<number> = { lower: 1, upper: 10 };
expect(createComparisonBuilder().build('numberType', 'notBetween', between)).toEqual({
numberType: { $lt: between.lower, $gt: between.upper },
});
});
it('should convert query fields to objectIds if the field is an objectId', (): void => {
expect(
createComparisonBuilder().build('testReference', 'notBetween', {
lower: '5f74af112fae2b251510e3ad',
upper: '5f74af112fae2b251510e3ad',
}),
).toEqual({
testReference: {
$lt: Types.ObjectId('5f74af112fae2b251510e3ad'),
$gt: Types.ObjectId('5f74af112fae2b251510e3ad'),
},
});
});
it('should throw an error if the comparison is not a between comparison', (): void => {
const between = [1, 10];
expect(() => createComparisonBuilder().build('numberType', 'notBetween', between)).toThrow(
'Invalid value for notbetween expected {lower: val, upper: val} got [1,10]',
);
});
});
});