C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Test_MonetaryUnit.cs
Go to the documentation of this file.
1using System;
2using System.Globalization;
3
4using AnalyzeRe;
7
8#if MSTEST
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10#elif NUNIT
11using NUnit.Framework;
12using TestClass = NUnit.Framework.TestFixtureAttribute;
13using TestMethod = NUnit.Framework.TestAttribute;
14using TestCategory = NUnit.Framework.CategoryAttribute;
15#endif
16
18{
23 [TestClass]
24 public sealed class TestSuite_MonetaryUnit : BaseMutableResourceTestSuite<SurplusShare>
25 {
26 #region Set Up and Configuration
27 private const string TypeName = "MonetaryUnit";
28 protected override IInjectableResource<SurplusShare>
30 #endregion Set Up and Configuration
31
32 #region ToString
33 [TestMethod, TestCategory(TypeName)]
35 {
36 string t = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator;
37 string d = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
38 string neg = CultureInfo.CurrentCulture.NumberFormat.NegativeSign;
39
40 // Simple monetary units for sane amounts
41 Assert.AreEqual($"USD 123{d}45",
42 new MonetaryUnit(123.45, "USD").ToString());
43 Assert.AreEqual($"USD 12{t}345{t}678{t}901{d}57",
44 new MonetaryUnit(12345678901.5678, "USD").ToString());
45 Assert.AreEqual($"USD 0{d}01234",
46 new MonetaryUnit(0.01234, "USD").ToString());
47
48 // No currency is okay
49 Assert.AreEqual($"123{d}45",
50 new MonetaryUnit(123.45, null).ToString());
51
52 // Small amounts, very large numbers, and special values
53 Assert.AreEqual($"CAD 1{d}234E{neg}06",
54 new MonetaryUnit(0.000001234, "CAD").ToString());
55 Assert.AreEqual($"CAD 1{d}2345678E+55",
56 new MonetaryUnit(1.2345678e55, "CAD").ToString());
57 Assert.AreEqual($"CAD 1{d}79769313486232E+308",
58 new MonetaryUnit(Double.MaxValue, "CAD").ToString());
59 Assert.AreEqual($"CAD {CultureInfo.CurrentCulture.NumberFormat.PositiveInfinitySymbol}",
60 new MonetaryUnit(Double.PositiveInfinity, "CAD").ToString());
61
62 // Fixed date and fixed rate monetary units.
63 DateTime aTime = new DateTime(2017, 8, 16, 1, 2, 3, DateTimeKind.Utc);
64 Assert.AreEqual($"USD 123{d}45 FX rate fixed to 2017-08-16T01:02:03.0000000Z",
65 new MonetaryUnit(123.45, "USD", aTime).ToString());
66 Assert.AreEqual($"USD 123{d}45 FX rate fixed to EUR 0{d}998877",
67 new MonetaryUnit(123.45, "USD", 0.998877, "EUR").ToString());
68 // The user wouldn't allow this so the string representation doesn't matter too much
69 Assert.AreEqual($"123{d}45 FX rate fixed to 1{d}3344",
70 new MonetaryUnit(123.45, null, 1.3344, null).ToString());
71
72 // Negative amounts should follow the same behaviour
73 Assert.AreEqual($"USD {neg}123{d}45",
74 new MonetaryUnit(-123.45, "USD").ToString());
75 Assert.AreEqual($"USD {neg}12{t}345{t}678{t}901{d}57",
76 new MonetaryUnit(-12345678901.5678, "USD").ToString());
77 Assert.AreEqual($"USD {neg}0{d}01234",
78 new MonetaryUnit(-0.01234, "USD").ToString());
79 Assert.AreEqual($"CAD {neg}1{d}234E{neg}06",
80 new MonetaryUnit(-0.000001234, "CAD").ToString());
81 Assert.AreEqual($"CAD {neg}1{d}2345678E+55",
82 new MonetaryUnit(-1.2345678e55, "CAD").ToString());
83 Assert.AreEqual($"CAD {neg}1{d}79769313486232E+308",
84 new MonetaryUnit(Double.MinValue, "CAD").ToString());
85 Assert.AreEqual($"CAD {CultureInfo.CurrentCulture.NumberFormat.NegativeInfinitySymbol}",
86 new MonetaryUnit(Double.NegativeInfinity, "CAD").ToString());
87
88 // Representation of NaN isn't so important, just ensuring it doesn't throw
89 Assert.AreEqual($"USD {CultureInfo.CurrentCulture.NumberFormat.NaNSymbol}",
90 new MonetaryUnit(Double.NaN, "USD").ToString());
91 }
92 #endregion ToString
93
94 #region Test Helper Methods
95 private void TestPOSTValue(double value, bool shouldSucceed)
96 {
97 PUT_WithValue(l => l.premium, new MonetaryUnit(value, "USD"), shouldSucceed);
98 }
99 private void TestPOSTCurrency(string currency, bool shouldSucceed)
100 {
101 POST_WithValue(l => l.premium, new MonetaryUnit(0, currency), shouldSucceed);
102 }
103 private void TestPUTValue(double value, bool shouldSucceed)
104 {
105 PUT_WithValue(l => l.premium, new MonetaryUnit(value, "USD"), shouldSucceed);
106 }
107 private void TestPUTCurrency(string currency, bool shouldSucceed)
108 {
109 PUT_WithValue(l => l.premium, new MonetaryUnit(0, currency), shouldSucceed);
110 }
111 #endregion Test Helper Methods
112
113 #region POST
114 #region MonetaryUnit.value
115 [TestMethod, TestCategory(TypeName)]
117 {
118 TestPOSTValue(Double.MaxValue, true);
119 }
120 [TestMethod, TestCategory(TypeName)]
122 {
123 TestPOSTValue(Double.MinValue, false);
124 }
125 [TestMethod, TestCategory(TypeName)]
127 {
128 TestPOSTValue(0d, true);
129 }
130 [TestMethod, TestCategory(TypeName)]
132 {
133 TestPOSTValue(-1d, false);
134 }
135 [TestMethod, TestCategory(TypeName)]
137 {
138 TestPOSTValue(Double.NaN, false);
139 }
140 [TestMethod, TestCategory(TypeName)]
142 {
143 TestPOSTValue(Double.PositiveInfinity, false);
144 }
145 [TestMethod, TestCategory(TypeName)]
147 {
148 TestPOSTValue(Double.NegativeInfinity, false);
149 }
150 [TestMethod, TestCategory(TypeName)]
152 {
153 TestPOSTValue(Math.PI / 1E12, true);
154 }
155 #endregion MonetaryUnit.value
156
157 #region MonetaryUnit.currency
158 [TestMethod, TestCategory(TypeName)]
160 {
161 TestPOSTCurrency(null, false);
162 }
163 [TestMethod, TestCategory(TypeName)]
165 {
166 TestPOSTCurrency("", false);
167 }
168 [TestMethod, TestCategory(TypeName)]
170 {
171 TestPOSTCurrency("AU", false);
172 }
173 [TestMethod, TestCategory(TypeName)]
175 {
176 TestPOSTCurrency("asdf", false);
177 }
178 [TestMethod, TestCategory(TypeName)]
180 {
181 TestPOSTCurrency("EUR", true);
182 }
183 [TestMethod, TestCategory(TypeName)]
185 {
186 TestPOSTCurrency("AAA", true);
187 }
188 #endregion MonetaryUnit.currency
189
190 #region MonetaryUnit.value_date
191 [TestMethod, TestCategory(TypeName)]
193 {
194 POST_WithValue(l => l.premium, new MonetaryUnit(0, "USD",
195 new DateTime(2015, 01, 02, 3, 4, 5)), true);
196 }
197 #endregion MonetaryUnit.value_date
198
199 #region MonetaryUnit.rate
200 [TestMethod, TestCategory(TypeName)]
202 {
203 POST_WithValue(l => l.premium, new MonetaryUnit(0, "USD", 1.23, "CAD"), true);
204 }
205
206 [TestMethod, TestCategory(TypeName)]
208 {
209 POST_WithValue(l => l.premium, new MonetaryUnit(0, "USD", 0, "CAD"), true);
210 }
211
212 [TestMethod, TestCategory(TypeName)]
214 {
215 POST_WithValue(l => l.premium, new MonetaryUnit(0, "USD", -1.23, "CAD"), false);
216 }
217
218 [TestMethod, TestCategory(TypeName)]
220 {
221 POST_WithValue(l => l.premium, new MonetaryUnit(0, "USD", 1.23, null), false);
222 }
223 #endregion MonetaryUnit.rate
224
225 #region MonetaryUnit.rate and .value_date
227 [TestMethod, TestCategory(TypeName)]
229 {
230 MonetaryUnit toSupply = new MonetaryUnit(0, "USD", 1.23, "CAD")
231 {
232 // Set the value_date property (shouldn't be supported if rate was set)
233 value_date = new DateTime(2015, 01, 02, 3, 4, 5)
234 };
235 POST_WithValue(l => l.premium, toSupply, false);
236 }
237 #endregion MonetaryUnit.rate and .value_date
238 #endregion POST
239
240 #region PUT
241 #region MonetaryUnit.value
242 [TestMethod, TestCategory(TypeName)]
244 {
245 TestPUTValue(Double.MaxValue, true);
246 }
247 [TestMethod, TestCategory(TypeName)]
249 {
250 TestPUTValue(Double.MinValue, false);
251 }
252 [TestMethod, TestCategory(TypeName)]
254 {
255 TestPUTValue(0d, true);
256 }
257 [TestMethod, TestCategory(TypeName)]
259 {
260 TestPUTValue(-1d, false);
261 }
262 [TestMethod, TestCategory(TypeName)]
264 {
265 TestPUTValue(Double.NaN, false);
266 }
267 [TestMethod, TestCategory(TypeName)]
269 {
270 TestPUTValue(Double.PositiveInfinity, false);
271 }
272 [TestMethod, TestCategory(TypeName)]
274 {
275 TestPUTValue(Double.NegativeInfinity, false);
276 }
277 [TestMethod, TestCategory(TypeName)]
279 {
280 TestPUTValue(Math.PI / 1E12, true);
281 }
282 #endregion MonetaryUnit.value
283
284 #region MonetaryUnit.currency
285 [TestMethod, TestCategory(TypeName)]
287 {
288 // Monetary Unit structures are updated atomically - which means the new
289 // object must specify all required values. Thus, currency cannot be omitted.
290 TestPUTCurrency(null, false);
291 }
292 [TestMethod, TestCategory(TypeName)]
294 {
295 TestPUTCurrency("", false);
296 }
297 [TestMethod, TestCategory(TypeName)]
299 {
300 TestPUTCurrency("AU", false);
301 }
302 [TestMethod, TestCategory(TypeName)]
304 {
305 TestPUTCurrency("asdf", false);
306 }
307 [TestMethod, TestCategory(TypeName)]
309 {
310 TestPUTCurrency("EUR", true);
311 }
312 [TestMethod, TestCategory(TypeName)]
314 {
315 TestPUTCurrency("AAA", true);
316 }
317 #endregion MonetaryUnit.currency
318
319 #region MonetaryUnit.value_date
320 [TestMethod, TestCategory(TypeName)]
322 {
323 PUT_WithValue(l => l.premium, new MonetaryUnit(0, "USD",
324 new DateTime(2015, 01, 02, 3, 4, 5)), true);
325 }
326 #endregion MonetaryUnit.value_date
327
328 #region MonetaryUnit.rate
329 [TestMethod, TestCategory(TypeName)]
331 {
332 PUT_WithValue(l => l.premium, new MonetaryUnit(0, "USD", 1.23, "CAD"), true);
333 }
334
335 [TestMethod, TestCategory(TypeName)]
337 {
338 PUT_WithValue(l => l.premium, new MonetaryUnit(0, "USD", 0, "CAD"), true);
339 }
340
341 [TestMethod, TestCategory(TypeName)]
343 {
344 PUT_WithValue(l => l.premium, new MonetaryUnit(0, "USD", -1.23, "CAD"), false);
345 }
346 #endregion MonetaryUnit.rate
347
348 #region MonetaryUnit.rate and .value_date
350 [TestMethod, TestCategory(TypeName)]
352 {
353 MonetaryUnit toSupply = new MonetaryUnit(0, "USD", 1.23, "CAD")
354 {
355 // Set the value_date property (shouldn't be supported if rate was set)
356 value_date = new DateTime(2015, 01, 02, 3, 4, 5)
357 };
358 PUT_WithValue(l => l.premium, toSupply, false);
359 }
360 #endregion MonetaryUnit.rate and .value_date
361 #endregion PUT
362
363 // TODO: Test application of exchange rates including behaviour around override rate/date
364 }
365}
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
IInjectableResource< SurplusShare > Layer_SurplusShare
Test the MonetaryUnit by playing with the premium on a Layer_WithTerms object. This is a necessary sp...
override IInjectableResource< SurplusShare > TestInjectableResource
void Test_MonetaryUnit_PUT_DateAndRate()
Date and Rate are mutually exclusive.
void Test_MonetaryUnit_POST_DateAndRate()
Date and Rate are mutually exclusive.
Representation of a monetary value with a currency.
override string ToString()
String representation of this MonetaryUnit's currency and value, and of its fixed exchange rate prope...
Interface for a class containing a resource that can be Posted with dependencies.