diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/FormulaPropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/FormulaPropertySchema.cs new file mode 100644 index 00000000..e1802d38 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesCreateParameters/PropertySchema/FormulaPropertySchema.cs @@ -0,0 +1,7 @@ +namespace Notion.Client +{ + public class FormulaPropertySchema : IPropertySchema + { + public Formula Formula { get; set; } + } +} diff --git a/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FormulaUpdatePropertySchema.cs b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FormulaUpdatePropertySchema.cs new file mode 100644 index 00000000..ef315458 --- /dev/null +++ b/Src/Notion.Client/Api/Databases/RequestParams/DatabasesUpdateParameters/PropertySchema/FormulaUpdatePropertySchema.cs @@ -0,0 +1,7 @@ +namespace Notion.Client +{ + public class FormulaUpdatePropertySchema : IUpdatePropertySchema + { + public Formula Formula { get; set; } + } +} diff --git a/Test/Notion.UnitTests/DatabasesClientTests.cs b/Test/Notion.UnitTests/DatabasesClientTests.cs index 1e8ab15e..10740a29 100644 --- a/Test/Notion.UnitTests/DatabasesClientTests.cs +++ b/Test/Notion.UnitTests/DatabasesClientTests.cs @@ -356,5 +356,56 @@ public async Task UpdateDatabaseAsync() var price = (NumberProperty)database.Properties["Price"]; price.Number.Format.Should().Be(NumberFormat.Yen); } + + [Fact] + public async Task FormulaPropertyCanBeSetWhenCreatingDatabase() + { + var pageId = "98ad959b-2b6a-4774-80ee-00246fb0ea9b"; + var path = ApiEndpoints.DatabasesApiUrls.Create; + var jsonData = await File.ReadAllTextAsync("data/databases/FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json"); + + Server.Given(CreatePostRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); + + var createDatabaseParameters = new DatabasesCreateParameters(); + + createDatabaseParameters.Parent = new ParentPageInput + { + PageId = pageId + }; + + createDatabaseParameters.Title = new List + { + new RichTextTextInput + { + Text = new Text + { + Content = "Grocery List", + Link = null + } + } + }; + + createDatabaseParameters.Properties = new Dictionary + { + { "Cost of next trip", new FormulaPropertySchema { Formula = new Formula { Expression = "if(prop(\"In stock\"), 0, prop(\"Price\"))" } } }, + { "Price", new NumberPropertySchema { Number = new Number { Format = NumberFormat.Dollar } } } + }; + + var database = await _client.CreateAsync(createDatabaseParameters); + + database.Parent.Type.Should().Be(ParentType.PageId); + database.Parent.Should().BeOfType(); + ((PageParent)database.Parent).PageId.Should().Be(pageId); + + database.Properties.Should().HaveCount(2); + + var formulaProperty = (FormulaProperty)database.Properties["Cost of next trip"]; + formulaProperty.Formula.Expression.Should().Be("if(prop(\"In stock\"), 0, prop(\"Price\"))"); + } } } diff --git a/Test/Notion.UnitTests/Notion.UnitTests.csproj b/Test/Notion.UnitTests/Notion.UnitTests.csproj index 8b4c91e4..2cf488b2 100644 --- a/Test/Notion.UnitTests/Notion.UnitTests.csproj +++ b/Test/Notion.UnitTests/Notion.UnitTests.csproj @@ -81,6 +81,9 @@ Always + + Always + diff --git a/Test/Notion.UnitTests/data/databases/FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json b/Test/Notion.UnitTests/data/databases/FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json new file mode 100644 index 00000000..e0a57252 --- /dev/null +++ b/Test/Notion.UnitTests/data/databases/FormulaPropertyCanBeSetWhenCreatingDatabaseResponse.json @@ -0,0 +1,47 @@ +{ + "object": "database", + "id": "c23f0085-a061-41c0-b8a6-cbe14d15a4de", + "created_time": "2021-08-20T16:08:00.000Z", + "last_edited_time": "2021-08-20T16:08:00.000Z", + "title": [ + { + "type": "text", + "text": { + "content": "Grocery List", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Grocery List", + "href": null + } + ], + "properties": { + "Cost of next trip": { + "id": "Rbq\\", + "name": "Cost of next trip", + "type": "formula", + "formula": { + "expression": "if(prop(\"In stock\"), 0, prop(\"Price\"))" + } + }, + "Price": { + "id": "u|<{", + "name": "Price", + "type": "number", + "number": { + "format": "dollar" + } + } + }, + "parent": { + "type": "page_id", + "page_id": "98ad959b-2b6a-4774-80ee-00246fb0ea9b" + } +}