Skip to content

Commit e7e853d

Browse files
authored
CSHARP-5251: Fix tests of Decimal128 and DateTime with locales (#1430)
* CSHARP-5251: Fix compatibility of Decimal128 and DateTime tests with certain locales * Removed unused variable and fixed string interpolation * Removed unused using
1 parent 0e56324 commit e7e853d

12 files changed

+45
-36
lines changed

src/MongoDB.Bson/IO/JsonWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public override void WriteDateTime(long value)
217217
if (value >= 0 && value <= BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch)
218218
{
219219
var utcDateTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(value);
220-
_textWriter.Write("{{ \"$date\" : \"{0}\" }}", utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
220+
_textWriter.Write("{{ \"$date\" : \"{0}\" }}", utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
221221
}
222222
else
223223
{
@@ -236,7 +236,7 @@ public override void WriteDateTime(long value)
236236
value <= BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch)
237237
{
238238
var utcDateTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(value);
239-
_textWriter.Write("ISODate(\"{0}\")", utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
239+
_textWriter.Write("ISODate(\"{0}\")", utcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
240240
}
241241
else
242242
{

tests/MongoDB.Bson.Tests/IO/JsonWriterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.Globalization;
1819
using System.IO;
1920
using System.Linq;
2021
using System.Text;
@@ -402,7 +403,7 @@ public void TestDateTimeShell()
402403
{
403404
var utcNow = DateTime.UtcNow;
404405
var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
405-
var isoDate = string.Format("ISODate(\"{0}\")", utcNowTruncated.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
406+
var isoDate = string.Format("ISODate(\"{0}\")", utcNowTruncated.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
406407
var tests = new TestData<BsonDateTime>[]
407408
{
408409
new TestData<BsonDateTime>(new BsonDateTime(long.MinValue), "new Date(-9223372036854775808)"),

tests/MongoDB.Bson.Tests/Jira/CSharp133Tests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Globalization;
1718
using System.Linq;
1819
using MongoDB.Bson;
1920
using MongoDB.Bson.Serialization;
@@ -61,7 +62,7 @@ public void TestNotNull()
6162
var date = new DateTime(1980, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
6263
var c = new C { S = "xyz", I = "xyz", I2 = date, D = "xyz" };
6364
var json = c.ToJson();
64-
var expected = ("{ 'S' : 'xyz', 'I' : 'xyz', 'D' : 'xyz', 'I2' : ISODate('" + date.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") + "') }").Replace("'", "\"");
65+
var expected = ("{ 'S' : 'xyz', 'I' : 'xyz', 'D' : 'xyz', 'I2' : ISODate('" + date.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture) + "') }").Replace("'", "\"");
6566
Assert.Equal(expected, json);
6667

6768
var bson = c.ToBson();

tests/MongoDB.Bson.Tests/ObjectModel/Decimal128Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Decimal(string valueString, string expectedResult)
7979
public void ToDecimal_should_return_expected_result(string valueString, string expectedResultString)
8080
{
8181
var subject = Decimal128.Parse(valueString);
82-
var expectedResult = decimal.Parse(expectedResultString);
82+
var expectedResult = decimal.Parse(expectedResultString, CultureInfo.InvariantCulture);
8383

8484
var result = Decimal128.ToDecimal(subject);
8585

tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonPrimitiveSerializerTests.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Globalization;
1718
using System.IO;
1819
using System.Linq;
1920
using FluentAssertions;
@@ -457,14 +458,14 @@ public void TestLocal()
457458
var json = obj.ToJson();
458459
var expected = __expectedTemplate;
459460
var milliseconds = (utc.Ticks - BsonConstants.UnixEpoch.Ticks) / 10000;
460-
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
461+
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
461462
expected = expected.Replace("#Default", utcJson);
462463
expected = expected.Replace("#Local", utcJson);
463464
expected = expected.Replace("#Unspecified", utcJson);
464465
expected = expected.Replace("#Utc", utcJson);
465466
expected = expected.Replace("#Ticks", string.Format("NumberLong('{0}')", utc.Ticks.ToString()));
466-
expected = expected.Replace("#String", local.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz"));
467-
expected = expected.Replace("#DateOnlyString", local.Date.ToString("yyyy-MM-dd"));
467+
expected = expected.Replace("#String", local.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz", CultureInfo.InvariantCulture));
468+
expected = expected.Replace("#DateOnlyString", local.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
468469
expected = expected.Replace("#Document", "{ 'DateTime' : #D, 'Ticks' : NumberLong('#T') }".Replace("#D", utcJson).Replace("#T", utc.Ticks.ToString()));
469470
expected = expected.Replace("'", "\"");
470471
Assert.Equal(expected, json);
@@ -510,14 +511,14 @@ public void TestUnspecified()
510511
var json = obj.ToJson();
511512
var expected = __expectedTemplate;
512513
var milliseconds = (utc.Ticks - BsonConstants.UnixEpoch.Ticks) / 10000;
513-
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
514+
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
514515
expected = expected.Replace("#Default", utcJson);
515516
expected = expected.Replace("#Local", utcJson);
516517
expected = expected.Replace("#Unspecified", utcJson);
517518
expected = expected.Replace("#Utc", utcJson);
518519
expected = expected.Replace("#Ticks", string.Format("NumberLong('{0}')", utc.Ticks.ToString()));
519-
expected = expected.Replace("#String", unspecified.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz"));
520-
expected = expected.Replace("#DateOnlyString", unspecified.Date.ToString("yyyy-MM-dd"));
520+
expected = expected.Replace("#String", unspecified.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz", CultureInfo.InvariantCulture));
521+
expected = expected.Replace("#DateOnlyString", unspecified.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
521522
expected = expected.Replace("#Document", "{ 'DateTime' : #D, 'Ticks' : NumberLong('#T') }".Replace("#D", utcJson).Replace("#T", utc.Ticks.ToString()));
522523
expected = expected.Replace("'", "\"");
523524
Assert.Equal(expected, json);
@@ -562,14 +563,14 @@ public void TestUtc()
562563
var json = obj.ToJson();
563564
var expected = __expectedTemplate;
564565
var milliseconds = (utc.Ticks - BsonConstants.UnixEpoch.Ticks) / 10000;
565-
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
566+
var utcJson = string.Format("ISODate(\"{0}\")", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
566567
expected = expected.Replace("#Default", utcJson);
567568
expected = expected.Replace("#Local", utcJson);
568569
expected = expected.Replace("#Unspecified", utcJson);
569570
expected = expected.Replace("#Utc", utcJson);
570571
expected = expected.Replace("#Ticks", string.Format("NumberLong('{0}')", utc.Ticks.ToString()));
571-
expected = expected.Replace("#String", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFZ"));
572-
expected = expected.Replace("#DateOnlyString", utc.Date.ToString("yyyy-MM-dd"));
572+
expected = expected.Replace("#String", utc.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFZ", CultureInfo.InvariantCulture));
573+
expected = expected.Replace("#DateOnlyString", utc.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
573574
expected = expected.Replace("#Document", "{ 'DateTime' : #D, 'Ticks' : NumberLong('#T') }".Replace("#D", utcJson).Replace("#T", utc.Ticks.ToString()));
574575
expected = expected.Replace("'", "\"");
575576
Assert.Equal(expected, json);
@@ -600,7 +601,8 @@ public void TestUtc()
600601
public void TestPrivateFieldWithBsonRepresentation()
601602
{
602603
var testValue = new DateTime(2020, 01, 01);
603-
var json = $"{{ '_d' : '{testValue:yyyy-MM-ddTHH:mm:ss.FFFZ}' }}";
604+
var stringTestValue = testValue.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
605+
var json = $"{{ '_d' : '{stringTestValue}' }}";
604606

605607
var deserialized = BsonSerializer.Deserialize<TestClassWithPrivateField>(json);
606608
Assert.Equal(testValue, deserialized.GetPrivateD());

tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.Globalization;
1819
using System.Linq;
1920
using FluentAssertions;
2021
using MongoDB.Bson.IO;
@@ -600,8 +601,7 @@ public void TestMaxUtc()
600601
public void TestLocal()
601602
{
602603
var obj = new TestClass(new DateTime(2010, 10, 08, 13, 30, 0, DateTimeKind.Local));
603-
var isoDate = string.Format("ISODate(\"{0}\")", obj.V.ToUniversalTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
604-
long milliseconds = (long)(obj.V.ToUniversalTime() - BsonConstants.UnixEpoch).TotalMilliseconds;
604+
var isoDate = $"ISODate(\"{obj.V.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture)}\")";
605605
var json = obj.ToJson();
606606
var expected = "{ 'B' : #, 'V' : # }".Replace("#", isoDate).Replace("'", "\"");
607607
Assert.Equal(expected, json);
@@ -615,7 +615,7 @@ public void TestLocal()
615615
public void TestUnspecified()
616616
{
617617
var obj = new TestClass(new DateTime(2010, 10, 08, 13, 30, 0, DateTimeKind.Unspecified));
618-
var isoDate = string.Format("ISODate(\"{0}\")", obj.V.ToUniversalTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
618+
var isoDate = string.Format("ISODate(\"{0}\")", obj.V.ToUniversalTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
619619
var json = obj.ToJson();
620620
var expected = "{ 'B' : #, 'V' : # }".Replace("#", isoDate).Replace("'", "\"");
621621
Assert.Equal(expected, json);
@@ -629,7 +629,7 @@ public void TestUnspecified()
629629
public void TestUtc()
630630
{
631631
var obj = new TestClass(new DateTime(2010, 10, 08, 13, 30, 0, DateTimeKind.Utc));
632-
var isoDate = string.Format("ISODate(\"{0}\")", obj.V.ToUniversalTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
632+
var isoDate = string.Format("ISODate(\"{0}\")", obj.V.ToUniversalTime().ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
633633
var json = obj.ToJson();
634634
var expected = "{ 'B' : #, 'V' : # }".Replace("#", isoDate).Replace("'", "\"");
635635
Assert.Equal(expected, json);

tests/MongoDB.Bson.Tests/Serialization/Serializers/CollectionSerializerGenericTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.Globalization;
1819
using System.IO;
1920
using System.Linq;
2021
using MongoDB.Bson.IO;
@@ -224,7 +225,7 @@ public void TestMixedPrimitiveTypes(
224225

225226
#pragma warning disable 618
226227
var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
227-
var isoDate = string.Format("ISODate(\"{0}\")", dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
228+
var isoDate = string.Format("ISODate(\"{0}\")", dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
228229
var guid = Guid.Empty;
229230
string expectedGuidJson = null;
230231
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)

tests/MongoDB.Bson.Tests/Serialization/Serializers/CollectionSerializerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections;
18+
using System.Globalization;
1819
using System.IO;
1920
using System.Linq;
2021
using MongoDB.Bson.IO;
@@ -222,7 +223,7 @@ public void TestMixedPrimitiveTypes(
222223

223224
#pragma warning disable 618
224225
var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
225-
var isoDate = string.Format("ISODate(\"{0}\")", dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
226+
var isoDate = string.Format("ISODate(\"{0}\")", dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));
226227
var guid = Guid.Empty;
227228
string expectedGuidJson = null;
228229
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)

tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeSerializerTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Globalization;
1718
using System.Linq;
1819
using FluentAssertions;
1920
using MongoDB.Bson;
@@ -237,8 +238,8 @@ public void TestNow()
237238
c.D = c.DT.Date;
238239

239240
var json = c.ToJson();
240-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
241-
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
241+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
242+
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
242243
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
243244
Assert.Equal(expected, json);
244245

@@ -255,8 +256,8 @@ public void TestUtcNow()
255256
c.D = c.DT.Date;
256257

257258
var json = c.ToJson();
258-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
259-
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
259+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
260+
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
260261
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
261262
Assert.Equal(expected, json);
262263

@@ -539,8 +540,8 @@ public void TestNow()
539540
c.D = c.DT.Date;
540541

541542
var json = c.ToJson();
542-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
543-
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
543+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
544+
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
544545
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
545546
Assert.Equal(expected, json);
546547

@@ -556,8 +557,8 @@ public void TestUtcNow()
556557
c.D = c.DT.Date;
557558

558559
var json = c.ToJson();
559-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
560-
var rep2 = c.D.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
560+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
561+
var rep2 = c.D.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
561562
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
562563
Assert.Equal(expected, json);
563564

@@ -689,8 +690,8 @@ public void TestNow()
689690
c.D = c.DT.Date;
690691

691692
var json = c.ToJson();
692-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
693-
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
693+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
694+
var rep2 = c.D.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
694695
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
695696
Assert.Equal(expected, json);
696697

@@ -707,8 +708,8 @@ public void TestUtcNow()
707708
c.D = c.DT.Date;
708709

709710
var json = c.ToJson();
710-
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
711-
var rep2 = c.D.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
711+
var rep1 = c.DT.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
712+
var rep2 = c.D.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
712713
var expected = "{ 'DT' : ISODate('#1'), 'D' : ISODate('#2') }".Replace("#1", rep1).Replace("#2", rep2).Replace("'", "\"");
713714
Assert.Equal(expected, json);
714715

tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryGenericSerializerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Collections.ObjectModel;
19+
using System.Globalization;
1920
using System.IO;
2021
using System.Linq;
2122
using System.Text;
@@ -378,7 +379,7 @@ public void TestMixedPrimitiveTypes(
378379

379380
#pragma warning disable 618
380381
var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
381-
var isoDate = dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
382+
var isoDate = dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture);
382383
var guid = Guid.Empty;
383384
string expectedGuidJson = null;
384385
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)

0 commit comments

Comments
 (0)