Skip to content

Commit f48fa7b

Browse files
Merge pull request #102 from notion-dotnet/tests/69-add-test-to-check-missing-parent
Add missing parent unit test for Create a page API ✅
2 parents 8f8f4b9 + 0832578 commit f48fa7b

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

Src/Notion.Client/Api/Pages/PagesClient.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ public PagesClient(IRestClient client)
1616

1717
public async Task<Page> CreateAsync(NewPage page)
1818
{
19+
if (page == null)
20+
{
21+
throw new ArgumentNullException(nameof(page));
22+
}
23+
24+
if (page.Parent == null)
25+
{
26+
throw new ArgumentNullException(nameof(page.Parent), "Parent is required!");
27+
}
28+
29+
if (page.Properties == null)
30+
{
31+
throw new ArgumentNullException(nameof(page.Properties), "Properties are required!");
32+
}
33+
1934
return await _client.PostAsync<Page>(PagesApiUrls.Create(), page);
2035
}
2136

Test/Notion.UnitTests/PagesClientTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -56,6 +57,11 @@ public async Task CreateAsync()
5657
);
5758

5859
var newPage = new NewPage();
60+
newPage.Parent = new PageParent
61+
{
62+
PageId = "3c357473-a281-49a4-88c0-10d2b245a589"
63+
};
64+
5965
newPage.AddProperty("Name", new TitlePropertyValue()
6066
{
6167
Title = new List<RichTextBase>()
@@ -77,6 +83,8 @@ public async Task CreateAsync()
7783
page.Properties.Should().HaveCount(1);
7884
page.Properties.First().Key.Should().Be("Name");
7985
page.IsArchived.Should().BeFalse();
86+
page.Parent.Should().NotBeNull();
87+
((PageParent)page.Parent).PageId.Should().Be("3c357473-a281-49a4-88c0-10d2b245a589");
8088
}
8189

8290
[Fact]
@@ -190,5 +198,38 @@ public async Task ArchivePageAsync()
190198
var updatedProperty = page.Properties.First(x => x.Key == "In stock");
191199
((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue();
192200
}
201+
202+
[Fact]
203+
public async Task CreateAsync_Throws_ArgumentNullException_When_Parameter_Is_Null()
204+
{
205+
Func<Task> act = async () => await _client.CreateAsync(null);
206+
207+
(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("page");
208+
}
209+
210+
[Fact]
211+
public async Task CreateAsync_Throws_ArgumentNullException_When_Parent_Is_Missing()
212+
{
213+
var newPage = new NewPage();
214+
215+
Func<Task> act = async () => await _client.CreateAsync(newPage);
216+
217+
(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("Parent");
218+
}
219+
220+
[Fact]
221+
public async Task CreateAsync_Throws_ArgumentNullException_When_Properties_Is_Missing()
222+
{
223+
var newPage = new NewPage(new PageParent()
224+
{
225+
PageId = "3c357473-a281-49a4-88c0-10d2b245a589"
226+
});
227+
228+
newPage.Properties = null;
229+
230+
Func<Task> act = async () => await _client.CreateAsync(newPage);
231+
232+
(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("Properties");
233+
}
193234
}
194235
}

Test/Notion.UnitTests/data/pages/CreatePageResponse.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@
2929
}
3030
]
3131
}
32+
},
33+
"parent": {
34+
"page_id": "3c357473-a281-49a4-88c0-10d2b245a589",
35+
"type": "page_id"
3236
}
3337
}

0 commit comments

Comments
 (0)