Skip to content

Commit

Permalink
Merge pull request #112 from notion-dotnet/test/add-unit-or-integrati…
Browse files Browse the repository at this point in the history
…on-tests-for-search-client

Add unit or integration tests for search client ✅
  • Loading branch information
KoditkarVedant authored Sep 9, 2021
2 parents a05797e + bdb1f56 commit ad0532d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Src/Notion.Client/Api/Search/Parameters/SearchSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class SearchSort
{
public SearchDirection Direction { get; set; }

[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime Timestamp { get; set; }
public string Timestamp { get; set; }
}
}
3 changes: 3 additions & 0 deletions Test/Notion.UnitTests/Notion.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
<None Update="data\users\RetrieveUserResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\search\SearchResponse.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions Test/Notion.UnitTests/SearchClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Notion.Client;
using WireMock.ResponseBuilders;
using Xunit;

namespace Notion.UnitTests
{
public class SearchClientTest : ApiTestBase
{
private readonly SearchClient _client;

public SearchClientTest()
{
_client = new SearchClient(new RestClient(ClientOptions));
}

[Fact]
public async Task Search()
{
// Arrange
var path = ApiEndpoints.SearchApiUrls.Search();
var jsonData = await File.ReadAllTextAsync("data/search/SearchResponse.json");

Server.Given(CreatePostRequestBuilder(path))
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(jsonData)
);

SearchParameters searchParameters = new SearchParameters()
{
Query = "External tasks",
Sort = new SearchSort
{
Direction = SearchDirection.Ascending,
Timestamp = "last_edited_time"
}
};

// Act
var searchResult = await _client.SearchAsync(searchParameters);

// Assert
var results = searchResult.Results;

results.Should().SatisfyRespectively(
obj =>
{
obj.Object.Should().Be(ObjectType.Database);

var database = (Database)obj;
database.Properties.Should().HaveCount(2);
},
obj =>
{
obj.Object.Should().Be(ObjectType.Page);

var page = (Page)obj;
page.IsArchived.Should().BeFalse();
page.Properties.Should().HaveCount(1);
page.Parent.Should().BeAssignableTo<DatabaseParent>();
((DatabaseParent)page.Parent).DatabaseId.Should().Be("e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6");
}
);
}
}
}
82 changes: 82 additions & 0 deletions Test/Notion.UnitTests/data/search/SearchResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"has_more": false,
"next_cursor": null,
"object": "list",
"results": [
{
"created_time": "2021-04-22T22:23:26.080Z",
"id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
"last_edited_time": "2021-04-23T04:21:00.000Z",
"object": "database",
"properties": {
"Name": {
"id": "title",
"title": {},
"type": "title"
},
"Task Type": {
"id": "vd@l",
"multi_select": {
"options": []
},
"type": "multi_select"
}
},
"title": [
{
"annotations": {
"bold": false,
"code": false,
"color": "default",
"italic": false,
"strikethrough": false,
"underline": false
},
"href": null,
"plain_text": "Tasks",
"text": {
"content": "Tasks",
"link": null
},
"type": "text"
}
]
},
{
"archived": false,
"created_time": "2021-04-23T04:21:00.000Z",
"id": "4f555b50-3a9b-49cb-924c-3746f4ca5522",
"last_edited_time": "2021-04-23T04:21:00.000Z",
"object": "page",
"parent": {
"database_id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
"type": "database_id"
},
"properties": {
"Name": {
"id": "title",
"title": [
{
"annotations": {
"bold": false,
"code": false,
"color": "default",
"italic": false,
"strikethrough": false,
"underline": false
},
"href": null,
"plain_text": "Task 1",
"text": {
"content": "Task1 1",
"link": null
},
"type": "text"
}
],
"type": "title"
}
}
}
]
}

0 comments on commit ad0532d

Please sign in to comment.