Skip to content

Commit

Permalink
Fix System.Text.Json tests affected by DST (#66559)
Browse files Browse the repository at this point in the history
* Correct whitespace

* Switch DateTime.Now -> DateTime.UtcNow

* Tweak formatting

* Change references to UtcNow to a fixed date/time value.

* Add missing common file reference
  • Loading branch information
Clockwork-Muse committed Mar 16, 2022
1 parent 2a95018 commit f0b7773
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/libraries/Common/tests/System/DateTimeTestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace System
{
// Provide date/time test helpers
public static class DateTimeTestHelpers
{
// Fixed DateTimeOffset, to avoid the use of DateTimeOffset.(Utc)Now in situations that simply require a value be present.
public static DateTimeOffset FixedDateTimeOffsetValue => new DateTimeOffset(2001, 2, 3, 4, 5, 6, 789, TimeSpan.Zero);

// Fixed DateTime, to avoid the use of DateTime.(Utc)Now in situations that simply require a value be present.
public static DateTime FixedDateTimeValue => FixedDateTimeOffsetValue.UtcDateTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -376,8 +376,8 @@ public async Task ClassWithPrimitives_Parameterless()
point.SecondInt = 348943;
point.FirstString = "934sdkjfskdfssf";
point.SecondString = "sdad9434243242";
point.FirstDateTime = DateTime.Now;
point.SecondDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.FirstDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.SecondDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

point.X = 234235;
point.Y = 912874;
Expand All @@ -387,8 +387,8 @@ public async Task ClassWithPrimitives_Parameterless()
point.FourthInt = 348943;
point.ThirdString = "934sdkjfskdfssf";
point.FourthString = "sdad9434243242";
point.ThirdDateTime = DateTime.Now;
point.FourthDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.ThirdDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.FourthDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

string json = await JsonSerializerWrapperForString.SerializeWrapper(point);

Expand Down Expand Up @@ -421,15 +421,15 @@ public async Task ClassWithPrimitives()
point.SecondInt = 348943;
point.FirstString = "934sdkjfskdfssf";
point.SecondString = "sdad9434243242";
point.FirstDateTime = DateTime.Now;
point.SecondDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.FirstDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.SecondDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

point.ThirdInt = 348943;
point.FourthInt = 348943;
point.ThirdString = "934sdkjfskdfssf";
point.FourthString = "sdad9434243242";
point.ThirdDateTime = DateTime.Now;
point.FourthDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.ThirdDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.FourthDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

string json = await JsonSerializerWrapperForString.SerializeWrapper(point);

Expand Down Expand Up @@ -462,15 +462,15 @@ public async Task ClassWithPrimitivesPerf()
point.SecondInt = 348943;
point.FirstString = "934sdkjfskdfssf";
point.SecondString = "sdad9434243242";
point.FirstDateTime = DateTime.Now;
point.SecondDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.FirstDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.SecondDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

point.ThirdInt = 348943;
point.FourthInt = 348943;
point.ThirdString = "934sdkjfskdfssf";
point.FourthString = "sdad9434243242";
point.ThirdDateTime = DateTime.Now;
point.FourthDateTime = DateTime.Now.AddHours(1).AddYears(1);
point.ThirdDateTime = DateTimeTestHelpers.FixedDateTimeValue;
point.FourthDateTime = DateTimeTestHelpers.FixedDateTimeValue.AddHours(1).AddYears(1);

string json = await JsonSerializerWrapperForString.SerializeWrapper(point);

Expand Down Expand Up @@ -833,7 +833,7 @@ public async Task HonorExtensionDataGeneric()
[Fact]
public async Task ArgumentDeserialization_Honors_JsonInclude()
{
Point_MembersHave_JsonInclude point = new Point_MembersHave_JsonInclude(1, 2,3);
Point_MembersHave_JsonInclude point = new Point_MembersHave_JsonInclude(1, 2, 3);

string json = await JsonSerializerWrapperForString.SerializeWrapper(point);
Assert.Contains(@"""X"":1", json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,8 @@ public Class_With_Ctor_With_64_Params(int int0, int int1, int int2, int int3, in
Int56 = int56; Int57 = int57; Int58 = int58; Int59 = int59; Int60 = int60; Int61 = int61; Int62 = int62; Int63 = int63;
}

public static string Json {
public static string Json
{
get
{
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -2282,7 +2283,7 @@ public struct Point_With_Property
{
public int X { get; }
public int Y { get; }
public int Z { get; set; }
public int Z { get; set; }

[JsonConstructor]
public Point_With_Property(int x, int y)
Expand Down Expand Up @@ -2325,18 +2326,18 @@ public static MyEventsListerItem Instance
= new MyEventsListerItem
{
Campaign = "A very nice campaign",
EndDate = DateTime.UtcNow.AddDays(7),
EndDate = DateTimeTestHelpers.FixedDateTimeValue.AddDays(7),
EventId = 321,
EventName = "wonderful name",
Organization = "Local Animal Shelter",
StartDate = DateTime.UtcNow.AddDays(-7),
StartDate = DateTimeTestHelpers.FixedDateTimeValue.AddDays(-7),
TimeZone = TimeZoneInfo.Utc.DisplayName,
VolunteerCount = 15,
Tasks = Enumerable.Repeat(
new MyEventsListerItemTask
{
StartDate = DateTime.UtcNow,
EndDate = DateTime.UtcNow.AddDays(1),
StartDate = DateTimeTestHelpers.FixedDateTimeValue,
EndDate = DateTimeTestHelpers.FixedDateTimeValue.AddDays(1),
Name = "A very nice task to have"
}, 4).ToList()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ protected static ActiveOrUpcomingEvent CreateActiveOrUpcomingEvent()
CampaignManagedOrganizerName = "Name FamilyName",
CampaignName = "The very new campaign",
Description = "The .NET Foundation works with Microsoft and the broader industry to increase the exposure of open source projects in the .NET community and the .NET Foundation. The .NET Foundation provides access to these resources to projects and looks to promote the activities of our communities.",
EndDate = DateTime.UtcNow.AddYears(1),
EndDate = DateTimeTestHelpers.FixedDateTimeValue.AddYears(1),
Name = "Just a name",
ImageUrl = "https://www.dotnetfoundation.org/theme/img/carousel/foundation-diagram-content.png",
StartDate = DateTime.UtcNow,
StartDate = DateTimeTestHelpers.FixedDateTimeValue,
Offset = TimeSpan.FromHours(2)
};
}
Expand Down Expand Up @@ -460,10 +460,10 @@ protected static IndexViewModel CreateIndexViewModel()
CampaignManagedOrganizerName = "Name FamilyName",
CampaignName = "The very new campaign",
Description = "The .NET Foundation works with Microsoft and the broader industry to increase the exposure of open source projects in the .NET community and the .NET Foundation. The .NET Foundation provides access to these resources to projects and looks to promote the activities of our communities.",
EndDate = DateTime.UtcNow.AddYears(1),
EndDate = DateTimeTestHelpers.FixedDateTimeValue.AddYears(1),
Name = "Just a name",
ImageUrl = "https://www.dotnetfoundation.org/theme/img/carousel/foundation-diagram-content.png",
StartDate = DateTime.UtcNow
StartDate = DateTimeTestHelpers.FixedDateTimeValue
},
count: 20).ToList()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonTestPath)System\DateTimeTestHelpers.cs" Link="CommonTest\System\DateTimeTestHelpers.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.AsyncEnumerable.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.AsyncEnumerable.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.Concurrent.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.Concurrent.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void DictionaryWithNullableValue()
dictOfDictWithNull: new Dictionary<string, Dictionary<string, float?>> { { "key", dictWithFloatNull } },
42.0f);

DateTime now = DateTime.Now;
DateTime now = DateTimeTestHelpers.FixedDateTimeValue;
Dictionary<string, DateTime?> dictWithDateTimeValue = new Dictionary<string, DateTime?> { { "key", now } };
Dictionary<string, DateTime?> dictWithDateTimeNull = new Dictionary<string, DateTime?> { { "key", null } };
TestDictionaryWithNullableValue<Dictionary<string, DateTime?>, Dictionary<string, Dictionary<string, DateTime?>>, DateTime?>(
Expand Down Expand Up @@ -205,7 +205,7 @@ public static void EnumerableWithNullableValue()
enumerableOfEnumerableWithNull: new List<IEnumerable<float?>> { ieWithFloatNull },
42.0f);

DateTime now = DateTime.Now;
DateTime now = DateTimeTestHelpers.FixedDateTimeValue;
IEnumerable<DateTime?> ieWithDateTimeValue = new List<DateTime?> { now };
IEnumerable<DateTime?> ieWithDateTimeNull = new List<DateTime?> { null };
TestEnumerableWithNullableValue<IEnumerable<DateTime?>, IEnumerable<IEnumerable<DateTime?>>, DateTime?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public class Class_With_ListsOfBoxedNumbers
[Fact]
public static void NonNumber_AsBoxed_Property()
{
DateTime dateTime = DateTime.Now;
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();

string expected = @$"{{""MyDateTime"":{JsonSerializer.Serialize(dateTime)},""MyNullableGuid"":{JsonSerializer.Serialize(nullableGuid)}}}";
Expand Down Expand Up @@ -293,7 +293,7 @@ public class Class_With_BoxedNonNumbers
[Fact]
public static void NonNumber_AsBoxed_CollectionRootType_Element()
{
DateTime dateTime = DateTime.Now;
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();

string expected = @$"[{JsonSerializer.Serialize(dateTime)}]";
Expand Down Expand Up @@ -324,7 +324,7 @@ public static void NonNumber_AsBoxed_CollectionRootType_Element()
[Fact]
public static void NonNumber_AsBoxed_CollectionProperty_Element()
{
DateTime dateTime = DateTime.Now;
DateTime dateTime = DateTimeTestHelpers.FixedDateTimeValue;
Guid? nullableGuid = Guid.NewGuid();

string expected = @$"{{""MyDateTimes"":[{JsonSerializer.Serialize(dateTime)}],""MyNullableGuids"":[{JsonSerializer.Serialize(nullableGuid)}]}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<Compile Include="$(CommonTestPath)System\IO\WrappedMemoryStream.cs" Link="CommonTest\System\IO\WrappedMemoryStream.cs" />
<Compile Include="$(CommonTestPath)System\DateTimeTestHelpers.cs" Link="CommonTest\System\DateTimeTestHelpers.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.AsyncEnumerable.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.AsyncEnumerable.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.Concurrent.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.Concurrent.cs" />
<Compile Include="..\Common\CollectionTests\CollectionTests.cs" Link="CommonTest\System\Text\Json\Tests\Serialization\CollectionTests\CollectionTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6195,7 +6195,7 @@ public void WriteLargeKeyOrValue(bool formatted, bool skipValidation)
var output = new ArrayBufferWriter<byte>(1024);
using var jsonUtf8 = new Utf8JsonWriter(output, options);
jsonUtf8.WriteStartObject();
Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(key, DateTime.Now));
Assert.Throws<ArgumentException>(() => jsonUtf8.WriteString(key, DateTimeTestHelpers.FixedDateTimeValue));
Assert.Equal(0, output.WrittenCount);
}

Expand Down Expand Up @@ -6304,7 +6304,7 @@ public void WriteDateTimeOffset_TrimsFractionCorrectly(string testStr, string ex
[Fact]
public void WriteDateTime_TrimsFractionCorrectly_SerializerRoundtrip()
{
DateTime utcNow = DateTime.UtcNow;
DateTime utcNow = DateTimeTestHelpers.FixedDateTimeValue;
Assert.Equal(utcNow, JsonSerializer.Deserialize(JsonSerializer.SerializeToUtf8Bytes(utcNow), typeof(DateTime)));
}

Expand Down

0 comments on commit f0b7773

Please sign in to comment.