-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuery.spec.ts
298 lines (217 loc) · 10.8 KB
/
Query.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { Query } from "../lib/builder/Query";
import { IFilterGroup } from "../lib/models/IFilterGroup";
import { IFilterClause } from "../lib/models/IFilterClause";
import { isFilterClause } from "../lib/builder/FilterBuilder";
interface Root {
Child: Entity;
}
interface Entity {
NumberProperty: number | null;
StringProperty: string;
BooleanProperty: boolean,
NavigationProperty: Detail;
DateProperty: Date;
}
interface Detail {
NumberProperty: number;
StringProperty: string;
}
describe("Query", () => {
it("should not compile", () => {
const query = new Query<Entity>("Entities");
// query.filter("NumberProperty", "=", "1337");
// query.filter("StringProperty", "=", 1337);
// query.filter("DateProperty", "=", 1337);
// query.filter(x => x.NavigationProperty.StringProperty, "=", 1337);
// query.filter(x => x.NavigationProperty.NumberProperty, "=", "1337");
}),
it("should append $count to odata-URI", () => {
const query = new Query<Entity>("Entities");
query.count();
const actual = query.toString();
expect(actual).toBe("Entities/$count");
}),
it("should allow to filter by a simple property and a discrete value", () => {
const query = new Query<Entity>("Entities");
query.filter((x) => x.NumberProperty, "=", 1337);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)");
}),
it("should not single quote null", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "!=", null);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty ne null)");
}),
it("should not single quote number", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "!=", 1337);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty ne 1337)");
}),
it("should not single quote boolean", () => {
const query = new Query<Entity>("Entities");
query.filter("BooleanProperty", "!=", true);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(BooleanProperty ne true)");
}),
it("should include $count-parameter when total count is included", () => {
const query = new Query<Entity>("Entities");
query.includeTotalCount().filter("NumberProperty", "=", 1337);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)&$count=true");
}),
it("should respect $orderby system query", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "=", 1337)
.order.by("StringProperty")
.order.by("NumberProperty", "desc");
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)&$orderby=StringProperty asc,NumberProperty desc");
}),
it("should respect $top system query", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "=", 1337)
.top(42);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)&$top=42");
}),
it("should respect $skip system query", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "=", 1337)
.skip(42);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)&$skip=42");
}),
it("should respect $skip system query", () => {
const query = new Query<Entity>("Entities");
query.filter("NumberProperty", "=", 1337)
.top(43).skip(41);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337)&$skip=41&$top=43");
}),
it("should allow to filter by a custom string and a discrete value", () => {
const query = new Query<Entity>("Entities");
query.filterCustom("Some/Custom/Expression", "=", "1337");
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(Some/Custom/Expression eq '1337')");
}),
it("should allow to filter by two simple properties and discrete values with logical AND operator", () => {
const query = new Query<Entity>("Entities")
.filter("NumberProperty", "=", 1337)
.and("StringProperty", "=", "1337");
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337) and (StringProperty eq '1337')");
}),
it("should allow to filter by two simple properties and discrete values with logical OR operator", () => {
const query = new Query<Entity>("Entities");
query
.filter("NumberProperty", "=", 1337)
.or("StringProperty", "=", "FilterValue");
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337) or (StringProperty eq 'FilterValue')");
}),
it("should allow to filter by a navigation property and a discrete value", () => {
const query = new Query<Entity>("Entities");
query.filter(x => x.NavigationProperty.NumberProperty, "=", 1337);
const actual = query.toString();
expect(actual).toBe("Entities?$filter=(NavigationProperty/NumberProperty eq 1337)");
}),
it("should allow to filter by two navigation properties and a discrete string value", () => {
const query = new Query<Root>("Roots");
query.filter(x => x.Child.NavigationProperty.StringProperty, "=", "FilterValue");
const actual = query.toString();
expect(actual).toBe("Roots?$filter=(Child/NavigationProperty/StringProperty eq 'FilterValue')");
}),
it("should allow to filter by two navigation properties and a discrete number value", () => {
const query = new Query<Root>("Roots");
query.filter(x => x.Child.NavigationProperty.NumberProperty, "=", 1337);
const actual = query.toString();
expect(actual).toBe("Roots?$filter=(Child/NavigationProperty/NumberProperty eq 1337)");
}),
it("should allow to group by multiple properties without a filter", () => {
const query = new Query<Entity>("Entities");
const actual = query.group.by(x => x.NavigationProperty.NumberProperty, "StringProperty").toString();
expect(actual).toBe("Entities?$apply=groupby((NavigationProperty/NumberProperty,StringProperty), aggregate($count as Count))");
}),
it("should allow to group by single property without a filter", () => {
const query = new Query<Entity>("Entities");
const actual = query.group.by("NumberProperty").toString();
expect(actual).toBe("Entities?$apply=groupby((NumberProperty), aggregate($count as Count))");
}),
it("should allow to group by single property with a single filter", () => {
const query = new Query<Entity>("Entities");
const actual = query
.filter("StringProperty", "!=", "FilterValue")
.group.by("NumberProperty").toString();
expect(actual).toBe("Entities?$apply=filter((StringProperty ne 'FilterValue'))/groupby((NumberProperty), aggregate($count as Count))");
}),
it("should allow to group by single property with a single filter and sum aggregation", () => {
const query = new Query<Entity>("Entities");
const actual = query
.filter("StringProperty", "!=", "FilterValue")
.group.by("NumberProperty")
.aggregate("StringProperty").with.sumAs("TheSum").toString();
expect(actual).toBe("Entities?$apply=filter((StringProperty ne 'FilterValue'))/groupby((NumberProperty), aggregate(StringProperty with sum as TheSum))");
}),
it("should allow to filter by a property and then group by another", () => {
const query = new Query<Entity>("Entities");
const date = new Date(2018, 0, 1, 0, 0, 0, 0);
const actual = query
.filter("StringProperty", "!=", "FilterValue")
.and("DateProperty", "d<", date)
.group.by("NumberProperty")
.aggregateCount()
.toString();
expect(actual)
.toBe(`Entities?$apply=filter((StringProperty ne 'FilterValue') and (DateProperty lt 2018-01-01T00%3A00%3A00%2B01%3A00))/groupby((NumberProperty), aggregate($count as Count))`);
}),
it("grouping products by day should yield correct url", () => {
const query = new Query<Entity>("Entities");
const actual = query.filter("StringProperty", "=", "FilterValue")
.countBy.dayOn("DateProperty").toString();
expect(actual).toBe(`Entities/count.byDay(on='DateProperty')/?$filter=(StringProperty eq 'FilterValue')`);
}),
it("should allow to return query", () => {
const query = new Query<Entity>("Entities");
const result = query
.filter("NumberProperty", "=", 1337)
.or("StringProperty", "=", "1337")
.query;
const actual = result.toString();
expect(actual).toBe("Entities?$filter=(NumberProperty eq 1337) or (StringProperty eq '1337')");
}),
it("should allow to group filters", () => {
const query = new Query<Entity>("Entities");
const filterClauses: IFilterClause[] = [
{ LogicalOperator: "and", Property: "NumberProperty", Operator: "=", Value: 1337 },
{ LogicalOperator: "or", Property: "NumberProperty", Operator: "=", Value: 7331 }];
const group: IFilterGroup = { LogicalOperator: "and", FilterClauses: filterClauses, isGroup: true } as IFilterGroup;
const result = query
.filter("StringProperty", "=", "1337")
.addGroup(group)
.query;
const actual = result.toString();
expect(actual).toBe("Entities?$filter=(StringProperty eq '1337') and ((NumberProperty eq 1337) or (NumberProperty eq 7331))");
}),
// it("should allow to fluently group filters", () => {
// const query = new Query<Entity>("Entities");
// const result = query
// .filter("StringProperty", "=", "1337").or("StringProperty", "=", "7331")
// .and( _ =>
// _.filter("NumberProperty", "=", 1337).or("NumberProperty", "=", 7331)
// )
// .query;
// const actual = result.toString();
// expect(actual).toBe("Entities?$filter=((StringProperty eq '1337') or (StringProperty eq '7331')) and ((NumberProperty eq 1337) or (NumberProperty eq 7331))");
// }),
it("should distinct between IFilterClause and IFilterGroup", () => {
const query = new Query<Entity>("Entities");
const filterClause: IFilterClause = { LogicalOperator: "or", Property: "Owner", Operator: "=", Value: "1337" } as IFilterClause;
const group: IFilterGroup = { LogicalOperator: "and", FilterClauses: [filterClause], isGroup: true } as IFilterGroup;
const isClause = isFilterClause(filterClause);
const isGroup = isFilterClause(group);
const actual = `${isClause} ${isGroup}`;
expect(actual).toBe("true false");
});
});