Skip to content

Commit 5de4799

Browse files
Merge pull request #273 from notion-dotnet/237-add-ability-to-clear-date-property
Add ability to clear date property 💖
2 parents db8700c + 2e9767c commit 5de4799

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DatePropertyValue : PropertyValue
1313
/// <summary>
1414
/// Date
1515
/// </summary>
16-
[JsonProperty("date")]
16+
[JsonProperty("date", NullValueHandling = NullValueHandling.Include)]
1717
public Date Date { get; set; }
1818
}
1919

Test/Notion.IntegrationTests/IPageClientTests.cs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Notion.IntegrationTests
1111
public class IPageClientTests
1212
{
1313
private readonly INotionClient _client;
14+
private readonly string _databaseId;
1415

1516
public IPageClientTests()
1617
{
@@ -20,14 +21,15 @@ public IPageClientTests()
2021
};
2122

2223
_client = NotionClientFactory.Create(options);
24+
_databaseId = Environment.GetEnvironmentVariable("DATABASE_ID") ?? "f86f2262-0751-40f2-8f63-e3f7a3c39fcb";
2325
}
2426

2527
[Fact]
2628
public async Task CreateAsync_CreatesANewPage()
2729
{
2830
PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput
2931
{
30-
DatabaseId = "f86f2262-0751-40f2-8f63-e3f7a3c39fcb"
32+
DatabaseId = _databaseId
3133
})
3234
.AddProperty("Name", new TitlePropertyValue
3335
{
@@ -48,7 +50,7 @@ public async Task CreateAsync_CreatesANewPage()
4850

4951
page.Should().NotBeNull();
5052
page.Parent.Should().BeOfType<DatabaseParent>().Which
51-
.DatabaseId.Should().Be("f86f2262-0751-40f2-8f63-e3f7a3c39fcb");
53+
.DatabaseId.Should().Be(_databaseId);
5254

5355
page.Properties.Should().ContainKey("Name");
5456
page.Properties["Name"].Should().BeOfType<TitlePropertyValue>().Which
@@ -65,7 +67,7 @@ public async Task Bug_unable_to_create_page_with_select_property()
6567
{
6668
PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput
6769
{
68-
DatabaseId = "f86f2262-0751-40f2-8f63-e3f7a3c39fcb"
70+
DatabaseId = _databaseId
6971
})
7072
.AddProperty("Name", new TitlePropertyValue
7173
{
@@ -93,7 +95,7 @@ public async Task Bug_unable_to_create_page_with_select_property()
9395

9496
page.Should().NotBeNull();
9597
page.Parent.Should().BeOfType<DatabaseParent>().Which
96-
.DatabaseId.Should().Be("f86f2262-0751-40f2-8f63-e3f7a3c39fcb");
98+
.DatabaseId.Should().Be(_databaseId);
9799

98100
page.Properties.Should().ContainKey("Name");
99101
page.Properties["Name"].Should().BeOfType<TitlePropertyValue>().Which
@@ -110,7 +112,7 @@ public async Task Test_RetrievePagePropertyItemAsync()
110112
{
111113
PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput
112114
{
113-
DatabaseId = "f86f2262-0751-40f2-8f63-e3f7a3c39fcb"
115+
DatabaseId = _databaseId
114116
})
115117
.AddProperty("Name", new TitlePropertyValue
116118
{
@@ -155,5 +157,70 @@ public async Task Test_RetrievePagePropertyItemAsync()
155157
Archived = true
156158
});
157159
}
160+
161+
[Fact]
162+
public async Task Test_UpdatePageProperty_with_date_as_null()
163+
{
164+
// setup - add property to db and create a page with the property having a date
165+
166+
string datePropertyName = "Test Date Property";
167+
var updateDatabaseParameters = new DatabasesUpdateParameters();
168+
updateDatabaseParameters.Properties = new Dictionary<string, IUpdatePropertySchema>
169+
{
170+
{ "Name", new TitleUpdatePropertySchema { Title = new Dictionary<string, object>() } },
171+
{ "Test Date Property", new DateUpdatePropertySchema{ Date = new Dictionary<string, object>() } }
172+
};
173+
174+
PagesCreateParameters pagesCreateParameters = PagesCreateParametersBuilder.Create(new DatabaseParentInput
175+
{
176+
DatabaseId = _databaseId
177+
})
178+
.AddProperty("Name", new TitlePropertyValue
179+
{
180+
Title = new List<RichTextBase>
181+
{
182+
new RichTextText
183+
{
184+
Text = new Text
185+
{
186+
Content = "Test Page Title"
187+
}
188+
}
189+
}
190+
})
191+
.AddProperty(datePropertyName, new DatePropertyValue
192+
{
193+
Date = new Date()
194+
{
195+
Start = Convert.ToDateTime("2020-12-08T12:00:00Z"),
196+
End = Convert.ToDateTime("2025-12-08T12:00:00Z")
197+
}
198+
})
199+
.Build();
200+
201+
var updatedDb = await _client.Databases.UpdateAsync(_databaseId, updateDatabaseParameters);
202+
203+
var page = await _client.Pages.CreateAsync(pagesCreateParameters);
204+
205+
var setDate = page.Properties[datePropertyName] as DatePropertyValue;
206+
207+
setDate?.Date?.Start.Should().Be(Convert.ToDateTime("2020-12-08T12:00:00Z"));
208+
209+
// verify
210+
IDictionary<string, PropertyValue> testProps = new Dictionary<string, PropertyValue>();
211+
testProps.Add(datePropertyName, new DatePropertyValue() { Date = null });
212+
213+
var updatedPage = await _client.Pages.UpdateAsync(page.Id, new PagesUpdateParameters
214+
{
215+
Properties = testProps
216+
});
217+
218+
var verifyDate = updatedPage.Properties[datePropertyName] as DatePropertyValue;
219+
220+
verifyDate?.Date.Should().BeNull();
221+
222+
//cleanup
223+
await _client.Blocks.DeleteAsync(page.Id);
224+
}
158225
}
159226
}

0 commit comments

Comments
 (0)