Skip to content

Commit a05797e

Browse files
Merge pull request #110 from notion-dotnet/test/add-unit-or-integration-tests-for-blocks-client
Add unit tests cases for blocks client ✅
2 parents e4c104c + afc72de commit a05797e

File tree

6 files changed

+292
-10
lines changed

6 files changed

+292
-10
lines changed

Src/Notion.Client/Api/Blocks/BlocksClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<PaginatedList<Block>> RetrieveChildrenAsync(string blockId, Bl
3434
return await _client.GetAsync<PaginatedList<Block>>(url, queryParams);
3535
}
3636

37-
public async Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null)
37+
public async Task<PaginatedList<Block>> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null)
3838
{
3939
if (string.IsNullOrWhiteSpace(blockId))
4040
{
@@ -45,7 +45,7 @@ public async Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildre
4545

4646
var body = (IBlocksAppendChildrenBodyParameters)parameters;
4747

48-
return await _client.PatchAsync<Block>(url, body);
48+
return await _client.PatchAsync<PaginatedList<Block>>(url, body);
4949
}
5050

5151
public async Task<Block> RetrieveAsync(string blockId)

Src/Notion.Client/Api/Blocks/IBlocksClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public interface IBlocksClient
2020
Task<Block> UpdateAsync(string blockId, IUpdateBlock updateBlock);
2121

2222
Task<PaginatedList<Block>> RetrieveChildrenAsync(string blockId, BlocksRetrieveChildrenParameters parameters = null);
23-
Task<Block> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null);
23+
Task<PaginatedList<Block>> AppendChildrenAsync(string blockId, BlocksAppendChildrenParameters parameters = null);
2424
}
2525
}

Test/Notion.UnitTests/BlocksClientTests.cs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,44 @@ public BlocksClientTests()
1818
_client = new BlocksClient(new RestClient(ClientOptions));
1919
}
2020

21-
[Fact(Skip = "Dev only")]
21+
[Fact]
2222
public async Task RetrieveBlockChildren()
2323
{
24+
// Arrange
2425
string blockId = "3c357473-a281-49a4-88c0-10d2b245a589";
26+
var path = ApiEndpoints.BlocksApiUrls.RetrieveChildren(blockId);
27+
var jsonData = await File.ReadAllTextAsync("data/blocks/RetrieveBlockChildrenResponse.json");
2528

26-
var children = await _client.RetrieveChildrenAsync(blockId, new BlocksRetrieveChildrenParameters());
29+
Server.Given(CreateGetRequestBuilder(path))
30+
.RespondWith(
31+
Response.Create()
32+
.WithStatusCode(200)
33+
.WithBody(jsonData)
34+
);
2735

28-
Assert.NotNull(children);
36+
// Act
37+
var childrenResult = await _client.RetrieveChildrenAsync(blockId, new BlocksRetrieveChildrenParameters());
38+
39+
// Assert
40+
var children = childrenResult.Results;
41+
children.Should().HaveCount(7);
2942
}
3043

31-
[Fact(Skip = "Dev only")]
44+
[Fact]
3245
public async Task AppendBlockChildren()
3346
{
34-
string blockId = "3c357473-a281-49a4-88c0-10d2b245a589";
47+
// Arrange
48+
string blockId = "7face6fd-3ef4-4b38-b1dc-c5044988eec0";
49+
var path = ApiEndpoints.BlocksApiUrls.AppendChildren(blockId);
50+
51+
var jsonData = await File.ReadAllTextAsync("data/blocks/AppendBlockChildrenResponse.json");
52+
53+
Server.Given(CreatePatchRequestBuilder(path))
54+
.RespondWith(
55+
Response.Create()
56+
.WithStatusCode(200)
57+
.WithBody(jsonData)
58+
);
3559

3660
var parameters = new BlocksAppendChildrenParameters()
3761
{
@@ -52,13 +76,52 @@ public async Task AppendBlockChildren()
5276
}
5377
}
5478
}
79+
},
80+
new ParagraphBlock()
81+
{
82+
Paragraph = new ParagraphBlock.Info
83+
{
84+
Text = new List<RichTextBase>
85+
{
86+
new RichTextText
87+
{
88+
Text = new Text
89+
{
90+
Content = "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
91+
Link = new Link
92+
{
93+
Url = "https://en.wikipedia.org/wiki/Lacinato_kale"
94+
}
95+
}
96+
}
97+
}
98+
}
5599
}
56100
}
57101
};
58102

