-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathQueryableTests.Select.cs
More file actions
593 lines (494 loc) · 21.1 KB
/
QueryableTests.Select.cs
File metadata and controls
593 lines (494 loc) · 21.1 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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using FluentAssertions;
using Linq.PropertyTranslator.Core;
using QueryInterceptor.Core;
using Xunit;
using NFluent;
using Newtonsoft.Json.Linq;
#if AspNetCoreIdentity
using Microsoft.AspNetCore.Identity;
#else
using Microsoft.AspNet.Identity.EntityFramework;
#endif
namespace System.Linq.Dynamic.Core.Tests
{
public partial class QueryableTests
{
[DynamicLinqType]
public class ClassWithItem
{
public string? Item { get; set; }
public int Value { get; set; }
}
[DynamicLinqType]
public class Example
{
public int Field;
public DateTime Time { get; set; }
public DateTime? TimeNull { get; set; }
public DayOfWeek? DOWNull { get; set; }
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }
[DynamicLinqType]
public class NestedDto
{
public string Name { get; set; }
[DynamicLinqType]
public class NestedDto2
{
public string Name2 { get; set; }
}
}
}
public class ExampleWithConstructor
{
public DateTime Time { get; set; }
public DayOfWeek? DOWNull { get; set; }
public DayOfWeek DOW { get; set; }
public int Sec { get; set; }
public int? SecNull { get; set; }
// public long X { get; set; }
public ExampleWithConstructor(DateTime t, DayOfWeek? dn, DayOfWeek d, int s, int? sn)
{
Time = t;
DOWNull = dn;
DOW = d;
Sec = s;
SecNull = sn;
}
}
public class ExampleWithConstructor2
{
public int? Value { get; set; }
public ExampleWithConstructor2(bool b)
{
}
public ExampleWithConstructor2(int? value)
{
Value = value;
}
}
/// <summary>
/// Cannot work with property which in base class. https://github.com/StefH/System.Linq.Dynamic.Core/issues/23
/// </summary>
[Fact]
public void Select_Dynamic_PropertyInBaseClass()
{
var queryable = new[] { new IdentityUser("a"), new IdentityUser("b") }.AsQueryable();
var expected = queryable.Select(i => i.Id);
var dynamic = queryable.Select<string>("Id");
Assert.Equal(expected.ToArray(), dynamic.ToArray());
}
[Fact]
public void Select_Dynamic_As_WithDot()
{
// Assign
var qry = User.GenerateSampleModels(1).AsQueryable();
var config = new ParsingConfig
{
SupportDotInPropertyNames = true
};
// Act
var result = qry.Select(config, "new (Profile.FirstName as P.FirstName)").ToDynamicArray();
// Assert
result.Should().HaveCount(1);
}
[Fact]
public void Select_Dynamic1()
{
// Assign
var qry = User.GenerateSampleModels(1).AsQueryable();
// Act
var userRoles1 = qry.Select("new (Roles.Select(Id) as RoleIds)");
var userRoles2 = qry.Select("new (Roles.Select(it.Id) as RoleIds)");
// Assert
Check.That(userRoles1.Count()).IsEqualTo(1);
Check.That(userRoles2.Count()).IsEqualTo(1);
}
[Fact]
public void Select_Dynamic2()
{
// Assign
var qry = User.GenerateSampleModels(1).AsQueryable();
// Act
var userRoles = qry.Select("new (Roles.Select(it).ToArray() as Rolez)");
// Assert
Check.That(userRoles.Count()).IsEqualTo(1);
}
[Fact]
public void Select_Dynamic3()
{
//Arrange
List<int> range = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var testList = User.GenerateSampleModels(100);
var qry = testList.AsQueryable();
//Act
IEnumerable rangeResult = range.AsQueryable().Select("it * it");
var userNames = qry.Select("UserName");
var userFirstName = qry.Select("new (UserName, Profile.FirstName as MyFirstName)");
var userRoles = qry.Select("new (UserName, Roles.Select(Id) AS RoleIds)");
//Assert
Assert.Equal(range.Select(x => x * x).ToArray(), rangeResult.Cast<int>().ToArray());
#if NET35
Assert.Equal(testList.Select(x => x.UserName).ToArray(), userNames.AsEnumerable().Cast<string>().ToArray());
Assert.Equal(
testList.Select(x => "{ UserName = " + x.UserName + ", MyFirstName = " + x.Profile.FirstName + " }").ToArray(),
userFirstName.Cast<object>().Select(x => x.ToString()).ToArray());
Assert.Equal(testList[0].Roles.Select(x => x.Id).ToArray(), Enumerable.ToArray(userRoles.First().GetDynamicProperty<IEnumerable<Guid>>("RoleIds")));
#else
Assert.Equal(testList.Select(x => x.UserName).ToArray(), userNames.Cast<string>().ToArray());
Assert.Equal(
testList.Select(x => "{ UserName = " + x.UserName + ", MyFirstName = " + x.Profile.FirstName + " }").ToArray(),
userFirstName.AsDynamicEnumerable().Select(x => x.ToString()).Cast<string>().ToArray());
Assert.Equal(testList[0].Roles.Select(x => x.Id).ToArray(), Enumerable.ToArray(userRoles.First().RoleIds));
#endif
}
[Fact]
public void Select_Dynamic_Add_Integers()
{
// Arrange
var range = new List<int> { 1, 2 };
// Act
IEnumerable rangeResult = range.AsQueryable().Select("it + 1");
// Assert
Assert.Equal(range.Select(x => x + 1).ToArray(), rangeResult.Cast<int>().ToArray());
}
[Fact]
public void Select_Dynamic_Add_Strings()
{
// Arrange
var range = new List<string> { "a", "b" };
// Act
IEnumerable rangeResult = range.AsQueryable().Select("it + \"c\"");
// Assert
Assert.Equal(range.Select(x => x + "c").ToArray(), rangeResult.Cast<string>().ToArray());
}
[Fact]
public void Select_Dynamic_Add_DayOfWeekEnum_And_Integer()
{
// Arrange
var range = new DayOfWeek[] { DayOfWeek.Monday };
// Act
var rangeResult = range.AsQueryable().Select("it + 1");
// Assert
Assert.Equal(range.Select(x => x + 1).ToArray(), rangeResult.Cast<DayOfWeek>().ToArray());
}
[Fact]
public void Select_Dynamic_Add_Integer_And_DayOfWeekEnum()
{
// Arrange
var range = new int[] { 1 };
// Act
var rangeResult = range.AsQueryable().Select("it + DayOfWeek.Monday");
// Assert
Assert.Equal(range.Select(x => x + DayOfWeek.Monday).Cast<int>().ToArray(), rangeResult.Cast<int>().ToArray());
}
[Fact]
public void Select_Dynamic_WithIncludes()
{
// Arrange
var qry = new List<Entities.Employee>().AsQueryable();
// Act
string includesX =
", it.Company as TEntity__Company, it.Company.MainCompany as TEntity__Company_MainCompany, it.Country as TEntity__Country, it.Function as TEntity__Function, it.SubFunction as TEntity__SubFunction";
string select = $"new (\"__Key__\" as __Key__, it AS TEntity__{includesX})";
var userNames = qry.Select(select).ToDynamicList();
// Assert
Assert.NotNull(userNames);
}
[Fact]
public void Select_Dynamic_WithPropertyVisitorAndQueryInterceptor()
{
var testList = new List<Entities.Employee>
{
new Entities.Employee {FirstName = "first", LastName = "last"}
};
var qry = testList.AsEnumerable().AsQueryable().InterceptWith(new PropertyVisitor());
var dynamicSelect = qry.Select("new (FirstName, LastName, FullName)").ToDynamicList();
Assert.NotNull(dynamicSelect);
Assert.Single(dynamicSelect);
var firstEmployee = dynamicSelect.FirstOrDefault();
Assert.NotNull(firstEmployee);
Assert.Equal("first last", firstEmployee.FullName);
}
[Fact]
public void Select_Dynamic_TResult()
{
//Arrange
List<int> range = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var testList = User.GenerateSampleModels(100);
var qry = testList.AsQueryable();
//Act
IEnumerable rangeResult = range.AsQueryable().Select<int>("it * it").ToList();
var userNames = qry.Select<string>("UserName").ToList();
var userProfiles = qry.Select<UserProfile>("Profile").ToList();
//Assert
Assert.Equal(range.Select(x => x * x).ToList(), rangeResult);
Assert.Equal(testList.Select(x => x.UserName).ToList(), userNames);
Assert.Equal(testList.Select(x => x.Profile).ToList(), userProfiles);
}
[Fact]
public void Select_Dynamic_IntoType()
{
//Arrange
List<int> range = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var testList = User.GenerateSampleModels(10);
var qry = testList.AsQueryable();
//Act
IEnumerable rangeResult = range.AsQueryable().Select(typeof(int), "it * it");
var userNames = qry.Select(typeof(string), "UserName");
var userProfiles = qry.Select(typeof(UserProfile), "Profile");
//Assert
Assert.Equal(range.Select(x => x * x).Cast<object>().ToList(), rangeResult.ToDynamicList());
Assert.Equal(testList.Select(x => x.UserName).Cast<object>().ToList(), userNames.ToDynamicList());
Assert.Equal(testList.Select(x => x.Profile).Cast<object>().ToList(), userProfiles.ToDynamicList());
}
[Fact]
public void Select_Dynamic_IntoTypeWithNullableProperties1()
{
// Arrange
var dates = Enumerable.Repeat(0, 7)
.Select((d, i) => new DateTime(2000, 1, 1).AddDays(i).AddSeconds(i))
.AsQueryable();
var config = new ParsingConfig { SupportEnumerationsFromSystemNamespace = false };
// Act
IQueryable<Example> result = dates
.Select(d => new Example { Time = d, DOWNull = d.DayOfWeek, DOW = d.DayOfWeek, Sec = d.Second, SecNull = d.Second });
IQueryable<Example> resultDynamic = dates
.Select<Example>(config, "new (it as Time, DayOfWeek as DOWNull, DayOfWeek as DOW, Second as Sec, int?(Second) as SecNull)");
// Assert
Check.That(resultDynamic.First()).Equals(result.First());
Check.That(resultDynamic.Last()).Equals(result.Last());
}
[Fact]
public void Select_Dynamic_IntoTypeWithNullableProperties2()
{
// Arrange
var dates = Enumerable.Repeat(0, 7)
.Select((d, i) => new DateTime(2000, 1, 1).AddDays(i).AddSeconds(i))
.AsQueryable();
var config = new ParsingConfig { SupportEnumerationsFromSystemNamespace = false };
// Act
IQueryable<ExampleWithConstructor> result = dates
.Select(d => new ExampleWithConstructor(d, d.DayOfWeek, d.DayOfWeek, d.Second, d.Second));
IQueryable<ExampleWithConstructor> resultDynamic = dates
.Select<ExampleWithConstructor>(config, "new (it as Time, DayOfWeek as DOWNull, DayOfWeek as DOW, Second as Sec, int?(Second) as SecNull)");
// Assert
Check.That(resultDynamic.First()).Equals(result.First());
Check.That(resultDynamic.Last()).Equals(result.Last());
}
[Fact]
public void Select_Dynamic_IntoTypeWithNullableParameterInConstructor()
{
// Arrange
var values = Enumerable.Repeat(0, 3).AsQueryable();
var config = new ParsingConfig { AllowNewToEvaluateAnyType = true };
// Act
var result = values.Select(v => new ExampleWithConstructor2(v));
var resultDynamic = values.Select<ExampleWithConstructor2>(config, "new (it as value)");
// Assert
Check.That(resultDynamic.First()).Equals(result.First());
Check.That(resultDynamic.Last()).Equals(result.Last());
}
[Fact]
public void Select_Dynamic_SystemType1()
{
// Arrange
var config = new ParsingConfig
{
AllowNewToEvaluateAnyType = true
};
config.UseDefaultDynamicLinqCustomTypeProvider([typeof(DirectoryInfo)]);
var queryable = new[] { "test" }.AsQueryable();
// Act
var result = queryable.Select(config, $"new {typeof(DirectoryInfo).FullName}(~ as path)").ToDynamicArray().First();
// Assert
Assert.NotNull(result);
Assert.Equal(result.Name, "test");
}
[Fact]
public void Select_Dynamic_SystemType2()
{
// Arrange
var config = new ParsingConfig { AllowNewToEvaluateAnyType = true };
var queryable = new[] { "test" }.AsQueryable();
// Act
var result = queryable.Select<DirectoryInfo>(config, "new (~ as path)").ToDynamicArray().First();
// Assert
Assert.NotNull(result);
Assert.Equal(result.Name, "test");
}
[Fact]
public void Select_Dynamic_WithField()
{
// Arrange
var config = new ParsingConfig { AllowNewToEvaluateAnyType = true };
var queryable = new List<int> { 1, 2 }.AsQueryable();
// Act
var projectedData = queryable.Select<Example>(config, $"new {typeof(Example).FullName}(~ as Field)");
// Assert
Check.That(projectedData.First().Field).Equals(1);
Check.That(projectedData.Last().Field).Equals(2);
}
[Fact]
public void Select_Dynamic_IntoKnownNestedType()
{
// Arrange
var config = new ParsingConfig { AllowNewToEvaluateAnyType = true };
var queryable = new List<string> { "name1", "name2" }.AsQueryable();
// Act
var projectedData = queryable.Select<Example.NestedDto>(config, $"new {typeof(Example.NestedDto).FullName}(~ as Name)");
// Assert
Check.That(projectedData.First().Name).Equals("name1");
Check.That(projectedData.Last().Name).Equals("name2");
}
[Fact]
public void Select_Dynamic_IntoKnownNestedTypeSecondLevel()
{
// Arrange
var config = new ParsingConfig { AllowNewToEvaluateAnyType = true };
var queryable = new List<string> { "name1", "name2" }.AsQueryable();
// Act
var projectedData = queryable.Select<Example.NestedDto.NestedDto2>(config, $"new {typeof(Example.NestedDto.NestedDto2).FullName}(~ as Name2)");
// Assert
Check.That(projectedData.First().Name2).Equals("name1");
Check.That(projectedData.Last().Name2).Equals("name2");
}
[Fact]
public void Select_Dynamic_RenameParameterExpression_Is_False()
{
// Arrange
var config = new ParsingConfig
{
RenameParameterExpression = false
};
var queryable = new int[0].AsQueryable();
// Act
string result = queryable.Select<int>(config, "it * it").ToString();
// Assert
Check.That(result).Equals("System.Int32[].Select(Param_0 => (Param_0 * Param_0))");
}
[Fact]
public void Select_Dynamic_RenameParameterExpression_Is_True()
{
// Arrange
var config = new ParsingConfig
{
RenameParameterExpression = true
};
var queryable = new int[0].AsQueryable();
// Act
string result = queryable.Select<int>(config, "it * it").ToString();
// Assert
Check.That(result).Equals("System.Int32[].Select(it => (it * it))");
}
#if NET461 || NET5_0 || NET6_0 || NET7_0 || NET8_0 || NET9_0
[Fact(Skip = "Fails sometimes in GitHub CI build")]
#else
[Fact]
#endif
public void Select_Dynamic_JObject_With_Array_Should_Use_Correct_Indexer()
{
// Arrange
var j = new JObject
{
{"I", new JValue(9)},
{"A", new JArray(new[] {1,2,3}) },
{"L", new JValue(5)}
};
var queryable = new[] { j }.AsQueryable();
// Act
var result = queryable.Select("new (long(I) as I, (new [] { long(A[0]), long(A[1]), long(A[2])}) as A, long(L) as L)").ToDynamicArray().First();
// Assert
Assert.Equal(9, result.I);
Assert.Equal(5, result.L);
Assert.Equal(1, result.A[0]);
Assert.Equal(2, result.A[1]);
Assert.Equal(3, result.A[2]);
}
[Fact]
public void Select_Dynamic_ReservedKeyword()
{
// Arrange
var queryable = new[] { new { Id = 1, Guid = "a" } }.AsQueryable();
// Act
var result = queryable.Select("new (Id, @Guid, 42 as Answer)").ToDynamicArray();
// Assert
Check.That(result).IsNotNull();
}
[Fact]
public void Select_Dynamic_Exceptions()
{
//Arrange
var testList = User.GenerateSampleModels(100, allowNullableProfiles: true);
var qry = testList.AsQueryable();
//Act
Assert.Throws<ParseException>(() => qry.Select("Bad"));
Assert.Throws<ParseException>(() => qry.Select("Id, UserName"));
Assert.Throws<ParseException>(() => qry.Select("new Id, UserName"));
Assert.Throws<ParseException>(() => qry.Select("new (Id, UserName"));
Assert.Throws<ParseException>(() => qry.Select("new (Id, UserName, Bad)"));
Check.ThatCode(() => qry.Select<User>("new User(it.Bad as Bad)")).Throws<ParseException>().WithMessage("No property or field 'Bad' exists in type 'User'");
Assert.Throws<ArgumentNullException>(() => DynamicQueryableExtensions.Select(null, "Id"));
Assert.Throws<ArgumentNullException>(() => qry.Select(null));
Assert.Throws<ArgumentException>(() => qry.Select(""));
Assert.Throws<ArgumentException>(() => qry.Select(" "));
}
[Fact]
public void Select_Dynamic_Nested_With_SubString()
{
// Arrange
var users = new User[]
{
new() { Id = Guid.NewGuid(), UserName = "Luke Skywalker"},
new() { Id = Guid.NewGuid(), UserName = "Darth Vader"},
new() { Id = Guid.NewGuid(), UserName = "Han Solo"}
};
var queryable = users.AsQueryable();
// Act
var result = queryable.Select(x => x.UserName.Substring(0, x.UserName.IndexOf(" "))).ToArray();
var resultDynamic = queryable.Select("UserName.Substring(0, UserName.IndexOf(\" \"))").ToDynamicArray<string>();
// Assert
resultDynamic.Should().BeEquivalentTo(result);
}
// 845
[Theory]
[InlineData("it + \"txt\" & 99", "_a_txt99")]
[InlineData("it & \"txt\" + 99", "_a_txt99")]
[InlineData("99 + it & \"txt\"", "99_a_txt")]
[InlineData("99 & it + \"txt\"", "99_a_txt")]
public void Select_Dynamic_StringConcatDifferentTypes(string expression, string expectedResult)
{
// Arrange
var config = new ParsingConfig
{
ConvertObjectToSupportComparison = true
};
var queryable = new[] { "_a_" }.AsQueryable();
// Act
queryable.Select(config, expression).ToDynamicArray<string>()[0].Should().Be(expectedResult);
}
[Fact]
public void Select_Dynamic_ClassWithItemProperty()
{
// Arrange
var data = new []
{
new ClassWithItem { Item = "Value1", Value = 1 },
new ClassWithItem { Item = "Value2", Value = 2 }
};
var queryable = data.AsQueryable();
// Act
var result = queryable.Select(x => new {x.Item, x.Value }).ToArray();
var resultDynamic = queryable.Select("new (Item, Value)").ToDynamicArray();
// Assert
resultDynamic.Should().BeEquivalentTo(result);
}
}
}