From 083257885104e787fddc9cc38f35a6e123171d68 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Thu, 19 Aug 2021 04:49:56 +0530 Subject: [PATCH] Add missing parent unit test for Create a page API :white_check_mark: --- Src/Notion.Client/Api/Pages/PagesClient.cs | 15 +++++++ Test/Notion.UnitTests/PagesClientTests.cs | 43 ++++++++++++++++++- .../data/pages/CreatePageResponse.json | 4 ++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Src/Notion.Client/Api/Pages/PagesClient.cs b/Src/Notion.Client/Api/Pages/PagesClient.cs index 2b7adf76..a7346f24 100644 --- a/Src/Notion.Client/Api/Pages/PagesClient.cs +++ b/Src/Notion.Client/Api/Pages/PagesClient.cs @@ -16,6 +16,21 @@ public PagesClient(IRestClient client) public async Task CreateAsync(NewPage page) { + if (page == null) + { + throw new ArgumentNullException(nameof(page)); + } + + if (page.Parent == null) + { + throw new ArgumentNullException(nameof(page.Parent), "Parent is required!"); + } + + if (page.Properties == null) + { + throw new ArgumentNullException(nameof(page.Properties), "Properties are required!"); + } + return await _client.PostAsync(PagesApiUrls.Create(), page); } diff --git a/Test/Notion.UnitTests/PagesClientTests.cs b/Test/Notion.UnitTests/PagesClientTests.cs index 04957bef..1e00e59d 100644 --- a/Test/Notion.UnitTests/PagesClientTests.cs +++ b/Test/Notion.UnitTests/PagesClientTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -56,6 +57,11 @@ public async Task CreateAsync() ); var newPage = new NewPage(); + newPage.Parent = new PageParent + { + PageId = "3c357473-a281-49a4-88c0-10d2b245a589" + }; + newPage.AddProperty("Name", new TitlePropertyValue() { Title = new List() @@ -77,6 +83,8 @@ public async Task CreateAsync() page.Properties.Should().HaveCount(1); page.Properties.First().Key.Should().Be("Name"); page.IsArchived.Should().BeFalse(); + page.Parent.Should().NotBeNull(); + ((PageParent)page.Parent).PageId.Should().Be("3c357473-a281-49a4-88c0-10d2b245a589"); } [Fact] @@ -190,5 +198,38 @@ public async Task ArchivePageAsync() var updatedProperty = page.Properties.First(x => x.Key == "In stock"); ((CheckboxPropertyValue)updatedProperty.Value).Checkbox.Should().BeTrue(); } + + [Fact] + public async Task CreateAsync_Throws_ArgumentNullException_When_Parameter_Is_Null() + { + Func act = async () => await _client.CreateAsync(null); + + (await act.Should().ThrowAsync()).And.ParamName.Should().Be("page"); + } + + [Fact] + public async Task CreateAsync_Throws_ArgumentNullException_When_Parent_Is_Missing() + { + var newPage = new NewPage(); + + Func act = async () => await _client.CreateAsync(newPage); + + (await act.Should().ThrowAsync()).And.ParamName.Should().Be("Parent"); + } + + [Fact] + public async Task CreateAsync_Throws_ArgumentNullException_When_Properties_Is_Missing() + { + var newPage = new NewPage(new PageParent() + { + PageId = "3c357473-a281-49a4-88c0-10d2b245a589" + }); + + newPage.Properties = null; + + Func act = async () => await _client.CreateAsync(newPage); + + (await act.Should().ThrowAsync()).And.ParamName.Should().Be("Properties"); + } } } diff --git a/Test/Notion.UnitTests/data/pages/CreatePageResponse.json b/Test/Notion.UnitTests/data/pages/CreatePageResponse.json index 520d23bf..20a95c3c 100644 --- a/Test/Notion.UnitTests/data/pages/CreatePageResponse.json +++ b/Test/Notion.UnitTests/data/pages/CreatePageResponse.json @@ -29,5 +29,9 @@ } ] } + }, + "parent": { + "page_id": "3c357473-a281-49a4-88c0-10d2b245a589", + "type": "page_id" } } \ No newline at end of file