Skip to content

Commit

Permalink
fix: invariant culture in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender committed Oct 14, 2024
1 parent 43efa96 commit 8412ffc
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 79 deletions.
2 changes: 0 additions & 2 deletions src/_common/Quotes/Quote.Converters.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Globalization;

namespace Skender.Stock.Indicators;

// QUOTE UTILITIES (CONVERTERS)
Expand Down
13 changes: 11 additions & 2 deletions src/_common/Quotes/Quote.Validation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Globalization;

namespace Skender.Stock.Indicators;

// QUOTE UTILITIES: VALIDATION

public static partial class Quotes
{
private static readonly CultureInfo invariantCulture
= CultureInfo.InvariantCulture;

/// <summary>
/// Check that quotes are valid and in ascending order.
/// </summary>
Expand Down Expand Up @@ -34,13 +39,17 @@ public static IReadOnlyList<TQuote> Validate<TQuote>(

if (lastDate == currentDate)
{
string msg = $"Duplicate date found on {currentDate}.";
string msg =
$"Duplicate date found on {currentDate.ToString("MM/dd/yyyy", invariantCulture)}.";

throw new InvalidQuotesException(nameof(quotes), msg);
}

if (lastDate > currentDate)
{
string msg = $"Quotes are out of sequence on {currentDate}.";
string msg =
$"Quotes are out of sequence on {currentDate.ToString("MM/dd/yyyy", invariantCulture)}.";

throw new InvalidQuotesException(nameof(quotes), msg);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/indicators/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Test.Data;

public abstract class TestBase // base for all tests
{
internal static readonly CultureInfo englishCulture = new("en-US", false);
internal static readonly CultureInfo invariantCulture = CultureInfo.InvariantCulture;

internal static readonly IReadOnlyList<Quote> Quotes = Data.GetDefault();
internal static readonly IReadOnlyList<Quote> OtherQuotes = Data.GetCompare();
Expand All @@ -29,7 +29,7 @@ namespace Test.Data;
protected static readonly double DoublePrecision = 1E-13;

protected static readonly DateTime EvalDate
= DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
= DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions tests/indicators/_common/Candles/Candles.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public void SortCandles()
Assert.AreEqual(502, candles.Count);

// sample values
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", englishCulture);
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(firstDate, candles[0].Timestamp);

DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, candles[^1].Timestamp);

DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", englishCulture);
DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(spotDate, candles[50].Timestamp);
}

Expand Down
20 changes: 10 additions & 10 deletions tests/indicators/_common/Generics/Sorting.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ public void ToSortedCollection()
// baseline for comparison
IReadOnlyList<SmaResult> baseline =
[
new(Timestamp: DateTime.Parse("1/1/2000", englishCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/2/2000", englishCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/9/2000", englishCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/3/2000", englishCulture), Sma: 3),
new(Timestamp: DateTime.Parse("1/4/2000", englishCulture), Sma: 4),
new(Timestamp: DateTime.Parse("1/5/2000", englishCulture), Sma: 5),
new(Timestamp: DateTime.Parse("1/6/2000", englishCulture), Sma: 6),
new(Timestamp: DateTime.Parse("1/7/2000", englishCulture), Sma: 7),
new(Timestamp: DateTime.Parse("1/8/2000", englishCulture), Sma: double.NaN)
new(Timestamp: DateTime.Parse("1/1/2000", invariantCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/2/2000", invariantCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/9/2000", invariantCulture), Sma: null),
new(Timestamp: DateTime.Parse("1/3/2000", invariantCulture), Sma: 3),
new(Timestamp: DateTime.Parse("1/4/2000", invariantCulture), Sma: 4),
new(Timestamp: DateTime.Parse("1/5/2000", invariantCulture), Sma: 5),
new(Timestamp: DateTime.Parse("1/6/2000", invariantCulture), Sma: 6),
new(Timestamp: DateTime.Parse("1/7/2000", invariantCulture), Sma: 7),
new(Timestamp: DateTime.Parse("1/8/2000", invariantCulture), Sma: double.NaN)
];

// PUBLIC VARIANT, generic sorted Collection
Collection<SmaResult> sortResults = baseline
.ToSortedCollection();

Assert.AreEqual(5, sortResults[4].Sma);
Assert.AreEqual(DateTime.Parse("1/9/2000", englishCulture), sortResults.LastOrDefault().Timestamp);
Assert.AreEqual(DateTime.Parse("1/9/2000", invariantCulture), sortResults.LastOrDefault().Timestamp);
}
}
4 changes: 2 additions & 2 deletions tests/indicators/_common/Math/Numerical.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public void SlopeMismatch()
public void RoundDownDate()
{
TimeSpan interval = PeriodSize.OneHour.ToTimeSpan();
DateTime evDate = DateTime.Parse("2020-12-15 09:35:45", englishCulture);
DateTime evDate = DateTime.Parse("2020-12-15 09:35:45", invariantCulture);

DateTime rnDate = evDate.RoundDown(interval);
DateTime exDate = DateTime.Parse("2020-12-15 09:00:00", englishCulture);
DateTime exDate = DateTime.Parse("2020-12-15 09:00:00", invariantCulture);

Assert.AreEqual(exDate, rnDate);
}
Expand Down
18 changes: 9 additions & 9 deletions tests/indicators/_common/Quotes/Quote.Aggregates.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ public void Aggregate()

// sample values
Quote r0 = results[0];
Assert.AreEqual(DateTime.Parse("2020-12-15 09:30", englishCulture), r0.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 09:30", TestBase.invariantCulture), r0.Timestamp);
Assert.AreEqual(367.40m, r0.Open);
Assert.AreEqual(367.775m, r0.High);
Assert.AreEqual(367.02m, r0.Low);
Assert.AreEqual(367.24m, r0.Close);
Assert.AreEqual(2401786m, r0.Volume);

Quote r1 = results[1];
Assert.AreEqual(DateTime.Parse("2020-12-15 09:45", englishCulture), r1.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 09:45", TestBase.invariantCulture), r1.Timestamp);
Assert.AreEqual(367.25m, r1.Open);
Assert.AreEqual(367.44m, r1.High);
Assert.AreEqual(366.69m, r1.Low);
Assert.AreEqual(366.86m, r1.Close);
Assert.AreEqual(1669983m, r1.Volume);

Quote r2 = results[2];
Assert.AreEqual(DateTime.Parse("2020-12-15 10:00", englishCulture), r2.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 10:00", TestBase.invariantCulture), r2.Timestamp);
Assert.AreEqual(366.85m, r2.Open);
Assert.AreEqual(367.17m, r2.High);
Assert.AreEqual(366.57m, r2.Low);
Expand All @@ -61,23 +61,23 @@ public void AggregateTimeSpan()

// sample values
Quote r0 = results[0];
Assert.AreEqual(DateTime.Parse("2020-12-15 09:30", englishCulture), r0.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 09:30", TestBase.invariantCulture), r0.Timestamp);
Assert.AreEqual(367.40m, r0.Open);
Assert.AreEqual(367.775m, r0.High);
Assert.AreEqual(367.02m, r0.Low);
Assert.AreEqual(367.24m, r0.Close);
Assert.AreEqual(2401786m, r0.Volume);

Quote r1 = results[1];
Assert.AreEqual(DateTime.Parse("2020-12-15 09:45", englishCulture), r1.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 09:45", TestBase.invariantCulture), r1.Timestamp);
Assert.AreEqual(367.25m, r1.Open);
Assert.AreEqual(367.44m, r1.High);
Assert.AreEqual(366.69m, r1.Low);
Assert.AreEqual(366.86m, r1.Close);
Assert.AreEqual(1669983m, r1.Volume);

Quote r2 = results[2];
Assert.AreEqual(DateTime.Parse("2020-12-15 10:00", englishCulture), r2.Timestamp);
Assert.AreEqual(DateTime.Parse("2020-12-15 10:00", TestBase.invariantCulture), r2.Timestamp);
Assert.AreEqual(366.85m, r2.Open);
Assert.AreEqual(367.17m, r2.High);
Assert.AreEqual(366.57m, r2.Low);
Expand All @@ -102,23 +102,23 @@ public void AggregateMonth()

// sample values
Quote r0 = results[0];
Assert.AreEqual(DateTime.Parse("2017-01-01", englishCulture), r0.Timestamp);
Assert.AreEqual(DateTime.Parse("2017-01-01", TestBase.invariantCulture), r0.Timestamp);
Assert.AreEqual(212.61m, r0.Open);
Assert.AreEqual(217.02m, r0.High);
Assert.AreEqual(211.52m, r0.Low);
Assert.AreEqual(214.96m, r0.Close);
Assert.AreEqual(1569087580m, r0.Volume);

Quote r1 = results[1];
Assert.AreEqual(DateTime.Parse("2017-02-01", englishCulture), r1.Timestamp);
Assert.AreEqual(DateTime.Parse("2017-02-01", TestBase.invariantCulture), r1.Timestamp);
Assert.AreEqual(215.65m, r1.Open);
Assert.AreEqual(224.20m, r1.High);
Assert.AreEqual(214.29m, r1.Low);
Assert.AreEqual(223.41m, r1.Close);
Assert.AreEqual(1444958340m, r1.Volume);

Quote r23 = results[23];
Assert.AreEqual(DateTime.Parse("2018-12-01", englishCulture), r23.Timestamp);
Assert.AreEqual(DateTime.Parse("2018-12-01", TestBase.invariantCulture), r23.Timestamp);
Assert.AreEqual(273.47m, r23.Open);
Assert.AreEqual(273.59m, r23.High);
Assert.AreEqual(229.42m, r23.Low);
Expand Down
12 changes: 6 additions & 6 deletions tests/indicators/_common/Quotes/Quote.Converters.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public void ToSortedCollection()
Assert.AreEqual(502, h.Count);

// check first date
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", englishCulture);
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(firstDate, h[0].Timestamp);

// check last date
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(lastDate, h.LastOrDefault().Timestamp);

// spot check an out of sequence date
DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", englishCulture);
DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(spotDate, h[50].Timestamp);
}

Expand All @@ -41,15 +41,15 @@ public void ToSortedList()
Assert.AreEqual(502, h.Count);

// check first date
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", englishCulture);
DateTime firstDate = DateTime.ParseExact("01/18/2016", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(firstDate, h[0].Timestamp);

// check last date
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(lastDate, h[^1].Timestamp);

// spot check an out of sequence date
DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", englishCulture);
DateTime spotDate = DateTime.ParseExact("03/16/2017", "MM/dd/yyyy", TestBase.invariantCulture);
Assert.AreEqual(spotDate, h[50].Timestamp);
}
}
33 changes: 17 additions & 16 deletions tests/indicators/_common/Quotes/Quote.Validation.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Globalization;

namespace Utilities;

// quote validation
Expand All @@ -15,10 +17,10 @@ public void Validate()
Assert.AreEqual(502, h.Count);

// sample values
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, h[501].Timestamp);

DateTime spotDate = DateTime.ParseExact("02/01/2017", "MM/dd/yyyy", englishCulture);
DateTime spotDate = DateTime.ParseExact("02/01/2017", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(spotDate, h[20].Timestamp);
}

Expand All @@ -31,7 +33,7 @@ public void ValidateLong()
Assert.AreEqual(5285, h.Count);

// sample values
DateTime lastDate = DateTime.ParseExact("09/04/2020", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("09/04/2020", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, h[5284].Timestamp);
}

Expand Down Expand Up @@ -80,39 +82,38 @@ public void ValidateDuplicates()
{
IReadOnlyList<Quote> dupQuotes = new List<Quote>
{
new(Timestamp: DateTime.ParseExact("2017-01-03", "yyyy-MM-dd", englishCulture), Open: 214.86m, High: 220.33m, Low: 210.96m, Close: 216.99m, Volume: 5923254),
new(Timestamp: DateTime.ParseExact("2017-01-04", "yyyy-MM-dd", englishCulture), Open: 214.75m, High: 228.00m, Low: 214.31m, Close: 226.99m, Volume: 11213471),
new(Timestamp: DateTime.ParseExact("2017-01-05", "yyyy-MM-dd", englishCulture), Open: 226.42m, High: 227.48m, Low: 221.95m, Close: 226.75m, Volume: 5911695),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", englishCulture), Open: 226.93m, High: 230.31m, Low: 225.45m, Close: 229.01m, Volume: 5527893),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", englishCulture), Open: 228.97m, High: 231.92m, Low: 228.00m, Close: 231.28m, Volume: 3979484)
new(Timestamp: DateTime.ParseExact("2017-01-03", "yyyy-MM-dd", invariantCulture), Open: 214.86m, High: 220.33m, Low: 210.96m, Close: 216.99m, Volume: 5923254),
new(Timestamp: DateTime.ParseExact("2017-01-04", "yyyy-MM-dd", invariantCulture), Open: 214.75m, High: 228.00m, Low: 214.31m, Close: 226.99m, Volume: 11213471),
new(Timestamp: DateTime.ParseExact("2017-01-05", "yyyy-MM-dd", invariantCulture), Open: 226.42m, High: 227.48m, Low: 221.95m, Close: 226.75m, Volume: 5911695),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", invariantCulture), Open: 226.93m, High: 230.31m, Low: 225.45m, Close: 229.01m, Volume: 5527893),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", invariantCulture), Open: 228.97m, High: 231.92m, Low: 228.00m, Close: 231.28m, Volume: 3979484)
};

InvalidQuotesException dx
= Assert.ThrowsException<InvalidQuotesException>(
() => dupQuotes.Validate());

dx.Message.Should()
.Be("Duplicate date found on 1/6/2017 12:00:00 AM. (Parameter 'quotes')");
.Be("Duplicate date found on 01/06/2017. (Parameter 'quotes')");
}