59-
var block = await _client.AppendChildrenAsync(blockId, parameters);
103+
// Act
104+
var blocksResult = await _client.AppendChildrenAsync(blockId, parameters);
60105

61-
Assert.NotNull(block);
106+
// Assert
107+
var blocks = blocksResult.Results;
108+
blocks.Should().SatisfyRespectively(
109+
block =>
110+
{
111+
block.Type.Should().Be(BlockType.Heading_2);
112+
var headingBlock = (HeadingTwoBlock)block;
113+
var text = headingBlock.Heading_2.Text.OfType<RichTextText>().FirstOrDefault();
114+
text.Text.Content.Should().Be("Lacinato kale");
115+
},
116+
block =>
117+
{
118+
block.Type.Should().Be(BlockType.Paragraph);
119+
var paragraphBlock = (ParagraphBlock)block;
120+
var text = paragraphBlock.Paragraph.Text.OfType<RichTextText>().LastOrDefault();
121+
text.Text.Content.Should().Be("Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.");
122+
text.Text.Link.Url.Should().Be("https://en.wikipedia.org/wiki/Lacinato_kale");
123+
}
124+
);
62125
}
63126

64127
[Fact]

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<None Update="data\blocks\UpdateBlockResponse.json">
3434
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3535
</None>
36+
<None Update="data/blocks/RetrieveBlockChildrenResponse.json">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
39+
<None Update="data/blocks/AppendBlockChildrenResponse.json">
40+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
41+
</None>
3642
<None Update="data\databases\CreateDatabaseResponse.json">
3743
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3844
</None>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "block",
6+
"id": "9bc30ad4-9373-46a5-84ab-0a7845ee52e6",
7+
"created_time": "2021-03-16T16:31:00.000Z",
8+
"last_edited_time": "2021-03-16T16:32:00.000Z",
9+
"has_children": false,
10+
"type": "heading_2",
11+
"heading_2": {
12+
"text": [
13+
{
14+
"type": "text",
15+
"text": {
16+
"content": "Lacinato kale",
17+
"link": null
18+
},
19+
"annotations": {
20+
"bold": false,
21+
"italic": false,
22+
"strikethrough": false,
23+
"underline": false,
24+
"code": false,
25+
"color": "default"
26+
},
27+
"plain_text": "Lacinato kale",
28+
"href": null
29+
}
30+
]
31+
}
32+
},
33+
{
34+
"object": "block",
35+
"id": "7face6fd-3ef4-4b38-b1dc-c5044988eec0",
36+
"created_time": "2021-03-16T16:34:00.000Z",
37+
"last_edited_time": "2021-03-16T16:36:00.000Z",
38+
"has_children": false,
39+
"type": "paragraph",
40+
"paragraph": {
41+
"text": [
42+
{
43+
"type": "text",
44+
"text": {
45+
"content": "Lacinato kale",
46+
"link": {
47+
"url": "https://en.wikipedia.org/wiki/Lacinato_kale"
48+
}
49+
},
50+
"annotations": {
51+
"bold": false,
52+
"italic": false,
53+
"strikethrough": false,
54+
"underline": false,
55+
"code": false,
56+
"color": "default"
57+
},
58+
"plain_text": "Lacinato kale",
59+
"href": "https://en.wikipedia.org/wiki/Lacinato_kale"
60+
},
61+
{
62+
"type": "text",
63+
"text": {
64+
"content": "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
65+
"link": {
66+
"url": "https://en.wikipedia.org/wiki/Lacinato_kale"
67+
}
68+
},
69+
"annotations": {
70+
"bold": false,
71+
"italic": false,
72+
"strikethrough": false,
73+
"underline": false,
74+
"code": false,
75+
"color": "default"
76+
},
77+
"plain_text": "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
78+
"href": "https://en.wikipedia.org/wiki/Lacinato_kale"
79+
}
80+
]
81+
}
82+
}
83+
],
84+
"next_cursor": null,
85+
"has_more": false
86+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "block",
6+
"id": "63ef2bbd-6420-4a34-95b9-7e2f15fc9437",
7+
"created_time": "2021-05-29T16:22:00.000Z",
8+
"last_edited_time": "2021-05-29T16:22:00.000Z",
9+
"has_children": false,
10+
"archived": false,
11+
"type": "paragraph",
12+
"paragraph": {
13+
"text": []
14+
}
15+
},
16+
{
17+
"object": "block",
18+
"id": "62c66d15-2598-4217-939a-ea3c73b674a5",
19+
"created_time": "2021-05-30T09:25:00.000Z",
20+
"last_edited_time": "2021-05-30T09:25:00.000Z",
21+
"has_children": false,
22+
"archived": false,
23+
"type": "heading_2",
24+
"heading_2": {
25+
"text": [
26+
{
27+
"type": "text",
28+
"text": {
29+
"content": "Lacinato kale",
30+
"link": null
31+
},
32+
"annotations": {
33+
"bold": false,
34+
"italic": false,
35+
"strikethrough": false,
36+
"underline": false,
37+
"code": false,
38+
"color": "default"
39+
},
40+
"plain_text": "Lacinato kale",
41+
"href": null
42+
}
43+
]
44+
}
45+
},
46+
{
47+
"object": "block",
48+
"id": "8fa4e0d1-a3b0-40ca-99c0-d04c4439a4d2",
49+
"created_time": "2021-05-30T09:36:00.000Z",
50+
"last_edited_time": "2021-05-30T09:36:00.000Z",
51+
"has_children": false,
52+
"archived": false,
53+
"type": "heading_2",
54+
"heading_2": {
55+
"text": [
56+
{
57+
"type": "text",
58+
"text": {
59+
"content": "Lacinato kale",
60+
"link": null
61+
},
62+
"annotations": {
63+
"bold": false,
64+
"italic": false,
65+
"strikethrough": false,
66+
"underline": false,
67+
"code": false,
68+
"color": "default"
69+
},
70+
"plain_text": "Lacinato kale",
71+
"href": null
72+
}
73+
]
74+
}
75+
},
76+
{
77+
"object": "block",
78+
"id": "666f0c12-0e0a-4583-91bb-58bb7d3e1739",
79+
"created_time": "2021-08-18T21:12:00.000Z",
80+
"last_edited_time": "2021-08-18T21:12:00.000Z",
81+
"has_children": false,
82+
"archived": false,
83+
"type": "paragraph",
84+
"paragraph": {
85+
"text": []
86+
}
87+
},
88+
{
89+
"object": "block",
90+
"id": "416c4065-0014-4aca-bbd9-d1c28acc0375",
91+
"created_time": "2021-08-18T23:00:00.000Z",
92+
"last_edited_time": "2021-08-18T23:00:00.000Z",
93+
"has_children": false,
94+
"archived": false,
95+
"type": "child_page",
96+
"child_page": {
97+
"title": ""
98+
}
99+
},
100+
{
101+
"object": "block",
102+
"id": "ca3b5be9-e2d2-4384-9e01-bd23ab90d924",
103+
"created_time": "2021-08-18T23:00:00.000Z",
104+
"last_edited_time": "2021-08-18T23:00:00.000Z",
105+
"has_children": false,
106+
"archived": false,
107+
"type": "child_page",
108+
"child_page": {
109+
"title": ""
110+
}
111+
},
112+
{
113+
"object": "block",
114+
"id": "d57fc098-132e-4835-a511-a7850fb187c0",
115+
"created_time": "2021-09-09T05:22:00.000Z",
116+
"last_edited_time": "2021-09-09T05:22:00.000Z",
117+
"has_children": false,
118+
"archived": false,
119+
"type": "paragraph",
120+
"paragraph": {
121+
"text": []
122+
}
123+
}
124+
],
125+
"next_cursor": null,
126+
"has_more": false
127+
}

0 commit comments

Comments
 (0)