Repository navigation
Expand file tree
/
Copy pathHasConversionTests.cs
More file actions
534 lines (448 loc) · 15.3 KB
/
Copy pathHasConversionTests.cs
File metadata and controls
534 lines (448 loc) · 15.3 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
namespace QueryKit.UnitTests;
using Configuration;
using Exceptions;
using FluentAssertions;
using WebApiTestProject.Entities;
public class HasConversionTests
{
private static readonly Guid KnownGuid = Guid.Parse("ab7afb17-abca-4530-9f8f-bd1497e0f6be");
[Fact]
public void can_filter_struct_with_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""Id == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_struct_with_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("wrappedid").HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""wrappedid == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_struct_with_has_conversion_configured_before_query_name()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasConversion<string>().HasQueryName("wrappedid");
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""wrappedid == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_struct_with_query_name_differing_only_in_case_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("ID").HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""ID == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_struct_with_not_equals_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("wrappedid").HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""wrappedid != "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("one");
}
[Fact]
public void can_filter_by_property_path_when_query_name_and_has_conversion_are_configured()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("wrappedid").HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""Id == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_reference_type_with_query_name_differing_only_in_case_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasQueryName("email").HasConversion<string>();
});
// Act
var result = EmailRows().ApplyQueryKitFilter("""email == "b@x.com" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Email!.Value.Should().Be("b@x.com");
}
[Fact]
public void can_filter_reference_type_with_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasQueryName("mail").HasConversion<string>();
});
// Act
var result = EmailRows().ApplyQueryKitFilter("""mail == "b@x.com" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Email!.Value.Should().Be("b@x.com");
}
[Fact]
public void can_filter_nested_property_with_query_name_and_has_conversion()
{
// Arrange
var rows = new List<OwnerRow>
{
new() { Owner = new Owner { Contact = new EmailAddressRecord("a@x.com") } },
new() { Owner = new Owner { Contact = new EmailAddressRecord("b@x.com") } }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<OwnerRow>(x => x.Owner.Contact!).HasQueryName("contact").HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""contact == "b@x.com" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Owner.Contact!.Value.Should().Be("b@x.com");
}
[Fact]
public void child_property_of_converted_parent_with_query_name_compares_parent()
{
// Arrange
var input = """Email.Value == "a@x.com" """;
var configWithQueryName = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Email).HasQueryName("mail").HasConversion<string>();
});
var configWithoutQueryName = new QueryKitConfiguration(config =>
{
config.Property<TestingPerson>(x => x.Email).HasConversion<string>();
});
// Act
var filterWithQueryName = FilterParser.ParseFilter<TestingPerson>(input, configWithQueryName);
var filterWithoutQueryName = FilterParser.ParseFilter<TestingPerson>(input, configWithoutQueryName);
// Assert
filterWithQueryName.ToDisplayString().Should().Be("""x => (x.Email == new EmailAddress("a@x.com"))""");
filterWithQueryName.ToDisplayString().Should().Be(filterWithoutQueryName.ToDisplayString());
}
[Fact]
public void can_filter_property_list_with_lowercase_path_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""(id) == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Fact]
public void can_filter_nullable_struct_with_has_conversion()
{
// Arrange
var rows = new List<NullableWrappedIdRow>
{
new() { Id = new WrappedId(1) },
new() { Id = new WrappedId(2) },
new() { Id = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NullableWrappedIdRow>(x => x.Id!).HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""Id == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Id.Should().Be(new WrappedId(2));
}
[Fact]
public void can_filter_null_on_nullable_struct_with_has_conversion()
{
// Arrange
var rows = new List<NullableWrappedIdRow>
{
new() { Id = new WrappedId(1) },
new() { Id = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NullableWrappedIdRow>(x => x.Id!).HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""Id == null""", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Id.Should().BeNull();
}
[Fact]
public void can_filter_null_on_reference_type_with_has_conversion()
{
// Arrange
var rows = EmailRows();
rows.Add(new EmailRow { Email = null });
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""Email == null""", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Email.Should().BeNull();
}
[Fact]
public void can_filter_null_on_reference_type_with_query_name_and_has_conversion()
{
// Arrange
var rows = EmailRows();
rows.Add(new EmailRow { Email = null });
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasQueryName("mail").HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""mail == null""", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Email.Should().BeNull();
}
[Fact]
public void can_filter_guid_with_contains_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<GuidRow>(x => x.Id).HasConversion<string>();
});
// Act
var result = GuidRows().ApplyQueryKitFilter("""Id @= "ab7afb17" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Id.Should().Be(KnownGuid);
}
[Fact]
public void can_filter_guid_with_contains_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<GuidRow>(x => x.Id).HasQueryName("identifier").HasConversion<string>();
});
// Act
var result = GuidRows().ApplyQueryKitFilter("""identifier @= "ab7afb17" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Id.Should().Be(KnownGuid);
}
[Fact]
public void can_filter_nullable_struct_with_query_name_and_has_conversion()
{
// Arrange
var rows = new List<NullableWrappedIdRow>
{
new() { Id = new WrappedId(1) },
new() { Id = new WrappedId(2) },
new() { Id = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NullableWrappedIdRow>(x => x.Id!).HasQueryName("wrappedid").HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("""wrappedid == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Id.Should().Be(new WrappedId(2));
}
[Fact]
public void can_filter_property_list_with_lowercase_path_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("wrappedid").HasConversion<string>();
});
// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""(id) == "2" """, config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}
[Theory]
[InlineData("count")]
[InlineData("Number")]
public void int_with_has_conversion_compares_the_int(string queryName)
{
// Arrange
var rows = new List<NumberRow>
{
new() { Number = 1 },
new() { Number = 2 }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NumberRow>(x => x.Number).HasQueryName(queryName).HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter($"{queryName} == 2", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Number.Should().Be(2);
}
[Fact]
public void nullable_int_with_query_name_and_has_conversion_compares_the_int()
{
// Arrange
var rows = new List<NullableNumberRow>
{
new() { Number = 1 },
new() { Number = 2 },
new() { Number = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NullableNumberRow>(x => x.Number!).HasQueryName("count").HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter("count == 2", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Number.Should().Be(2);
}
[Theory]
[InlineData("level")]
[InlineData("Level")]
public void enum_with_has_conversion_compares_the_enum(string queryName)
{
// Arrange
var rows = new List<LevelRow>
{
new() { Level = LevelKind.Low },
new() { Level = LevelKind.High }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<LevelRow>(x => x.Level).HasQueryName(queryName).HasConversion<string>();
});
// Act
var result = rows.ApplyQueryKitFilter($"{queryName} == High", config).ToList();
// Assert
result.Count.Should().Be(1);
result[0].Level.Should().Be(LevelKind.High);
}
[Theory]
[InlineData("identifier")]
[InlineData("Id")]
public void guid_with_has_conversion_compares_a_guid_constant(string queryName)
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<GuidRow>(x => x.Id).HasQueryName(queryName).HasConversion<string>();
});
// Act
var filter = FilterParser.ParseFilter<GuidRow>($"{queryName} == \"{KnownGuid}\"", config);
// Assert
filter.ToDisplayString().Should().Be($"x => (x.Id == {KnownGuid})");
}
private static List<WrappedIdRow> WrappedIdRows() => new()
{
new() { Id = new WrappedId(1), Name = "one" },
new() { Id = new WrappedId(2), Name = "two" }
};
private static List<EmailRow> EmailRows() => new()
{
new() { Email = new EmailAddressRecord("a@x.com") },
new() { Email = new EmailAddressRecord("b@x.com") }
};
private static List<GuidRow> GuidRows() => new()
{
new() { Id = KnownGuid },
new() { Id = Guid.NewGuid() }
};
private readonly record struct WrappedId
{
public WrappedId(int value) => Value = value;
public WrappedId(string value) : this(int.Parse(value)) { }
public int Value { get; }
}
private record EmailAddressRecord(string Value);
private class WrappedIdRow
{
public WrappedId Id { get; set; }
public string Name { get; set; } = null!;
}
private class NullableWrappedIdRow
{
public WrappedId? Id { get; set; }
}
private class EmailRow
{
public EmailAddressRecord? Email { get; set; }
}
private class Owner
{
public EmailAddressRecord? Contact { get; set; }
}
private class OwnerRow
{
public Owner Owner { get; set; } = new();
}
private class GuidRow
{
public Guid Id { get; set; }
}
private class NumberRow
{
public int Number { get; set; }
}
private class NullableNumberRow
{
public int? Number { get; set; }
}
private enum LevelKind
{
Low,
High
}
private class LevelRow
{
public LevelKind Level { get; set; }
}
}