-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathDynamicGetMemberBinderTests.cs
More file actions
172 lines (139 loc) · 6.02 KB
/
DynamicGetMemberBinderTests.cs
File metadata and controls
172 lines (139 loc) · 6.02 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
using System.Data;
using System.Linq.Dynamic.Core.Tests.TestHelpers;
using FluentAssertions;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests;
public class DynamicGetMemberBinderTests
{
public class SalesData
{
public string Region { get; set; } = null!;
public string Product { get; set; } = null!;
public string Sales { get; set; } = null!;
}
public class GroupedSalesData
{
public string Region { get; set; } = null!;
public string? Product { get; set; }
public int TotalSales { get; set; }
public int GroupLevel { get; set; }
}
[Fact]
public void DynamicGetMemberBinder_SelectOnArrayWithComplexObjects()
{
// Arrange
var rows = new SalesData[]
{
new() { Region = "North", Product = "Widget", Sales = "100" },
new() { Region = "North", Product = "Gadget", Sales = "150" },
new() { Region = "South", Product = "Widget", Sales = "200" },
new() { Region = "South", Product = "Gadget", Sales = "100" },
new() { Region = "North", Product = "Widget", Sales = "50" }
}.AsQueryable();
// Act
var grouping1 = rows
.GroupBy("new (Region, Product)")
.Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
var grouping2 = rows
.GroupBy("Region")
.Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = grouping1.ToDynamicArray().Concat(grouping2.ToDynamicArray()).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
// Assert
ordered.Should().HaveCount(6);
}
[Fact]
public void DynamicGetMemberBinder_SelectTypeOnArrayWithComplexObjects()
{
// Arrange
var rows = new SalesData[]
{
new() { Region = "North", Product = "Widget", Sales = "100" },
new() { Region = "North", Product = "Gadget", Sales = "150" },
new() { Region = "South", Product = "Widget", Sales = "200" },
new() { Region = "South", Product = "Gadget", Sales = "100" },
new() { Region = "North", Product = "Widget", Sales = "50" }
}.AsQueryable();
// Act
var grouping1 = rows
.GroupBy("new (Region, Product)")
.Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
var grouping2 = rows
.GroupBy("Region")
.Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = grouping1.Concat(grouping2).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
// Assert
ordered.Should().HaveCount(6);
}
[SkipIfGitHubActionsFact]
public void DynamicGetMemberBinder_SelectOnDataTable()
{
// Arrange
var dataTable = new DataTable();
dataTable.Columns.Add("Region", typeof(string));
dataTable.Columns.Add("Product", typeof(string));
dataTable.Columns.Add("Sales", typeof(int));
dataTable.Rows.Add("North", "Apples", 100);
dataTable.Rows.Add("North", "Oranges", 150);
dataTable.Rows.Add("South", "Apples", 200);
dataTable.Rows.Add("South", "Oranges", 250);
var rows = dataTable.Rows.Cast<DataRow>().AsQueryable();
// Act
var grouping1 = rows
.GroupBy("new (Region, Product)")
.Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
var grouping2 = rows
.GroupBy("Region")
.Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = grouping1.ToDynamicArray().Concat(grouping2.ToDynamicArray()).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
// Assert
ordered.Should().HaveCount(6);
}
[SkipIfGitHubActionsFact]
public void DynamicGetMemberBinder_SelectTypeOnDataTable()
{
// Arrange
var dataTable = new DataTable();
dataTable.Columns.Add("Region", typeof(string));
dataTable.Columns.Add("Product", typeof(string));
dataTable.Columns.Add("Sales", typeof(int));
dataTable.Rows.Add("North", "Apples", 100);
dataTable.Rows.Add("North", "Oranges", 150);
dataTable.Rows.Add("South", "Apples", 200);
dataTable.Rows.Add("South", "Oranges", 250);
var rows = dataTable.Rows.Cast<DataRow>().AsQueryable();
// Act
var grouping1 = rows
.GroupBy("new (Region, Product)")
.Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
var grouping2 = rows
.GroupBy("Region")
.Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = grouping1.ToDynamicArray().Concat(grouping2.ToDynamicArray()).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
// Assert
ordered.Should().HaveCount(6);
}
[Fact]
public void DynamicGetMemberBinder_SelectOnArrayWithIntegers()
{
// Arrange
var dynamicData = new[] { 1, 2 }
.AsQueryable()
.Select("new { it * 2 as Value }")
.ToDynamicArray()
.AsQueryable();
// Act
var dynamicResult1 = dynamicData
.Select("Value")
.ToDynamicArray();
var dynamicResult2 = dynamicData
.Select("Value")
.ToDynamicArray<int>();
// Assert
dynamicResult1.Should().HaveCount(2);
dynamicResult2.Should().BeEquivalentTo([2, 4]);
}
}