[TestMethod]
public void ValidateOutOfSequence()
{
IReadOnlyList<Quote> unorderedQuotes = new List<Quote>
{
new(Timestamp: DateTime.ParseExact("2017-01-03", "yyyy-MM-dd", englishCulture), Open: 214.86m, High: 220.33m, Low: 210.96m, Close: 216.99m, Volume: 5923254),
new(Timestamp: DateTime.ParseExact("2017-01-04", "yyyy-MM-dd", englishCulture), Open: 214.75m, High: 228.00m, Low: 214.31m, Close: 226.99m, Volume: 11213471),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", englishCulture), Open: 228.97m, High: 231.92m, Low: 228.00m, Close: 231.28m, Volume: 3979484),
new(Timestamp: DateTime.ParseExact("2017-01-05", "yyyy-MM-dd", englishCulture), Open: 226.42m, High: 227.48m, Low: 221.95m, Close: 226.75m, Volume: 5911695),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", englishCulture), Open: 226.93m, High: 230.31m, Low: 225.45m, Close: 229.01m, Volume: 5527893)
new(Timestamp: DateTime.ParseExact("2017-01-03", "yyyy-MM-dd", invariantCulture), Open: 214.86m, High: 220.33m, Low: 210.96m, Close: 216.99m, Volume: 5923254),
new(Timestamp: DateTime.ParseExact("2017-01-04", "yyyy-MM-dd", invariantCulture), Open: 214.75m, High: 228.00m, Low: 214.31m, Close: 226.99m, Volume: 11213471),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", invariantCulture), Open: 228.97m, High: 231.92m, Low: 228.00m, Close: 231.28m, Volume: 3979484),
new(Timestamp: DateTime.ParseExact("2017-01-05", "yyyy-MM-dd", invariantCulture), Open: 226.42m, High: 227.48m, Low: 221.95m, Close: 226.75m, Volume: 5911695),
new(Timestamp: DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", invariantCulture), Open: 226.93m, High: 230.31m, Low: 225.45m, Close: 229.01m, Volume: 5527893)
};

InvalidQuotesException dx
= Assert.ThrowsException<InvalidQuotesException>(
() => unorderedQuotes.Validate());

dx.Message.Should()
.Be("Quotes are out of sequence on 1/5/2017 12:00:00 AM. (Parameter 'quotes')");
.Be("Quotes are out of sequence on 01/05/2017. (Parameter 'quotes')");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ToReusableList()
[TestMethod]
public void QuoteToReusable()
{
DateTime t = DateTime.Parse("5/5/2055", englishCulture);
DateTime t = DateTime.Parse("5/5/2055", invariantCulture);

decimal l = 111111111111111m;
decimal o = 222222222222222m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Standard()
QuotePart rohlc = ohlc[501];

// proper last date
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, rc.Timestamp);

// last values should be correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void ConvertQuote()
QuotePart ohlc = Quotes[501].ToQuotePart(CandlePart.OHLC4);

// proper last date
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, c.Timestamp);

// last values should be correct
Expand Down Expand Up @@ -70,7 +70,7 @@ public void ConvertList()
QuotePart rohlc = ohlc[501];

// proper last date
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", englishCulture);
DateTime lastDate = DateTime.ParseExact("12/31/2018", "MM/dd/yyyy", invariantCulture);
Assert.AreEqual(lastDate, rc.Timestamp);

// last values should be correct
Expand Down
Loading

0 comments on commit 8412ffc

Please sign in to comment.