-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from Kentico/feature/DEL-2330_Include_total_c…
…ount_in_response Support for IncludeTotalCount parameter
- Loading branch information
Showing
13 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Kentico.Kontent.Delivery.Tests/QueryParameters/IncludeTotalCountTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System; | ||
using FakeItEasy; | ||
using FluentAssertions; | ||
using Kentico.Kontent.Delivery.Tests.Factories; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using RichardSzalay.MockHttp; | ||
using Xunit; | ||
|
||
namespace Kentico.Kontent.Delivery.Tests.QueryParameters | ||
{ | ||
public class IncludeTotalCountTests | ||
{ | ||
private static readonly Guid ProjectId = Guid.NewGuid(); | ||
private static readonly string BaseUrl = $"https://deliver.kontent.ai/{ProjectId}"; | ||
private static readonly MockHttpMessageHandler MockHttp = new MockHttpMessageHandler(); | ||
private static readonly DeliveryOptions Options = new DeliveryOptions | ||
{ | ||
ProjectId = ProjectId.ToString(), | ||
EnableRetryPolicy = false, | ||
IncludeTotalCount = true | ||
}; | ||
|
||
[Fact] | ||
public void PaginationResponse_WithoutTotalCount_DeserializedCorrectly() | ||
{ | ||
var responsePagination = JObject.FromObject(new | ||
{ | ||
skip = 20, | ||
limit = 10, | ||
count = 8, | ||
next_page = "nextPage" | ||
}); | ||
var expected = new Pagination(20, 10, 8, null, "nextPage"); | ||
|
||
var result = responsePagination.ToObject<Pagination>(); | ||
|
||
result.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Fact] | ||
public void PaginationResponse_WithTotalCount_DeserializedCorrectly() | ||
{ | ||
var responsePagination = JObject.FromObject(new | ||
{ | ||
skip = 20, | ||
limit = 10, | ||
count = 8, | ||
total_count = 28, | ||
next_page = "nextPage" | ||
}); | ||
var expected = new Pagination(20, 10, 8, 28, "nextPage"); | ||
|
||
var result = responsePagination.ToObject<Pagination>(); | ||
|
||
result.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Fact] | ||
public async void GetItemsJson_DeliveryOptionsWithIncludeTotalCount_IncludeTotalCountParameterAdded() | ||
{ | ||
var responseJson = JsonConvert.SerializeObject(CreateItemsResponse()); | ||
MockHttp | ||
.Expect($"{BaseUrl}/items") | ||
.WithExactQueryString("includeTotalCount") | ||
.Respond("application/json", responseJson); | ||
var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(Options, MockHttp); | ||
|
||
await client.GetItemsJsonAsync(); | ||
|
||
MockHttp.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
[Fact] | ||
public async void GetItems_DeliveryOptionsWithIncludeTotalCount_IncludeTotalCountParameterAdded() | ||
{ | ||
var responseJson = JsonConvert.SerializeObject(CreateItemsResponse()); | ||
MockHttp | ||
.Expect($"{BaseUrl}/items") | ||
.WithExactQueryString("includeTotalCount") | ||
.Respond("application/json", responseJson); | ||
var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(Options, MockHttp); | ||
|
||
await client.GetItemsAsync(); | ||
|
||
MockHttp.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
[Fact] | ||
public async void GetItemsTyped_DeliveryOptionsWithIncludeTotalCount_IncludeTotalCountParameterAdded() | ||
{ | ||
var responseJson = JsonConvert.SerializeObject(CreateItemsResponse()); | ||
MockHttp | ||
.Expect($"{BaseUrl}/items") | ||
.WithExactQueryString("system.type=cafe&includeTotalCount") | ||
.Respond("application/json", responseJson); | ||
var client = DeliveryClientFactory.GetMockedDeliveryClientWithOptions(Options, MockHttp); | ||
A.CallTo(() => client.TypeProvider.GetCodename(typeof(Cafe))).Returns("cafe"); | ||
|
||
await client.GetItemsAsync<Cafe>(); | ||
|
||
MockHttp.VerifyNoOutstandingExpectation(); | ||
} | ||
|
||
private static object CreateItemsResponse() => new | ||
{ | ||
items = new object[0], | ||
modular_content = new { }, | ||
pagination = new | ||
{ | ||
skip = 0, | ||
limit = 0, | ||
count = 1, | ||
total_count = 1, | ||
next_page = "" | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Kentico.Kontent.Delivery/QueryParameters/Parameters/IncludeTotalCountParameter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Kentico.Kontent.Delivery | ||
{ | ||
/// <summary> | ||
/// Specifies whether to include total count in the paging section. | ||
/// </summary> | ||
public sealed class IncludeTotalCountParameter : IQueryParameter | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="IncludeTotalCountParameter"/> class. | ||
/// </summary> | ||
public IncludeTotalCountParameter() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Returns the query string representation of the query parameter. | ||
/// </summary> | ||
public string GetQueryStringParameter() | ||
{ | ||
return "includeTotalCount"; | ||
} | ||
} | ||
} |
Oops, something went wrong.