forked from zzzprojects/System.Linq.Dynamic.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryableTests.GroupBy.cs
More file actions
67 lines (56 loc) · 2.86 KB
/
QueryableTests.GroupBy.cs
File metadata and controls
67 lines (56 loc) · 2.86 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
using NFluent;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using System.Reflection;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests
{
public partial class QueryableTests
{
[Fact]
public void GroupBy_Dynamic()
{
//Arrange
var testList = User.GenerateSampleModels(100);
var qry = testList.AsQueryable();
//Act
var byAgeReturnUserName = qry.GroupBy("Profile.Age", "UserName");
var byAgeReturnAll = qry.GroupBy("Profile.Age");
//Assert
Assert.Equal(testList.GroupBy(x => x.Profile.Age).Count(), byAgeReturnUserName.Count());
Assert.Equal(testList.GroupBy(x => x.Profile.Age).Count(), byAgeReturnAll.Count());
}
// https://github.com/StefH/System.Linq.Dynamic.Core/issues/75
[Fact]
public void GroupBy_Dynamic_Issue75()
{
var testList = User.GenerateSampleModels(100);
var resultDynamic = testList.AsQueryable().GroupBy("Profile.Age").Select("new (it.key as PropertyKey)");
var result = testList.GroupBy(e => e.Profile.Age).Select(e => new { PropertyKey = e.Key }).AsQueryable();
// I think this should be true, but it isn't. dynamicResult add System.Object Item [System.String] property.
PropertyInfo[] properties = result.ElementType.GetTypeInfo().GetProperties();
PropertyInfo[] propertiesDynamic = resultDynamic.ElementType.GetTypeInfo().GetProperties();
Check.That(propertiesDynamic.Length).IsStrictlyGreaterThan(properties.Length);
}
[Fact]
public void GroupBy_Dynamic_Exceptions()
{
//Arrange
var testList = User.GenerateSampleModels(100, allowNullableProfiles: true);
var qry = testList.AsQueryable();
//Act
Assert.Throws<ParseException>(() => qry.GroupBy("Bad"));
Assert.Throws<ParseException>(() => qry.GroupBy("Id, UserName"));
Assert.Throws<ParseException>(() => qry.GroupBy("new Id, UserName"));
Assert.Throws<ParseException>(() => qry.GroupBy("new (Id, UserName"));
Assert.Throws<ParseException>(() => qry.GroupBy("new (Id, UserName, Bad)"));
Assert.Throws<ArgumentNullException>(() => DynamicQueryableExtensions.GroupBy((IQueryable<string>)null, "Id"));
Assert.Throws<ArgumentNullException>(() => qry.GroupBy(null));
Assert.Throws<ArgumentException>(() => qry.GroupBy(""));
Assert.Throws<ArgumentException>(() => qry.GroupBy(" "));
Assert.Throws<ArgumentNullException>(() => qry.GroupBy("Id", (string)null));
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", ""));
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", " "));
}
}
}