Skip to content

Commit

Permalink
Merge pull request #102 from notion-dotnet/tests/69-add-test-to-check…
Browse files Browse the repository at this point in the history
…-missing-parent

Add missing parent unit test for Create a page API ✅
  • Loading branch information
KoditkarVedant authored Aug 18, 2021
2 parents 8f8f4b9 + 0832578 commit f48fa7b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Src/Notion.Client/Api/Pages/PagesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ public PagesClient(IRestClient client)

public async Task<Page> 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<Page>(PagesApiUrls.Create(), page);
}

Expand Down
43 changes: 42 additions & 1 deletion Test/Notion.UnitTests/PagesClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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<RichTextBase>()
Expand All @@ -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]
Expand Down Expand Up @@ -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<Task> act = async () => await _client.CreateAsync(null);

(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("page");
}

[Fact]
public async Task CreateAsync_Throws_ArgumentNullException_When_Parent_Is_Missing()
{
var newPage = new NewPage();

Func<Task> act = async () => await _client.CreateAsync(newPage);

(await act.Should().ThrowAsync<ArgumentNullException>()).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<Task> act = async () => await _client.CreateAsync(newPage);

(await act.Should().ThrowAsync<ArgumentNullException>()).And.ParamName.Should().Be("Properties");
}
}
}
4 changes: 4 additions & 0 deletions Test/Notion.UnitTests/data/pages/CreatePageResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
}
]
}
},
"parent": {
"page_id": "3c357473-a281-49a4-88c0-10d2b245a589",
"type": "page_id"
}
}

0 comments on commit f48fa7b

Please sign in to comment.