Skip to content

Commit

Permalink
Merge pull request #422 from notion-dotnet/415-make-date-property-val…
Browse files Browse the repository at this point in the history
…ue-use-datetimeoffset

Use DateTimeOffset instead of DateTime for DatePropertyValue
  • Loading branch information
KoditkarVedant authored Nov 3, 2024
2 parents 172570c + f93ceb6 commit a9e8c60
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Notion.Client
{
Expand All @@ -26,13 +27,15 @@ public class Date
/// Start date with optional time.
/// </summary>
[JsonProperty("start")]
public DateTime? Start { get; set; }
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTimeOffset? Start { get; set; }

/// <summary>
/// End date with optional time.
/// </summary>
[JsonProperty("end")]
public DateTime? End { get; set; }
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTimeOffset? End { get; set; }

/// <summary>
/// Optional time zone information for start and end. Possible values are extracted from the IANA database and they are
Expand Down
61 changes: 58 additions & 3 deletions Test/Notion.IntegrationTests/PageClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
{
Date = new Date
{
Start = Convert.ToDateTime("2020-12-08T12:00:00Z"),
End = Convert.ToDateTime("2025-12-08T12:00:00Z")
Start = DateTimeOffset.Parse("2024-06-26T00:00:00.000+01:00"),
End = DateTimeOffset.Parse("2025-12-08").Date
}
})
.Build();
Expand All @@ -236,7 +236,8 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
);

// Assert
setDate?.Date?.Start.Should().Be(Convert.ToDateTime("2020-12-08T12:00:00Z"));
setDate?.Date?.Start.Should().Be(DateTimeOffset.Parse("2024-06-26T00:00:00.000+01:00"));
setDate?.Date?.End.Should().Be(DateTimeOffset.Parse("2025-12-08T00:00:00.000+01:00"));

var pageUpdateParameters = new PagesUpdateParameters
{
Expand Down Expand Up @@ -384,4 +385,58 @@ public async Task Bug_exception_when_attempting_to_set_select_property_to_nothin
updatedPage.Properties["Colors1"].As<SelectPropertyValue>().Select.Name.Should().Be("Blue");
updatedPage.Properties["Colors2"].As<SelectPropertyValue>().Select.Should().BeNull();
}

[Fact]
public async Task Verify_date_property_is_parsed_correctly_in_mention_object()
{
var pageRequest = PagesCreateParametersBuilder
.Create(new DatabaseParentInput { DatabaseId = _database.Id })
.AddProperty("Name",
new TitlePropertyValue
{
Title = new List<RichTextBase>
{
new RichTextMention()
{
Mention = new Mention()
{
Page = new ObjectId()
{
Id = _page.Id,
},
Date = new DatePropertyValue()
{
Date = new Date()
{
Start = DateTime.UtcNow
}
}
}
}
}
})
.Build();

var page = await Client.Pages.CreateAsync(pageRequest);

page.Should().NotBeNull();

page.Parent.Should().BeOfType<DatabaseParent>().Which
.DatabaseId.Should().Be(_database.Id);

page.Properties.Should().ContainKey("Name");
var pageProperty = page.Properties["Name"].Should().BeOfType<TitlePropertyValue>().Subject;

var titleProperty = (ListPropertyItem)await Client.Pages.RetrievePagePropertyItemAsync(
new RetrievePropertyItemParameters
{
PageId = page.Id,
PropertyId = pageProperty.Id
});

titleProperty.Results.First()
.As<TitlePropertyItem>()
.Title.As<RichTextMention>()
.Mention.Date.Date.Should().NotBeNull();
}
}

0 comments on commit a9e8c60

Please sign in to comment.