Skip to content

Commit dca9f1c

Browse files
committed
Per review, Splitted up unit tests; Add Exception handling as part of Cql operators tied with warning Message
1 parent 645bab5 commit dca9f1c

File tree

3 files changed

+137
-91
lines changed

3 files changed

+137
-91
lines changed

Cql/CoreTests/PrimitiveTests.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,13 +3549,19 @@ public void Add_Date_Quantity()
35493549
var inputDate = new CqlDate(9999, 12, 30);
35503550
var quantity = new CqlQuantity(1, "day");
35513551
CqlDate expectedDate = new CqlDate(9999, 12, 31);
3552-
35533552
var newDate = fcq.Add(inputDate, quantity);
35543553
Assert.IsNotNull(newDate);
35553554
Assert.AreEqual(expectedDate, newDate);
3555+
}
35563556

3557+
[TestMethod]
3558+
public void Add_Date_Quantity_To_MaxDate()
3559+
{
3560+
var rc = GetNewContext();
3561+
var fcq = rc.Operators;
3562+
3563+
var quantity = new CqlQuantity(1, "day");
35573564
var inputDateMaxValue = CqlDate.MaxValue;
3558-
35593565
var newDateAddMax = fcq.Add(inputDateMaxValue, quantity);
35603566
Assert.IsNull(newDateAddMax);
35613567
}
@@ -3569,11 +3575,18 @@ public void Subtract_Date_Quantity()
35693575
var inputDate = new CqlDate(1, 1, 2);
35703576
var quantity = new CqlQuantity(1, "day");
35713577
CqlDate expectedDate = new CqlDate(1, 1, 1);
3572-
35733578
var newDate = fcq.Subtract(inputDate, quantity);
35743579
Assert.IsNotNull(newDate);
35753580
Assert.AreEqual(expectedDate, newDate);
3581+
}
3582+
3583+
[TestMethod]
3584+
public void Subtract_Date_Quantity_To_MinDate()
3585+
{
3586+
var rc = GetNewContext();
3587+
var fcq = rc.Operators;
35763588

3589+
var quantity = new CqlQuantity(1, "day");
35773590
var inputDateMinValue = CqlDate.MinValue;
35783591
var newDateSubtractedMin = fcq.Subtract(inputDateMinValue, quantity);
35793592
Assert.IsNull(newDateSubtractedMin);

Cql/Cql.Abstractions/Primitives/CqlDate.cs

Lines changed: 66 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -97,50 +97,42 @@ public static bool TryParse(string s, out CqlDate? cqlDate)
9797
quantity = quantity.NormalizeTo(Precision);
9898
var value = quantity.value!.Value;
9999
var dto = Value.DateTimeOffset;
100-
try
100+
switch (quantity.unit![0])
101101
{
102-
switch (quantity.unit![0])
103-
{
104-
case 'a':
105-
dto = dto.AddYears((int)value);
106-
break;
107-
case 'm':
108-
if (quantity.unit.Length > 1)
102+
case 'a':
103+
dto = dto.AddYears((int)value);
104+
break;
105+
case 'm':
106+
if (quantity.unit.Length > 1)
107+
{
108+
switch (quantity.unit[1])
109109
{
110-
switch (quantity.unit[1])
111-
{
112-
case 'o':
113-
dto = dto.AddMonths((int)value);
114-
break;
115-
case 'i':
116-
dto = dto.AddMinutes(Math.Truncate((double)value));
117-
break;
118-
case 's':
119-
dto = dto.AddMilliseconds(Math.Truncate((double)value));
120-
break;
121-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
122-
}
110+
case 'o':
111+
dto = dto.AddMonths((int)value);
112+
break;
113+
case 'i':
114+
dto = dto.AddMinutes(Math.Truncate((double)value));
115+
break;
116+
case 's':
117+
dto = dto.AddMilliseconds(Math.Truncate((double)value));
118+
break;
119+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
123120
}
124-
break;
125-
case 'd':
126-
dto = dto.AddDays((int)value!);
127-
break;
128-
case 'w':
129-
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
130-
break;
131-
case 'h':
132-
dto = dto.AddHours(Math.Truncate((double)value));
133-
break;
134-
case 's':
135-
dto = dto.AddSeconds(Math.Truncate((double)value));
136-
break;
137-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
138-
}
139-
}
140-
catch (ArgumentOutOfRangeException)
141-
{
142-
// In cases where e.g. Predecessor is called on minimum Date.
143-
return null;
121+
}
122+
break;
123+
case 'd':
124+
dto = dto.AddDays((int)value!);
125+
break;
126+
case 'w':
127+
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
128+
break;
129+
case 'h':
130+
dto = dto.AddHours(Math.Truncate((double)value));
131+
break;
132+
case 's':
133+
dto = dto.AddSeconds(Math.Truncate((double)value));
134+
break;
135+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
144136
}
145137

146138
var newIsoDate = new DateIso8601(dto, Value.Precision);
@@ -161,50 +153,42 @@ public static bool TryParse(string s, out CqlDate? cqlDate)
161153
quantity = quantity.NormalizeTo(Precision);
162154
var value = -1 * quantity.value!.Value;
163155
var dto = Value.DateTimeOffset;
164-
try
156+
switch (quantity.unit![0])
165157
{
166-
switch (quantity.unit![0])
167-
{
168-
case 'a':
169-
dto = dto.AddYears((int)value);
170-
break;
171-
case 'm':
172-
if (quantity.unit.Length > 1)
158+
case 'a':
159+
dto = dto.AddYears((int)value);
160+
break;
161+
case 'm':
162+
if (quantity.unit.Length > 1)
163+
{
164+
switch (quantity.unit[1])
173165
{
174-
switch (quantity.unit[1])
175-
{
176-
case 'o':
177-
dto = dto.AddMonths((int)value);
178-
break;
179-
case 'i':
180-
dto = dto.AddMinutes(Math.Truncate((double)value));
181-
break;
182-
case 's':
183-
dto = dto.AddMilliseconds(Math.Truncate((double)value));
184-
break;
185-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
186-
}
166+
case 'o':
167+
dto = dto.AddMonths((int)value);
168+
break;
169+
case 'i':
170+
dto = dto.AddMinutes(Math.Truncate((double)value));
171+
break;
172+
case 's':
173+
dto = dto.AddMilliseconds(Math.Truncate((double)value));
174+
break;
175+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
187176
}
188-
break;
189-
case 'd':
190-
dto = dto.AddDays((int)value!);
191-
break;
192-
case 'w':
193-
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
194-
break;
195-
case 'h':
196-
dto = dto.AddHours(Math.Truncate((double)value));
197-
break;
198-
case 's':
199-
dto = dto.AddSeconds(Math.Truncate((double)value));
200-
break;
201-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
202-
}
203-
}
204-
catch (ArgumentOutOfRangeException)
205-
{
206-
// In cases where e.g. Predecessor is called on minimum Date.
207-
return null;
177+
}
178+
break;
179+
case 'd':
180+
dto = dto.AddDays((int)value!);
181+
break;
182+
case 'w':
183+
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
184+
break;
185+
case 'h':
186+
dto = dto.AddHours(Math.Truncate((double)value));
187+
break;
188+
case 's':
189+
dto = dto.AddSeconds(Math.Truncate((double)value));
190+
break;
191+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
208192
}
209193

210194
var newIsoDate = new DateIso8601(dto, Value.Precision);

Cql/Cql.Runtime/Operators/CqlOperators.DateTimeOperators.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,44 @@ internal partial class CqlOperators
2121
{
2222
if (left == null || right == null)
2323
return null;
24-
else return left.Add(right);
24+
try
25+
{
26+
return left.Add(right);
27+
}
28+
catch (ArgumentOutOfRangeException e)
29+
{
30+
Message(new { left, right, e }, "CqlOperators.Add", "Warning", "Ignored overflow errors from date addition, returned null.");
31+
return null;
32+
}
2533
}
2634
public CqlDateTime? Add(CqlDateTime? left, CqlQuantity? right)
2735
{
2836
if (left == null || right == null)
2937
return null;
30-
else return left.Add(right);
38+
try
39+
{
40+
return left.Add(right);
41+
}
42+
catch (ArgumentOutOfRangeException e)
43+
{
44+
Message(new { left, right, e }, "CqlOperators.Add", "Warning", "Ignored overflow errors from datetime addition, returned null.");
45+
return null;
46+
}
3147
}
3248

3349
public CqlTime? Add(CqlTime? left, CqlQuantity? right)
3450
{
3551
if (left == null || right == null)
3652
return null;
37-
else return left.Add(right);
53+
try
54+
{
55+
return left.Add(right);
56+
}
57+
catch (ArgumentOutOfRangeException e)
58+
{
59+
Message(new { left, right, e }, "CqlOperators.Add", "Warning", "Ignored overflow errors from time addition, returned null.");
60+
return null;
61+
}
3862
}
3963

4064
#endregion
@@ -321,20 +345,45 @@ internal partial class CqlOperators
321345
{
322346
if (left == null || right == null)
323347
return null;
324-
else return left.Subtract(right);
348+
try
349+
{
350+
return left.Subtract(right);
351+
}
352+
catch (ArgumentOutOfRangeException e)
353+
{
354+
Message(new { left, right, e }, "CqlOperators.Subtract", "Warning", "Ignored overflow errors from date subtraction, returned null.");
355+
return null;
356+
}
325357
}
358+
326359
public CqlDateTime? Subtract(CqlDateTime? left, CqlQuantity? right)
327360
{
328361
if (left == null || right == null)
329362
return null;
330-
else return left.Subtract(right);
363+
try
364+
{
365+
return left.Subtract(right);
366+
}
367+
catch (ArgumentOutOfRangeException e)
368+
{
369+
Message(new { left, right, e }, "CqlOperators.Subtract", "Warning", "Ignored overflow errors from datetime subtraction, returned null.");
370+
return null;
371+
}
331372
}
332373

333374
public CqlTime? Subtract(CqlTime? left, CqlQuantity? right)
334375
{
335376
if (left == null || right == null)
336377
return null;
337-
else return left.Subtract(right);
378+
try
379+
{
380+
return left.Subtract(right);
381+
}
382+
catch (ArgumentOutOfRangeException e)
383+
{
384+
Message(new { left, right, e }, "CqlOperators.Subtract", "Warning", "Ignored overflow errors from time subtraction, returned null.");
385+
return null;
386+
}
338387
}
339388

340389
#endregion

0 commit comments

Comments
 (0)