-
Notifications
You must be signed in to change notification settings - Fork 56
Fix: date custom converter to serialize and deserialize ISO 8601 date and time #433
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4836c4b
fix: enhance DateCustomSerializer and add tests
KoditkarVedant 42d7e79
add explicit JsonIgnore attribute to IncludeTime property in Date class
KoditkarVedant 48ee22a
ensure culture-invariant date formatting in DateCustomConverter
KoditkarVedant 4e9c6be
Use CultureInfo.InvariantCulture in DateTimeOffset.Parse to ensure co…
KoditkarVedant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| using System; | ||
| using System.IO; | ||
| using Newtonsoft.Json; | ||
| using Notion.Client; | ||
| using Xunit; | ||
|
|
||
| namespace NotionUnitTests.PropertyValue; | ||
|
|
||
| public class DateCustomConverterTests | ||
| { | ||
| private readonly DateCustomConverter _converter = new(); | ||
| private readonly JsonSerializer _serializer = new(); | ||
|
|
||
| [Fact] | ||
| public void Serialize_null_writes_null() | ||
| { | ||
| // Arrange | ||
| Date date = null; | ||
| var stringWriter = new StringWriter(); | ||
| var jsonWriter = new JsonTextWriter(stringWriter); | ||
|
|
||
| // Act | ||
| _converter.WriteJson(jsonWriter, date, _serializer); | ||
| jsonWriter.Flush(); | ||
|
|
||
| // Assert | ||
| Assert.Equal("null", stringWriter.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialize_start_date_only_produces_correct_json() | ||
| { | ||
| // Arrange | ||
| var date = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 0, 0, 0, TimeSpan.Zero), | ||
| IncludeTime = false | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(date); | ||
|
|
||
| // Assert | ||
| Assert.Contains("\"start\":\"2023-05-15\"", json); | ||
| Assert.DoesNotContain("\"end\":", json); | ||
| Assert.DoesNotContain("\"time_zone\":", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialize_start_and_end_dates_produces_correct_json() | ||
| { | ||
| // Arrange | ||
| var date = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 0, 0, 0, TimeSpan.Zero), | ||
| End = new DateTimeOffset(2023, 5, 20, 0, 0, 0, TimeSpan.Zero), | ||
| IncludeTime = false | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(date); | ||
|
|
||
| // Assert | ||
| Assert.Contains("\"start\":\"2023-05-15\"", json); | ||
| Assert.Contains("\"end\":\"2023-05-20\"", json); | ||
| Assert.DoesNotContain("\"time_zone\":", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialize_with_time_included_formats_time_correctly() | ||
| { | ||
| // Arrange | ||
| var date = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), | ||
| IncludeTime = true | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(date); | ||
|
|
||
| // Assert | ||
| Assert.Contains("\"start\":\"2023-05-15T14:30:45Z\"", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialize_with_time_zone_includes_time_zone() | ||
| { | ||
| // Arrange | ||
| var date = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), | ||
| TimeZone = "Europe/London", | ||
| IncludeTime = true | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(date); | ||
|
|
||
| // Assert | ||
| Assert.Contains("\"start\":\"2023-05-15T14:30:45Z\"", json); | ||
| Assert.Contains("\"time_zone\":\"Europe/London\"", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Deserialize_null_returns_null() | ||
| { | ||
| // Arrange | ||
| const string Json = "null"; | ||
|
|
||
| // Act | ||
| var result = JsonConvert.DeserializeObject<Date>(Json); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Deserialize_start_date_only_returns_correct_date() | ||
| { | ||
| // Arrange | ||
| const string Json = "{\"start\":\"2023-05-15\"}"; | ||
|
|
||
| // Act | ||
| var result = JsonConvert.DeserializeObject<Date>(Json); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(new DateTimeOffset(2023, 5, 15, 0, 0, 0, TimeSpan.Zero), result.Start); | ||
| Assert.Null(result.End); | ||
| Assert.Null(result.TimeZone); | ||
| Assert.False(result.IncludeTime); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Deserialize_with_time_sets_include_time_flag() | ||
| { | ||
| // Arrange | ||
| const string Json = "{\"start\":\"2023-05-15T14:30:45\"}"; | ||
|
|
||
| // Act | ||
| var result = JsonConvert.DeserializeObject<Date>(Json); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), result.Start); | ||
| Assert.True(result.IncludeTime); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Deserialize_with_start_end_and_time_zone_returns_complete_date() | ||
| { | ||
| // Arrange | ||
| const string Json = "{\"start\":\"2023-05-15T14:30:45\",\"end\":\"2023-05-20T16:45:00\",\"time_zone\":\"America/New_York\"}"; | ||
|
|
||
| // Act | ||
| var result = JsonConvert.DeserializeObject<Date>(Json); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), result.Start); | ||
| Assert.Equal(new DateTimeOffset(2023, 5, 20, 16, 45, 0, TimeSpan.Zero), result.End); | ||
| Assert.Equal("America/New_York", result.TimeZone); | ||
| Assert.True(result.IncludeTime); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Date_property_value_serialize_deserialize_maintains_data() | ||
| { | ||
| // Arrange | ||
| var datePropertyValue = new DatePropertyValue | ||
| { | ||
| Date = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), | ||
| End = new DateTimeOffset(2023, 5, 20, 16, 45, 0, TimeSpan.Zero), | ||
| TimeZone = "Europe/Berlin", | ||
| IncludeTime = true | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(datePropertyValue); | ||
| var result = JsonConvert.DeserializeObject<DatePropertyValue>(json); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(PropertyValueType.Date, result.Type); | ||
| Assert.NotNull(result.Date); | ||
| Assert.Equal(datePropertyValue.Date.Start, result.Date.Start); | ||
| Assert.Equal(datePropertyValue.Date.End, result.Date.End); | ||
| Assert.Equal(datePropertyValue.Date.TimeZone, result.Date.TimeZone); | ||
| Assert.Equal(datePropertyValue.Date.IncludeTime, result.Date.IncludeTime); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Round_trip_preserves_data() | ||
| { | ||
| // Arrange | ||
| var originalDate = new Date | ||
| { | ||
| Start = new DateTimeOffset(2023, 5, 15, 14, 30, 45, TimeSpan.Zero), | ||
| End = new DateTimeOffset(2023, 5, 20, 16, 45, 0, TimeSpan.Zero), | ||
| TimeZone = "Europe/Berlin", | ||
| IncludeTime = true | ||
| }; | ||
|
|
||
| // Act | ||
| var json = JsonConvert.SerializeObject(originalDate); | ||
| var deserializedDate = JsonConvert.DeserializeObject<Date>(json); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(deserializedDate); | ||
| Assert.Equal(originalDate.Start, deserializedDate.Start); | ||
| Assert.Equal(originalDate.End, deserializedDate.End); | ||
| Assert.Equal(originalDate.TimeZone, deserializedDate.TimeZone); | ||
| Assert.True(deserializedDate.IncludeTime); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.