forked from zzzprojects/System.Linq.Dynamic.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryableTests.ThenBy.cs
More file actions
59 lines (50 loc) · 2.15 KB
/
QueryableTests.ThenBy.cs
File metadata and controls
59 lines (50 loc) · 2.15 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
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests
{
public partial class QueryableTests
{
[Fact]
public void ThenBy_Dynamic()
{
//Arrange
var testList = User.GenerateSampleModels(100);
var qry = testList.AsQueryable();
//Act
var ordered = qry.OrderBy("Id");
var thenByUserName = ordered.ThenBy("UserName");
var thenByComplex1 = ordered.ThenBy("Profile.Age, Income");
//Assert
Assert.Equal(testList.OrderBy(x => x.Id).ThenBy(x => x.UserName).ToArray(), thenByUserName.ToArray());
Assert.Equal(testList.OrderBy(x => x.Id).ThenBy(x => x.Profile.Age).ThenBy(x => x.Income).ToArray(), thenByComplex1.ToArray());
}
[Fact]
public void ThenBy_Dynamic_AsStringExpression()
{
//Arrange
var testList = User.GenerateSampleModels(100);
var qry = testList.AsQueryable();
//Act
var expected = qry.SelectMany(x => x.Roles.OrderBy(y => y.Name).ThenBy(y => y.Id)).Select(x => x.Name);
var orderById = qry.SelectMany("Roles.OrderBy(Name).ThenBy(Id)").Select("Name");
//Assert
Assert.Equal(expected.ToArray(), orderById.Cast<string>().ToArray());
}
[Fact]
public void ThenBy_Dynamic_Exceptions()
{
//Arrange
var testList = User.GenerateSampleModels(100, allowNullableProfiles: true);
var qry = testList.AsQueryable();
//Act
var ordered = qry.OrderBy("Id");
Assert.Throws<ParseException>(() => ordered.ThenBy("Bad=3"));
Assert.Throws<ParseException>(() => ordered.Where("Id=123"));
Assert.Throws<ArgumentNullException>(() => DynamicQueryableExtensions.ThenBy(null, "Id"));
Assert.Throws<ArgumentNullException>(() => ordered.ThenBy(null));
Assert.Throws<ArgumentException>(() => ordered.ThenBy(""));
Assert.Throws<ArgumentException>(() => ordered.ThenBy(" "));
}
}
}