Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DueDate.StringDate conversion to and from DateTime. #37

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Todoist.Net.Tests/Helpers/FakeLocalTimeZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Reflection;

namespace Todoist.Net.Tests.Helpers;

/// <summary>
/// A helper class that changes the local timezone to a fake timezone provided
/// at initialization, and resets the original local timezone when disposed.
/// </summary>
/// <remarks>
/// See this <see href="https://stackoverflow.com/questions/44413407/mock-the-country-timezone-you-are-running-unit-test-from">SO question</see> for more details.
/// </remarks>
public sealed class FakeLocalTimeZone : IDisposable
{

/// <summary>
/// Initializes a new instance of the <see cref="FakeLocalTimeZone"/> class
/// and changes the local time zone to the given <paramref name="fakeTimeZoneInfo"/>
/// until it's disposed.
/// </summary>
/// <param name="fakeTimeZoneInfo">The time zone to set as local until disposal.</param>
public FakeLocalTimeZone(TimeZoneInfo fakeTimeZoneInfo)
{
var info = typeof(TimeZoneInfo).GetField("s_cachedData", BindingFlags.NonPublic | BindingFlags.Static);
var cachedData = info.GetValue(null);

var field = cachedData.GetType().GetField("_localTimeZone",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Instance);

field.SetValue(cachedData, fakeTimeZoneInfo);
}

public void Dispose()
{
TimeZoneInfo.ClearCachedData();
}
}
37 changes: 37 additions & 0 deletions src/Todoist.Net.Tests/Helpers/FakeLocalTimeZoneTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;

using Todoist.Net.Tests.Extensions;

using Xunit;

namespace Todoist.Net.Tests.Helpers;

[Trait(Constants.TraitName, Constants.UnitTraitValue)]
public class FakeLocalTimeZoneTests
{

[Fact]
public void FakeLocalTimeZone_ShouldChangeLocalTimeZoneWithinScope_AndResetItBackOutsideScope()
{
var actualTimeZoneInfo = TimeZoneInfo.Local;

var timeZoneCollection = TimeZoneInfo
.GetSystemTimeZones()
.Where(t => !actualTimeZoneInfo.Equals(t))
.ToArray();

var randomIndex = new Random().Next(timeZoneCollection.Length);
var fakeTimeZoneInfo = timeZoneCollection.ElementAt(randomIndex);


Assert.NotEqual(fakeTimeZoneInfo, actualTimeZoneInfo);

using (var fakeLocalTimeZone = new FakeLocalTimeZone(fakeTimeZoneInfo))
{
Assert.Equal(fakeTimeZoneInfo, TimeZoneInfo.Local);
}
Assert.Equal(actualTimeZoneInfo, TimeZoneInfo.Local);
}

}
75 changes: 73 additions & 2 deletions src/Todoist.Net.Tests/Models/DueDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@

using Todoist.Net.Models;
using Todoist.Net.Tests.Extensions;
using Todoist.Net.Tests.Helpers;

using Xunit;

namespace Todoist.Net.Tests.Models
{
[Trait(Constants.TraitName, Constants.UnitTraitValue)]
public class DueDateTests
public class DueDateTests : IDisposable
{

private readonly FakeLocalTimeZone _fakeLocalTimeZone;

public DueDateTests()
{
var timeZoneCollection = System.TimeZoneInfo.GetSystemTimeZones();

var randomIndex = new Random().Next(timeZoneCollection.Count);
var fakeTimeZoneInfo = timeZoneCollection[randomIndex];

_fakeLocalTimeZone = new FakeLocalTimeZone(fakeTimeZoneInfo);
}

public void Dispose()
{
_fakeLocalTimeZone.Dispose();
GC.SuppressFinalize(this);
}


[Fact]
public void DateTimeAssignment_FullDayEvent_Success()
{
Expand All @@ -23,7 +45,7 @@ public void DateTimeAssignment_FullDayEvent_Success()
[Fact]
public void DateTimeAssignment_FloatingDueDateEvent_Success()
{
var date = new DateTime(2018, 2, 5, 0, 0, 0, DateTimeKind.Utc);
var date = new DateTime(2018, 2, 5, 0, 0, 0, DateTimeKind.Unspecified);

var dueDate = new DueDate(date);

Expand All @@ -41,5 +63,54 @@ public void DateTimeAssignment_FloatingDueDateWithTimezoneEvent_Success()
Assert.Equal("2018-02-05T00:00:00Z", dueDate.StringDate);
Assert.False(dueDate.IsFullDay);
}


[Fact]
public void StringDateProperty_ShouldReturnExactAssignedValue_WhenValueIsFullDayDate()
{
// Arrange
var dueDate = new DueDate();
string initialValue = "2016-12-01";

// Act
dueDate.StringDate = initialValue; // Set initial value.
string returnedValue = dueDate.StringDate; // Get.
dueDate.StringDate = returnedValue; // Set returned value.

// Assert
Assert.Equal(returnedValue, dueDate.StringDate);
}

[Fact]
public void StringDateProperty_ShouldReturnExactAssignedValue_WhenValueIsFloatingDate()
{
// Arrange
var dueDate = new DueDate();
string initialValue = "2016-12-03T12:00:00";

// Act
dueDate.StringDate = initialValue; // Set initial value.
string returnedValue = dueDate.StringDate; // Get.
dueDate.StringDate = returnedValue; // Set returned value.

// Assert
Assert.Equal(returnedValue, dueDate.StringDate);
}

[Fact]
public void StringDateProperty_ShouldReturnExactAssignedValue_WhenValueIsFixedDate()
{
// Arrange
var dueDate = new DueDate();
string initialValue = "2016-12-06T13:00:00Z";

// Act
dueDate.StringDate = initialValue; // Set initial value.
string returnedValue = dueDate.StringDate; // Get.
dueDate.StringDate = returnedValue; // Set returned value.

// Assert
Assert.Equal(returnedValue, dueDate.StringDate);
}
}
}
11 changes: 3 additions & 8 deletions src/Todoist.Net/Models/DueDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Todoist.Net.Models
/// </summary>
public class DueDate
{
private const string DefaultEventDateFormat = "yyyy-MM-ddTHH:mm:ssK"; // Roundtrip without milliseconds.
private const string FullDayEventDateFormat = "yyyy-MM-dd";

/// <summary>
Expand Down Expand Up @@ -124,13 +125,7 @@ internal string StringDate
return Date.Value.ToString(FullDayEventDateFormat);
}

var date = Date.Value.ToUniversalTime();
if (string.IsNullOrEmpty(Timezone))
{
return date.ToString("s");
}

return date.ToString("s") + "Z";
return Date.Value.ToString(DefaultEventDateFormat);
}

set
Expand All @@ -143,7 +138,7 @@ internal string StringDate
return;
}

Date = DateTime.Parse(value, CultureInfo.InvariantCulture);
Date = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
}
}
}
Expand Down