Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 93 additions & 15 deletions Test/Notion.UnitTests/DatabasesClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Notion.Client;
Expand All @@ -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<IDatabaseParent>();
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<Filter> {
new CheckboxFilter(
"In stock",
true
),
new NumberFilter(
"Cost of next trip",
greaterThanOrEqualTo: 2
)
},
},
Sorts = new List<Sort>
{
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<IPageParent>();
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>();
((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]
Expand Down
9 changes: 9 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
<None Update="data\databases\DatabasePropertyObjectContainNameProperty.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\databases\DatabaseRetrieveResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\databases\DatabasesListResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\databases\DatabasesQueryResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
89 changes: 89 additions & 0 deletions Test/Notion.UnitTests/data/databases/DatabaseRetrieveResponse.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading