-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpressionFunctionTests.cs
More file actions
226 lines (199 loc) · 7.47 KB
/
ExpressionFunctionTests.cs
File metadata and controls
226 lines (199 loc) · 7.47 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
using ObjectSemantics.NET.Tests.MoqModels;
using System.Collections.Generic;
using Xunit;
namespace ObjectSemantics.NET.Tests
{
public class ExpressionFunctionTests
{
[Fact]
public void Should_Sum_Collection_Path()
{
OrderEmailModel model = new OrderEmailModel
{
Customer = new OrderCustomer
{
Payments = new List<OrderPayment>
{
new OrderPayment { Amount = 1000 },
new OrderPayment { Amount = 2000.75m },
new OrderPayment { Amount = 999.25m }
}
}
};
string result = model.Map("{{ __sum(Customer.Payments.Amount):N2 }}");
Assert.Equal("4,000.00", result);
}
[Fact]
public void Should_Average_Collection_Path_With_Single_Underscore()
{
OrderEmailModel model = new OrderEmailModel
{
Customer = new OrderCustomer
{
Payments = new List<OrderPayment>
{
new OrderPayment { PaidAmount = 300 },
new OrderPayment { PaidAmount = 900 },
new OrderPayment { PaidAmount = 1200 }
}
}
};
string result = model.Map("{{ _avg(Customer.Payments.PaidAmount):N2 }}");
Assert.Equal("800.00", result);
}
[Fact]
public void Should_Calculate_Property_Expression()
{
OrderEmailModel model = new OrderEmailModel
{
PaidAmount = 10000,
Customer = new OrderCustomer
{
CreditLimit = 4500
}
};
string result = model.Map("{{ __calc(PaidAmount - Customer.CreditLimit):N2 }}");
Assert.Equal("5,500.00", result);
}
[Fact]
public void Should_Calculate_Expression_Including_Parentheses_And_Decimals()
{
OrderEmailModel model = new OrderEmailModel
{
Subtotal = 1000,
Tax = 160
};
string result = model.Map("{{ __calc((Subtotal + Tax) * 0.5):N2 }}");
Assert.Equal("580.00", result);
}
[Fact]
public void Should_Calculate_Count_Min_And_Max_Aggregates()
{
OrderEmailModel model = new OrderEmailModel
{
Customer = new OrderCustomer
{
Payments = new List<OrderPayment>
{
new OrderPayment { Amount = 700 },
new OrderPayment { Amount = 200 },
new OrderPayment { Amount = 1100 }
}
}
};
string template = "{{ __count(Customer.Payments.Amount) }}|{{ __min(Customer.Payments.Amount):N2 }}|{{ __max(Customer.Payments.Amount):N2 }}";
string result = model.Map(template);
Assert.Equal("3|200.00|1,100.00", result);
}
[Fact]
public void Should_Calculate_Per_Row_Expression_Inside_Loop()
{
OrderEmailModel model = new OrderEmailModel
{
Items = new List<OrderLineItem>
{
new OrderLineItem { Quantity = 2, UnitPrice = 350 },
new OrderLineItem { Quantity = 3, UnitPrice = 100.5m }
}
};
string template = "{{ #foreach(Items) }}[{{ __calc(Quantity * UnitPrice):N2 }}]{{ #endforeach }}";
string result = model.Map(template);
Assert.Equal("[700.00][301.50]", result);
}
[Fact]
public void Should_Render_Empty_When_Calc_Expression_Is_Invalid()
{
OrderEmailModel model = new OrderEmailModel
{
PaidAmount = 5000,
Customer = new OrderCustomer
{
CreditLimit = 1000
}
};
string result = model.Map("A{{ __calc(PaidAmount - UnknownValue):N2 }}B");
Assert.Equal("AB", result);
}
[Fact]
public void Should_Render_Empty_For_Unknown_Aggregate_Path()
{
OrderEmailModel model = new OrderEmailModel
{
Customer = new OrderCustomer
{
Payments = new List<OrderPayment>
{
new OrderPayment { Amount = 100 }
}
}
};
string result = model.Map("{{ __sum(Customer.UnknownPayments.Amount) }}|{{ __avg(Customer.UnknownPayments.Amount) }}|{{ __count(Customer.UnknownPayments.Amount) }}|{{ __min(Customer.UnknownPayments.Amount) }}|{{ __max(Customer.UnknownPayments.Amount) }}");
Assert.Equal("||||", result);
}
[Fact]
public void Should_Return_Zero_For_Aggregates_When_Source_Is_Null()
{
OrderEmailModel model = new OrderEmailModel
{
Customer = null
};
string result = model.Map("{{ __sum(Customer.Payments.Amount) }}|{{ __avg(Customer.Payments.Amount) }}|{{ __count(Customer.Payments.Amount) }}|{{ __min(Customer.Payments.Amount) }}|{{ __max(Customer.Payments.Amount) }}");
Assert.Equal("0|0|0|0|0", result);
}
[Fact]
public void Should_Return_Zero_When_Calc_Uses_Null_Operand_Path()
{
OrderEmailModel model = new OrderEmailModel
{
PaidAmount = 5000,
Customer = null
};
string result = model.Map("A{{ __calc(PaidAmount - Customer.CreditLimit) }}B");
Assert.Equal("A0B", result);
}
[Fact]
public void Should_Support_Expressions_Without_Number_Format()
{
OrderEmailModel model = new OrderEmailModel
{
PaidAmount = 10000,
Customer = new OrderCustomer
{
CreditLimit = 4500,
Payments = new List<OrderPayment>
{
new OrderPayment { Amount = 1000 },
new OrderPayment { Amount = 2000 },
new OrderPayment { Amount = 1500 }
}
}
};
string result = model.Map("{{ __sum(Customer.Payments.Amount) }}|{{ __calc(PaidAmount - Customer.CreditLimit) }}");
Assert.Equal("4500|5500", result);
}
[Fact]
public void Should_Render_Empty_When_Sum_Path_Is_String_Based()
{
OrderEmailModel model = new OrderEmailModel
{
Items = new List<OrderLineItem>
{
new OrderLineItem { Name = "Keyboard" },
new OrderLineItem { Name = "Mouse" }
}
};
string result = model.Map("X{{ __sum(Items.Name) }}Y");
Assert.Equal("XY", result);
}
[Fact]
public void Should_Render_Empty_When_Sum_Path_Is_Date_Based()
{
OrderEmailModel model = new OrderEmailModel
{
OrderDate = new System.DateTime(2026, 03, 07)
};
string result = model.Map("X{{ __sum(OrderDate) }}Y");
Assert.Equal("XY", result);
}
}
}