diff --git a/Test/Notion.UnitTests/DatabasesClientTests.cs b/Test/Notion.UnitTests/DatabasesClientTests.cs index f9c09f3a..ddf65b4d 100644 --- a/Test/Notion.UnitTests/DatabasesClientTests.cs +++ b/Test/Notion.UnitTests/DatabasesClientTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Threading.Tasks; using FluentAssertions; using Notion.Client; @@ -16,29 +17,106 @@ public DatabasesClientTests() _client = new DatabasesClient(new RestClient(ClientOptions)); } - [Fact(Skip = "Internal Testing Purpose")] + [Fact] public async Task ListDatabasesAsync() { - var databasesList = await _client.ListAsync(); - Assert.NotNull(databasesList); + var path = ApiEndpoints.DatabasesApiUrls.List(); + var jsonData = await File.ReadAllTextAsync("data/databases/DatabasesListResponse.json"); + + Server.Given(CreateGetRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); + + var databases = await _client.ListAsync(); + + databases.Results.Should().HaveCount(3); + + foreach (var database in databases.Results) + { + database.Parent.Should().BeAssignableTo(); + foreach (var property in database.Properties) + { + property.Key.Should().Be(property.Value.Name); + } + } } - [Fact(Skip = "Internal Testing Purpose")] - public async Task RetrieveDatabaseAsync() + [Fact] + public async Task QueryAsync() { - var databaseId = ""; - var database = await _client.RetrieveAsync(databaseId); - Assert.NotNull(database); + var databaseId = "f0212efc-caf6-4afc-87f6-1c06f1dfc8a1"; + var path = ApiEndpoints.DatabasesApiUrls.Query(databaseId); + var jsonData = await File.ReadAllTextAsync("data/databases/DatabasesQueryResponse.json"); + + Server.Given(CreatePostRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); + + var databasesQueryParams = new DatabasesQueryParameters + { + Filter = new CompoundFilter + { + Or = new List { + new CheckboxFilter( + "In stock", + true + ), + new NumberFilter( + "Cost of next trip", + greaterThanOrEqualTo: 2 + ) + }, + }, + Sorts = new List + { + new Sort + { + Property = "Last ordered", + Direction = Direction.Ascending + } + } + }; + + var pagesPaginatedList = await _client.QueryAsync(databaseId, databasesQueryParams); + + pagesPaginatedList.Results.Should().ContainSingle(); + + foreach (var page in pagesPaginatedList.Results) + { + page.Parent.Should().BeAssignableTo(); + page.Object.Should().Be(ObjectType.Page); + } } - [Fact(Skip = "Internal Testing Purpose")] - public async Task QueryAsync() + [Fact] + public async Task RetrieveDatabaseAsync() { - var databaseId = ""; - var databasesQueryParameters = new DatabasesQueryParameters { }; - var pagesList = await _client.QueryAsync(databaseId, databasesQueryParameters); + var databaseId = "f0212efc-caf6-4afc-87f6-1c06f1dfc8a1"; + var path = ApiEndpoints.DatabasesApiUrls.Retrieve(databaseId); + var jsonData = await File.ReadAllTextAsync("data/databases/DatabaseRetrieveResponse.json"); + + Server.Given(CreateGetRequestBuilder(path)) + .RespondWith( + Response.Create() + .WithStatusCode(200) + .WithBody(jsonData) + ); - Assert.NotNull(pagesList); + var database = await _client.RetrieveAsync(databaseId); + + database.Parent.Type.Should().Be(ParentType.PageId); + database.Parent.Should().BeOfType(); + ((PageParent)database.Parent).PageId.Should().Be("649089db-8984-4051-98fb-a03593b852d8"); + foreach (var property in database.Properties) + { + property.Key.Should().Be(property.Value.Name); + } } [Fact] diff --git a/Test/Notion.UnitTests/Notion.UnitTests.csproj b/Test/Notion.UnitTests/Notion.UnitTests.csproj index df1afc3c..bde5608a 100644 --- a/Test/Notion.UnitTests/Notion.UnitTests.csproj +++ b/Test/Notion.UnitTests/Notion.UnitTests.csproj @@ -42,6 +42,15 @@ Always + + Always + + + Always + + + Always + diff --git a/Test/Notion.UnitTests/data/databases/DatabaseRetrieveResponse.json b/Test/Notion.UnitTests/data/databases/DatabaseRetrieveResponse.json new file mode 100644 index 00000000..5ed09bb1 --- /dev/null +++ b/Test/Notion.UnitTests/data/databases/DatabaseRetrieveResponse.json @@ -0,0 +1,89 @@ +{ + "object": "database", + "id": "f0212efc-caf6-4afc-87f6-1c06f1dfc8a1", + "created_time": "2021-05-22T18:44:00.000Z", + "last_edited_time": "2021-05-23T12:29:00.000Z", + "title": [ + { + "type": "text", + "text": { + "content": "sample table", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "sample table", + "href": null + } + ], + "properties": { + "Tags": { + "id": "YG~h", + "name": "Tags", + "type": "multi_select", + "multi_select": { + "options": [] + } + }, + "SimpleText": { + "id": "_Dfp", + "name": "SimpleText", + "type": "rich_text", + "rich_text": {} + }, + "Column": { + "id": "bxhl", + "name": "Column", + "type": "multi_select", + "multi_select": { + "options": [ + { + "id": "5a44a233-33be-435e-b358-2c0ed1799dcf", + "name": "what", + "color": "gray" + } + ] + } + }, + "SelectProp": { + "id": "eZ[y", + "name": "SelectProp", + "type": "select", + "select": { + "options": [ + { + "id": "362dc255-c867-4543-b3ea-7bd988638228", + "name": "Female", + "color": "green" + } + ] + } + }, + "Property": { + "id": "zDGa", + "name": "Property", + "type": "relation", + "relation": { + "database_id": "f86f2262-0751-40f2-8f63-e3f7a3c39fcb", + "synced_property_name": "Related to sample table (Property)", + "synced_property_id": "VQ}{" + } + }, + "Name": { + "id": "title", + "name": "Name", + "type": "title", + "title": {} + } + }, + "parent": { + "type": "page_id", + "page_id": "649089db-8984-4051-98fb-a03593b852d8" + } +} diff --git a/Test/Notion.UnitTests/data/databases/DatabasesListResponse.json b/Test/Notion.UnitTests/data/databases/DatabasesListResponse.json new file mode 100644 index 00000000..ea380375 --- /dev/null +++ b/Test/Notion.UnitTests/data/databases/DatabasesListResponse.json @@ -0,0 +1,216 @@ +{ + "object": "list", + "results": [ + { + "object": "database", + "id": "f0212efc-caf6-4afc-87f6-1c06f1dfc8a1", + "created_time": "2021-05-22T18:44:00.000Z", + "last_edited_time": "2021-05-23T12:29:00.000Z", + "title": [ + { + "type": "text", + "text": { + "content": "sample table", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "sample table", + "href": null + } + ], + "properties": { + "Tags": { + "id": "YG~h", + "name": "Tags", + "type": "multi_select", + "multi_select": { + "options": [] + } + }, + "SimpleText": { + "id": "_Dfp", + "name": "SimpleText", + "type": "rich_text", + "rich_text": {} + }, + "Column": { + "id": "bxhl", + "name": "Column", + "type": "multi_select", + "multi_select": { + "options": [ + { + "id": "5a44a233-33be-435e-b358-2c0ed1799dcf", + "name": "what", + "color": "gray" + } + ] + } + }, + "SelectProp": { + "id": "eZ[y", + "name": "SelectProp", + "type": "select", + "select": { + "options": [ + { + "id": "362dc255-c867-4543-b3ea-7bd988638228", + "name": "Female", + "color": "green" + } + ] + } + }, + "Property": { + "id": "zDGa", + "name": "Property", + "type": "relation", + "relation": { + "database_id": "f86f2262-0751-40f2-8f63-e3f7a3c39fcb", + "synced_property_name": "Related to sample table (Property)", + "synced_property_id": "VQ}{" + } + }, + "Name": { + "id": "title", + "name": "Name", + "type": "title", + "title": {} + } + }, + "parent": { + "type": "page_id", + "page_id": "649089db-8984-4051-98fb-a03593b852d8" + } + }, + { + "object": "database", + "id": "b38b2eed-282c-40f2-b454-cae5d882fef5", + "created_time": "2021-05-22T18:43:00.000Z", + "last_edited_time": "2021-05-22T18:43:00.000Z", + "title": [], + "properties": { + "Created": { + "id": "?uvZ", + "name": "Created", + "type": "created_time", + "created_time": {} + }, + "Tags": { + "id": "{>_o", + "name": "Tags", + "type": "multi_select", + "multi_select": { + "options": [] + } + }, + "Name": { + "id": "title", + "name": "Name", + "type": "title", + "title": {} + } + }, + "parent": { + "type": "page_id", + "page_id": "649089db-8984-4051-98fb-a03593b852d8" + } + }, + { + "object": "database", + "id": "f86f2262-0751-40f2-8f63-e3f7a3c39fcb", + "created_time": "2021-05-22T18:30:00.000Z", + "last_edited_time": "2021-06-02T18:32:00.000Z", + "title": [ + { + "type": "text", + "text": { + "content": "SampleDB", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "SampleDB", + "href": null + } + ], + "properties": { + "Property": { + "id": "CfU;", + "type": "checkbox", + "checkbox": false + } + } + } + ], + "has_more": false, + "next_cursor": null +}