Skip to content

Commit b1f0192

Browse files
Add unit test for Update Database api endpoint ✅
1 parent 4442bd2 commit b1f0192

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

Test/Notion.UnitTests/DatabasesClientTests.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,108 @@ public async Task CreateDatabaseAsync()
255255
}
256256
);
257257
}
258+
259+
[Fact]
260+
public async Task UpdateDatabaseAsync()
261+
{
262+
var databaseId = "1e9eee34-9c5c-4fe6-a4e1-8244eb141ed8";
263+
var path = ApiEndpoints.DatabasesApiUrls.Update(databaseId);
264+
var jsonData = await File.ReadAllTextAsync("data/databases/UpdateDatabaseResponse.json");
265+
266+
Server.Given(CreatePatchRequestBuilder(path))
267+
.RespondWith(
268+
Response.Create()
269+
.WithStatusCode(200)
270+
.WithBody(jsonData)
271+
);
272+
273+
var updateDatabaseParameters = new DatabasesUpdateParameters();
274+
275+
updateDatabaseParameters.Title = new List<RichTextBaseInput>
276+
{
277+
new RichTextTextInput
278+
{
279+
Text = new Text
280+
{
281+
Content = "Grocery List New",
282+
Link = null
283+
}
284+
}
285+
};
286+
287+
updateDatabaseParameters.Properties = new Dictionary<string, IUpdatePropertySchema>
288+
{
289+
{ "Name", new TitleUpdatePropertySchema { Title = new Dictionary<string, object>() } },
290+
{ "Price", new NumberUpdatePropertySchema { Number = new Number { Format = NumberFormat.Yen } } },
291+
{ "Food group", new SelectUpdatePropertySchema
292+
{
293+
Select = new OptionWrapper<SelectOption>
294+
{
295+
Options = new List<SelectOption>
296+
{
297+
new SelectOption
298+
{
299+
Color = Color.Green,
300+
Name = "🥦Vegetables"
301+
},
302+
new SelectOption
303+
{
304+
Color = Color.Red,
305+
Name = "🍎Fruit"
306+
},
307+
new SelectOption
308+
{
309+
Color = Color.Yellow,
310+
Name = "💪Protein"
311+
}
312+
}
313+
}
314+
}
315+
},
316+
{ "Last ordered", new DateUpdatePropertySchema{ Date = new Dictionary<string, object>() } }
317+
};
318+
319+
var database = await _client.UpdateAsync(databaseId, updateDatabaseParameters);
320+
321+
database.Parent.Type.Should().Be(ParentType.PageId);
322+
database.Parent.Should().BeOfType<PageParent>();
323+
((PageParent)database.Parent).PageId.Should().Be("533578e3-edf1-4c0a-91a9-da6b09bac3ee");
324+
325+
database.Properties.Should().HaveCount(4);
326+
327+
328+
database.Title.Should().ContainSingle();
329+
database.Title.Should().SatisfyRespectively(
330+
title =>
331+
{
332+
title.Should().BeAssignableTo<RichTextText>();
333+
((RichTextText)title).Text.Content.Should().Be("Grocery List New");
334+
}
335+
);
336+
337+
338+
var selectOptions = (SelectProperty)database.Properties["Food group"];
339+
selectOptions.Name.Should().Be("Food group");
340+
selectOptions.Select.Options.Should().SatisfyRespectively(
341+
option =>
342+
{
343+
option.Name.Should().Be("🥦Vegetables");
344+
option.Color.Should().Be(Color.Green);
345+
},
346+
option =>
347+
{
348+
option.Name.Should().Be("🍎Fruit");
349+
option.Color.Should().Be(Color.Red);
350+
},
351+
option =>
352+
{
353+
option.Name.Should().Be("💪Protein");
354+
option.Color.Should().Be(Color.Yellow);
355+
}
356+
);
357+
358+
var price = (NumberProperty)database.Properties["Price"];
359+
price.Number.Format.Should().Be(NumberFormat.Yen);
360+
}
258361
}
259362
}

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<None Update="data\databases\DatabasePropertyObjectContainParentProperty.json">
3434
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3535
</None>
36+
<None Update="data\databases\UpdateDatabaseResponse.json">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
3639
<None Update="data\pages\CreatePageResponse.json">
3740
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3841
</None>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"object": "database",
3+
"id": "1e9eee34-9c5c-4fe6-a4e1-8244eb141ed8",
4+
"created_time": "2021-08-18T17:39:00.000Z",
5+
"last_edited_time": "2021-08-18T20:00:00.000Z",
6+
"title": [
7+
{
8+
"type": "text",
9+
"text": {
10+
"content": "Grocery List New",
11+
"link": null
12+
},
13+
"annotations": {
14+
"bold": false,
15+
"italic": false,
16+
"strikethrough": false,
17+
"underline": false,
18+
"code": false,
19+
"color": "default"
20+
},
21+
"plain_text": "Grocery List New",
22+
"href": null
23+
}
24+
],
25+
"properties": {
26+
"Price": {
27+
"id": "@xZm",
28+
"name": "Price",
29+
"type": "number",
30+
"number": {
31+
"format": "yen"
32+
}
33+
},
34+
"Last ordered": {
35+
"id": "SN;?",
36+
"name": "Last ordered",
37+
"type": "date",
38+
"date": {}
39+
},
40+
"Food group": {
41+
"id": "zIZQ",
42+
"name": "Food group",
43+
"type": "select",
44+
"select": {
45+
"options": [
46+
{
47+
"id": "58c5640a-d34d-4d11-9ce4-2dcd279fa9b7",
48+
"name": "🥦Vegetables",
49+
"color": "green"
50+
},
51+
{
52+
"id": "3ac2b777-180e-4f45-aa3f-5d49a5bd4344",
53+
"name": "🍎Fruit",
54+
"color": "red"
55+
},
56+
{
57+
"id": "f0b31ad9-a33b-491c-aeb5-15dffb40dae7",
58+
"name": "💪Protein",
59+
"color": "yellow"
60+
}
61+
]
62+
}
63+
},
64+
"Name": {
65+
"id": "title",
66+
"name": "Name",
67+
"type": "title",
68+
"title": {}
69+
}
70+
},
71+
"parent": {
72+
"type": "page_id",
73+
"page_id": "533578e3-edf1-4c0a-91a9-da6b09bac3ee"
74+
}
75+
}

0 commit comments

Comments
 (0)