Skip to content

Commit ad418ca

Browse files
Merge pull request #119 from notion-dotnet/feature/103-formula-property-can-now-be-created-and-updated-in-database
Add support to create and update Formula property in database ✨
2 parents fd80df4 + 44185cb commit ad418ca

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Notion.Client
2+
{
3+
public class FormulaPropertySchema : IPropertySchema
4+
{
5+
public Formula Formula { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Notion.Client
2+
{
3+
public class FormulaUpdatePropertySchema : IUpdatePropertySchema
4+
{
5+
public Formula Formula { get; set; }
6+
}
7+
}

Test/Notion.UnitTests/DatabasesClientTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,5 +356,56 @@ public async Task UpdateDatabaseAsync()
356356
var price = (NumberProperty)database.Properties["Price"];
357357
price.Number.Format.Should().Be(NumberFormat.Yen);
358358
}
359+
360+
[Fact]
361+
public async Task FormulaPropertyCanBeSetWhenCreatingDatabase()
362+
{
363+
var pageId = "98ad959b-2b6a-4774-80ee-00246fb0ea9b";
364+
var path = ApiEndpoints.DatabasesApiUrls.Create;
365+
var jsonData = await File.ReadAllTextAsync("data/databases/FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json");
366+
367+
Server.Given(CreatePostRequestBuilder(path))
368+
.RespondWith(
369+
Response.Create()
370+
.WithStatusCode(200)
371+
.WithBody(jsonData)
372+
);
373+
374+
var createDatabaseParameters = new DatabasesCreateParameters();
375+
376+
createDatabaseParameters.Parent = new ParentPageInput
377+
{
378+
PageId = pageId
379+
};
380+
381+
createDatabaseParameters.Title = new List<RichTextBaseInput>
382+
{
383+
new RichTextTextInput
384+
{
385+
Text = new Text
386+
{
387+
Content = "Grocery List",
388+
Link = null
389+
}
390+
}
391+
};
392+
393+
createDatabaseParameters.Properties = new Dictionary<string, IPropertySchema>
394+
{
395+
{ "Cost of next trip", new FormulaPropertySchema { Formula = new Formula { Expression = "if(prop(\"In stock\"), 0, prop(\"Price\"))" } } },
396+
{ "Price", new NumberPropertySchema { Number = new Number { Format = NumberFormat.Dollar } } }
397+
};
398+
399+
var database = await _client.CreateAsync(createDatabaseParameters);
400+
401+
database.Parent.Type.Should().Be(ParentType.PageId);
402+
database.Parent.Should().BeOfType<PageParent>();
403+
((PageParent)database.Parent).PageId.Should().Be(pageId);
404+
405+
database.Properties.Should().HaveCount(2);
406+
407+
var formulaProperty = (FormulaProperty)database.Properties["Cost of next trip"];
408+
formulaProperty.Formula.Expression.Should().Be("if(prop(\"In stock\"), 0, prop(\"Price\"))");
409+
}
359410
}
360411
}

Test/Notion.UnitTests/Notion.UnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
<None Update="data\search\SearchResponse.json">
8282
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8383
</None>
84+
<None Update="data\databases\FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json">
85+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
86+
</None>
8487
</ItemGroup>
8588

8689
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"object": "database",
3+
"id": "c23f0085-a061-41c0-b8a6-cbe14d15a4de",
4+
"created_time": "2021-08-20T16:08:00.000Z",
5+
"last_edited_time": "2021-08-20T16:08:00.000Z",
6+
"title": [
7+
{
8+
"type": "text",
9+
"text": {
10+
"content": "Grocery List",
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",
22+
"href": null
23+
}
24+
],
25+
"properties": {
26+
"Cost of next trip": {
27+
"id": "Rbq\\",
28+
"name": "Cost of next trip",
29+
"type": "formula",
30+
"formula": {
31+
"expression": "if(prop(\"In stock\"), 0, prop(\"Price\"))"
32+
}
33+
},
34+
"Price": {
35+
"id": "u|<{",
36+
"name": "Price",
37+
"type": "number",
38+
"number": {
39+
"format": "dollar"
40+
}
41+
}
42+
},
43+
"parent": {
44+
"type": "page_id",
45+
"page_id": "98ad959b-2b6a-4774-80ee-00246fb0ea9b"
46+
}
47+
}

0 commit comments

Comments
 (0)