forked from DevExpress/DevExtreme.AspNet.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterExpressionCompilerTests.cs
More file actions
380 lines (307 loc) · 13.2 KB
/
Copy pathFilterExpressionCompilerTests.cs
File metadata and controls
380 lines (307 loc) · 13.2 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using DevExtreme.AspNet.Data.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json;
using Xunit;
namespace DevExtreme.AspNet.Data.Tests {
public class FilterExpressionCompilerTests {
class DataItem1 {
public int IntProp { get; set; }
public string StringProp { get; set; }
public int? NullableProp { get; set; }
public DateTime Date { get; set; }
}
LambdaExpression Compile<T>(IList criteria, bool guardNulls = false) {
return new FilterExpressionCompiler(typeof(T), guardNulls).Compile(criteria);
}
[Fact]
public void ImplicitEquals() {
var expr = Compile<DataItem1>(new object[] { "IntProp", 123 });
Assert.Equal("(obj.IntProp == 123)", expr.Body.ToString());
}
[Fact]
public void ExplicitEquals() {
var expr = Compile<DataItem1>(new object[] { "IntProp", "=", 1225 });
Assert.Equal("(obj.IntProp == 1225)", expr.Body.ToString());
}
[Fact]
public void DoesNotEqual() {
var expr = Compile<DataItem1>(new object[] { "IntProp", "<>", 1 });
Assert.Equal("(obj.IntProp != 1)", expr.Body.ToString());
}
[Fact]
public void ComparisonOperations() {
foreach(var op in new[] { ">", "<", ">=", "<=" }) {
var expr = Compile<DataItem1>(new object[] { "IntProp", op, 9 });
Assert.Equal("(obj.IntProp " + op + " 9)", expr.Body.ToString());
}
}
[Fact]
public void StringContains() {
var expr = Compile<DataItem1>(new[] { "StringProp", "contains", "Abc" });
Assert.Equal("obj.StringProp.Contains(\"Abc\")", expr.Body.ToString());
}
[Fact]
public void StringNotContains() {
var expr = Compile<DataItem1>(new[] { "StringProp", "notContains", "Abc" });
Assert.Equal("Not(obj.StringProp.Contains(\"Abc\"))", expr.Body.ToString());
}
[Fact]
public void StartsWith() {
var expr = Compile<DataItem1>(new[] { "StringProp", "startsWith", "Prefix" });
Assert.Equal("obj.StringProp.StartsWith(\"Prefix\")", expr.Body.ToString());
}
[Fact]
public void EndsWith() {
var expr = Compile<DataItem1>(new[] { "StringProp", "endsWith", "Postfix" });
Assert.Equal("obj.StringProp.EndsWith(\"Postfix\")", expr.Body.ToString());
}
[Fact]
public void StringFunctionOnNonStringData() {
var expr = Compile<DataItem1>(new[] { "IntProp", "contains", "Abc" });
Assert.Equal("obj.IntProp.ToString().Contains(\"Abc\")", expr.Body.ToString());
}
[Fact]
public void StringFunctionGuardNulls() {
Assert.Equal(
@"(IIF((obj == null), null, obj.StringProp) ?? """").StartsWith(""abc"")",
Compile<DataItem1>(new[] { "StringProp", "startswith", "abc" }, true).Body.ToString()
);
Assert.Equal(
@"(IIF((obj == null), null, obj.IntProp.ToString()) ?? """").StartsWith(""abc"")",
Compile<DataItem1>(new[] { "IntProp", "startswith", "abc" }, true).Body.ToString()
);
}
[Fact]
public void ImplicitAndOfTwo() {
var crit = new[] {
new object[] { "IntProp", ">", 1 },
new object[] { "IntProp", "<", 10 }
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("((obj.IntProp > 1) AndAlso (obj.IntProp < 10))", expr.Body.ToString());
}
[Fact]
public void ExplicitAndOfTwo() {
var crit = new object[] {
new object[] { "IntProp", ">", 1 },
"and",
new object[] { "IntProp", "<", 10 }
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("((obj.IntProp > 1) AndAlso (obj.IntProp < 10))", expr.Body.ToString());
}
[Fact]
public void OrOfTwo() {
var crit = new object[] {
new object[] { "IntProp", 1 },
"or",
new object[] { "IntProp", 2 }
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("((obj.IntProp == 1) OrElse (obj.IntProp == 2))", expr.Body.ToString());
}
[Fact]
public void Not() {
var crit = new object[] {
"!",
new object[] {
new object[] { "IntProp", ">", 1 },
"and",
new object[] { "IntProp", "<", 10 }
}
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("Not(((obj.IntProp > 1) AndAlso (obj.IntProp < 10)))", expr.Body.ToString());
}
[Fact]
public void IsUnaryWithJsonCriteria() {
var crit = JsonSerializer.Deserialize<IList>("[\"!\", []]", DataSourceLoadOptionsParser.DEFAULT_SERIALIZER_OPTIONS);
var compiler = new FilterExpressionCompiler(typeof(object), false);
Assert.True(compiler.IsUnary(crit));
}
[Fact]
public void GroupOfMany() {
var crit = new object[] {
new object[] { "IntProp", ">", 1 },
new object[] { "IntProp", "<", 10 },
"and",
new[] { "StringProp", "<>", "abc" }
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("(((obj.IntProp > 1) AndAlso (obj.IntProp < 10)) AndAlso (obj.StringProp != \"abc\"))", expr.Body.ToString());
}
[Fact]
public void NestedGroups() {
var crit = new object[] {
new object[] { "IntProp", 1 },
"||",
new[] {
new object[] { "IntProp", ">", 1 },
new object[] { "IntProp", "<", 10 }
}
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("((obj.IntProp == 1) OrElse ((obj.IntProp > 1) AndAlso (obj.IntProp < 10)))", expr.Body.ToString());
}
[Fact]
public void MixedGroupOperatorsWithoutBrackets() {
var crit = new object[] {
new object[] { "IntProp", ">", 1 },
new object[] { "IntProp", "<", 10 },
"||",
new object[] { "IntProp", "=", 100 },
};
var e = Record.Exception(() => Compile<DataItem1>(crit));
Assert.Contains("Mixing", e.Message);
}
[Fact]
public void MultipleOrRegression() {
var crit = new object[] {
new object[] { "IntProp", 1 },
"or",
new object[] { "IntProp", 2 },
"or",
new object[] { "IntProp", 3 }
};
var expr = Compile<DataItem1>(crit);
Assert.Equal("(((obj.IntProp == 1) OrElse (obj.IntProp == 2)) OrElse (obj.IntProp == 3))", expr.Body.ToString());
}
[Fact]
public void ThisAsLeftValue() {
var expr = Compile<int>(new object[] { "this", 1 });
Assert.Equal("(obj == 1)", expr.Body.ToString());
}
[Fact]
public void NullablePropertyAndPureValue() {
var expr = Compile<DataItem1>(new object[] { "NullableProp", 1 });
Assert.Equal("(obj.NullableProp == 1)", expr.Body.ToString());
var method = expr.Compile();
var result = (bool)method.DynamicInvoke(new DataItem1 { NullableProp = 1 });
Assert.True(result);
}
[Fact]
public void NoConvertWhenCompareWithNull() {
var expr = Compile<DataItem1>(new[] { "StringProp", null });
Assert.Equal("(obj.StringProp == null)", expr.Body.ToString());
}
[Fact]
public void T105740() {
var data = new[] {
new DataItem1{ Date = new DateTime(2011, 12, 13) }
};
Assert.True((bool)Compile<DataItem1>(new object[] { "Date", "12/13/2011 00:00:00" }).Compile().DynamicInvoke(data[0]));
}
[Fact]
public void JsonObjects() {
var crit = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]", DataSourceLoadOptionsParser.DEFAULT_SERIALIZER_OPTIONS);
var expr = Compile<DataItem1>(crit);
Assert.Equal(@"((obj.StringProp == ""abc"") AndAlso (obj.NullableProp == null))", expr.Body.ToString());
}
[Fact]
public void StringInequality() {
foreach(var op in new[] { "<", "<=", ">=", ">" }) {
Assert.Equal(
$@"(Compare(obj.StringProp, ""a"") {op} 0)",
Compile<DataItem1>(new[] { "StringProp", op, "a" }).Body.ToString()
);
}
Assert.Equal(
"(Compare(obj.StringProp, null) > 0)",
Compile<DataItem1>(new[] { "StringProp", ">", null }).Body.ToString()
);
}
[Fact]
public void Issue136() {
var x = Record.Exception(delegate {
Compile<Tuple<int>>(new[] { "Item99", "1" });
});
Assert.True(x is ArgumentException);
}
[Fact]
public void ValueTypeAndNull() {
// Part of https://devexpress.com/issue=T616169 fix
string CompileOperation(string op) {
return Compile<Tuple<int>>(new[] { "Item1", op, null }).Body.ToString();
}
var expectedConvert = Compat.ExpectedConvert("obj.Item1", "Nullable`1");
Assert.Equal($"({expectedConvert} == null)", CompileOperation("="));
Assert.Equal($"({expectedConvert} != null)", CompileOperation("<>"));
// https://stackoverflow.com/q/4399932
Assert.Equal("False", CompileOperation(">"));
Assert.Equal("False", CompileOperation(">="));
Assert.Equal("False", CompileOperation("<"));
Assert.Equal("False", CompileOperation("<="));
}
[Fact]
public void GuidComparison() {
// https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/339
var sampleGuid = Guid.Empty.ToString();
Assert.Equal(
$"(obj.CompareTo({sampleGuid}) > 0)",
Compile<Guid>(new[] { "this", ">", sampleGuid }).Body.ToString()
);
Assert.Equal(
$"(obj.Value.CompareTo({sampleGuid}) < 0)",
Compile<Guid?>(new[] { "this", "<", sampleGuid }).Body.ToString()
);
Assert.Equal(
$"IIF((obj == null), False, (obj.Value.CompareTo({sampleGuid}) >= 0))",
Compile<Guid?>(new[] { "this", ">=", sampleGuid }, true).Body.ToString()
);
Assert.Equal(
"False",
Compile<Guid?>(new[] { "this", "<=", null }).Body.ToString()
);
}
[Fact]
public void EnumComparison() {
// https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/164
var fridayValues = new object[] {
DayOfWeek.Friday,
new DayOfWeek?(DayOfWeek.Friday),
5,
new long?(5),
"5",
"FRIDAY",
"friday",
};
void Case<T>(IList criteria, bool guardNulls, string expectedExprBodyText, IReadOnlyList<T> input, IReadOnlyList<bool> expectedOutput) {
var expr = Compile<T>(criteria, guardNulls);
Assert.Equal(expectedExprBodyText, expr.Body.ToString());
var func = expr.Compile();
var actualOutput = input.Select(i => (bool)func.DynamicInvoke(i));
Assert.Equal(expectedOutput, actualOutput);
}
foreach(var friday in fridayValues) {
Case(
new object[] { "this", ">", friday }, false,
$"({Compat.ExpectedConvert("obj", "Int32")} > 5)",
new[] { DayOfWeek.Friday, DayOfWeek.Saturday },
new[] { false, true }
);
Case(
new object[] { "this", "<", friday }, false,
$"({Compat.ExpectedConvert("obj", "Nullable`1")} < 5)",
new DayOfWeek?[] { DayOfWeek.Monday, DayOfWeek.Friday },
new[] { true, false }
);
Case(
new object[] { "this", ">=", friday }, true,
$"({Compat.ExpectedConvert("obj", "Nullable`1")} >= 5)",
new DayOfWeek?[] { DayOfWeek.Monday, DayOfWeek.Friday, DayOfWeek.Saturday },
new[] { false, true, true }
);
}
Case(
new object[] { "this", "<=", null }, false,
$"({Compat.ExpectedConvert("obj", "Nullable`1")} <= null)",
new DayOfWeek?[] { null, DayOfWeek.Thursday },
new[] { false, false }
);
}
}
}