Skip to content

Commit

Permalink
Paginate GetSections call (#478)
Browse files Browse the repository at this point in the history
* Changed signature and code of GetSections to use GenericPagedGet

* Converted paged signature to overload, added same code and test for GetCategories

* Added Async methods for GetSections and GetCategories + unit tests, changed Assert syntax
  • Loading branch information
ClaudineL authored May 19, 2020
1 parent ce4a049 commit 59b3328
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface ICategories : ICore
{
#if SYNC
GroupCategoryResponse GetCategories();
GroupCategoryResponse GetCategories(int perPage, int page);
IndividualCategoryResponse GetCategoryById(long id);
IndividualCategoryResponse CreateCategory(Category category);
IndividualCategoryResponse UpdateCategory(Category category);
Expand All @@ -19,6 +20,7 @@ public interface ICategories : ICore

#if ASYNC
Task<GroupCategoryResponse> GetCategoriesAsync();
Task<GroupCategoryResponse> GetCategoriesAsync(int perPage, int page);
Task<IndividualCategoryResponse> GetCategoryByIdAsync(long id);
Task<IndividualCategoryResponse> CreateCategoryAsync(Category category);
Task<IndividualCategoryResponse> UpdateCategoryAsync(Category category);
Expand Down Expand Up @@ -46,7 +48,12 @@ public GroupCategoryResponse GetCategories()
return GenericGet<GroupCategoryResponse>($"{GeneralCategoriesPath}.json");
}

public IndividualCategoryResponse GetCategoryById(long id)
public GroupCategoryResponse GetCategories(int perPage, int page)
{
return GenericPagedGet<GroupCategoryResponse>($"{GeneralCategoriesPath}.json", perPage, page);
}

public IndividualCategoryResponse GetCategoryById(long id)
{
return GenericGet<IndividualCategoryResponse>($"{GeneralCategoriesPath}/{id}.json");
}
Expand Down Expand Up @@ -81,6 +88,11 @@ public async Task<GroupCategoryResponse> GetCategoriesAsync()
return await GenericGetAsync<GroupCategoryResponse>($"{GeneralCategoriesPath}.json");
}

public async Task<GroupCategoryResponse> GetCategoriesAsync(int perPage, int page)
{
return await GenericPagedGetAsync<GroupCategoryResponse>($"{GeneralCategoriesPath}.json", perPage, page);
}

public async Task<IndividualCategoryResponse> GetCategoryByIdAsync(long id)
{
return await GenericGetAsync<IndividualCategoryResponse>($"{GeneralCategoriesPath}/{id}.json");
Expand Down
13 changes: 13 additions & 0 deletions src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface ISections : ICore
{
#if SYNC
GroupSectionResponse GetSections();
GroupSectionResponse GetSections(int perPage, int page);
GroupSectionResponse GetSectionsByCategoryId(long categoryId);
IndividualSectionResponse GetSectionById(long id);
IndividualSectionResponse CreateSection(Section section);
Expand All @@ -24,6 +25,7 @@ public interface ISections : ICore

#if ASYNC
Task<GroupSectionResponse> GetSectionsAsync();
Task<GroupSectionResponse> GetSectionsAsync(int perPage, int page);
Task<GroupSectionResponse> GetSectionsByCategoryIdAsync(long categoryId);
Task<IndividualSectionResponse> GetSectionByIdAsync(long id);
Task<IndividualSectionResponse> CreateSectionAsync(Section section);
Expand Down Expand Up @@ -53,6 +55,12 @@ public GroupSectionResponse GetSections()
{
return GenericGet<GroupSectionResponse>($"{_generalSectionsPath}.json?include=access_policies");
}

public GroupSectionResponse GetSections(int perPage, int page)
{
return GenericPagedGet<GroupSectionResponse>($"{_generalSectionsPath}.json?include=access_policies", perPage, page);
}

public GroupSectionResponse GetSectionsByCategoryId(long categoryId)
{
return GenericGet<GroupSectionResponse>(GetSectionPathWithCategory(categoryId));
Expand Down Expand Up @@ -105,6 +113,11 @@ public async Task<GroupSectionResponse> GetSectionsAsync()
return await GenericGetAsync<GroupSectionResponse>($"{_generalSectionsPath}.json?include=access_policies");
}

public async Task<GroupSectionResponse> GetSectionsAsync(int perPage, int page)
{
return await GenericPagedGetAsync<GroupSectionResponse>($"{_generalSectionsPath}.json?include=access_policies", perPage, page);
}

public async Task<GroupSectionResponse> GetSectionsByCategoryIdAsync(long categoryId)
{
return await GenericGetAsync<GroupSectionResponse>(GetSectionPathWithCategory(categoryId));
Expand Down
95 changes: 88 additions & 7 deletions test/ZendeskApi_v2.Test/HelpCenter/CategoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using ZendeskApi_v2;
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.HelpCenter.Categories;

namespace Tests.HelpCenter
Expand All @@ -15,24 +17,24 @@ public class CategoryTests
[OneTimeSetUp]
public async Task Setup()
{
var categotriesResponse = await api.HelpCenter.Categories.GetCategoriesAsync();
var categoriesResponse = await api.HelpCenter.Categories.GetCategoriesAsync();

do
{
foreach (var category in categotriesResponse.Categories)
foreach (var category in categoriesResponse.Categories)
{
if (category.Name == "My Test category")
{
await api.HelpCenter.Categories.DeleteCategoryAsync(category.Id.Value);
}
}

if (!string.IsNullOrWhiteSpace(categotriesResponse.NextPage))
if (!string.IsNullOrWhiteSpace(categoriesResponse.NextPage))
{
categotriesResponse = await api.HelpCenter.Articles.GetByPageUrlAsync<GroupCategoryResponse>(categotriesResponse.NextPage, 100);
categoriesResponse = await api.HelpCenter.Articles.GetByPageUrlAsync<GroupCategoryResponse>(categoriesResponse.NextPage, 100);
}

} while (!string.IsNullOrWhiteSpace(categotriesResponse.NextPage));
} while (!string.IsNullOrWhiteSpace(categoriesResponse.NextPage));
}


Expand All @@ -46,6 +48,85 @@ public void CanGetCategories()
Assert.AreEqual(res1.Category.Id, res.Categories[0].Id.Value);
}

[Test]
public void CanGetCategoriesPaged()
{
var category1 = api.HelpCenter.Categories.CreateCategory(new Category {
Name = "My Test category 1",
Position = 0,
Description = "First category"
});

var category2 = api.HelpCenter.Categories.CreateCategory(new Category {
Name = "My Test category 2",
Position = 0,
Description = "Second category"
});

const int count = 2;
var categories = api.HelpCenter.Categories.GetCategories(count, 1);

Assert.That(count, Is.EqualTo(categories.Categories.Count)); // 2
Assert.That(categories.Count, Is.Not.EqualTo(categories.Categories.Count)); // 2 != total count of categories (assumption)

const int page = 2;
var secondPage = api.HelpCenter.Categories.GetCategories(count, page);

Assert.That(count, Is.EqualTo(secondPage.Categories.Count));

var nextPage = secondPage.NextPage.GetQueryStringDict()
.Where(x => x.Key == "page")
.Select(x => x.Value)
.FirstOrDefault();

Assert.That(nextPage, Is.Not.Null);
Assert.That(nextPage, Is.EqualTo((page + 1).ToString()));
Assert.That(api.HelpCenter.Categories.DeleteCategory(category1.Category.Id.Value), Is.True);
Assert.That(api.HelpCenter.Categories.DeleteCategory(category2.Category.Id.Value), Is.True);
}

[Test]
public void CanGetCategoriesPagedAsync()
{
var category1 = api.HelpCenter.Categories.CreateCategory(new Category {
Name = "My Test category 1",
Position = 0,
Description = "First category"
});

var category2 = api.HelpCenter.Categories.CreateCategory(new Category {
Name = "My Test category 2",
Position = 0,
Description = "Second category"
});

const int count = 2;
var categoriesAsync = api.HelpCenter.Categories.GetCategoriesAsync(count, 1).Result;

Assert.That(categoriesAsync.Count, Is.GreaterThan(0));

var categoryById1 = api.HelpCenter.Categories.GetCategoryById(categoriesAsync.Categories[0].Id.Value);

Assert.That(categoryById1.Category.Id, Is.EqualTo(categoriesAsync.Categories[0].Id.Value));

const int page = 2;
var secondPage = api.HelpCenter.Categories.GetCategoriesAsync(count, page).Result;
var categoryById2 = api.HelpCenter.Categories.GetCategoryById(secondPage.Categories[0].Id.Value);

Assert.That(count, Is.EqualTo(secondPage.Categories.Count));
Assert.That(categoryById2.Category.Id, Is.EqualTo(secondPage.Categories[0].Id.Value));

var nextPage = secondPage.NextPage.GetQueryStringDict()
.Where(x => x.Key == "page")
.Select(x => x.Value)
.FirstOrDefault();

Assert.That(nextPage, Is.Not.Null);
Assert.That(nextPage, Is.EqualTo((page + 1).ToString()));
Assert.That(api.HelpCenter.Categories.DeleteCategory(category1.Category.Id.Value), Is.True);
Assert.That(api.HelpCenter.Categories.DeleteCategory(category2.Category.Id.Value), Is.True);
}

[Test]
public void CanCreateUpdateAndDeleteCategories()
{
Expand All @@ -65,4 +146,4 @@ public void CanCreateUpdateAndDeleteCategories()
Assert.True(api.HelpCenter.Categories.DeleteCategory(res.Category.Id.Value));
}
}
}
}
86 changes: 86 additions & 0 deletions test/ZendeskApi_v2.Test/HelpCenter/SectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using NUnit.Framework;
using ZendeskApi_v2;
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.Sections;

namespace Tests.HelpCenter
Expand Down Expand Up @@ -45,6 +46,91 @@ public void CanGetSections()
Assert.AreEqual(res1.Section.Id, res.Sections[0].Id.Value);
}

[Test]
public void CanGetSectionsPaged()
{
//https://csharpapi.zendesk.com/hc/en-us/categories/200382245-Category-1
long category_id = 200382245;

var section1 = api.HelpCenter.Sections.CreateSection(new Section {
Name = "My Test section 1",
Position = 0,
CategoryId = category_id
});

var section2 = api.HelpCenter.Sections.CreateSection(new Section {
Name = "My Test section 2",
Position = 0,
CategoryId = category_id
});

const int count = 2;
var sections = api.HelpCenter.Sections.GetSections(count, 1);

Assert.That(count, Is.EqualTo(sections.Sections.Count)); // 2
Assert.That(sections.Count, Is.Not.EqualTo(sections.Sections.Count)); // 2 != total count of sections (assumption)

const int page = 2;
var secondPage = api.HelpCenter.Sections.GetSections(count, page);

Assert.That(count, Is.EqualTo(secondPage.Sections.Count));

var nextPage = secondPage.NextPage.GetQueryStringDict()
.Where(x => x.Key == "page")
.Select(x => x.Value)
.FirstOrDefault();

Assert.That(nextPage, Is.Not.Null);
Assert.That(nextPage, Is.EqualTo((page + 1).ToString()));
Assert.That(api.HelpCenter.Sections.DeleteSection(section1.Section.Id.Value), Is.True);
Assert.That(api.HelpCenter.Sections.DeleteSection(section2.Section.Id.Value), Is.True);
}

[Test]
public void CanGetSectionsPagedAsync()
{
//https://csharpapi.zendesk.com/hc/en-us/categories/200382245-Category-1
long category_id = 200382245;

var section1 = api.HelpCenter.Sections.CreateSection(new Section {
Name = "My Test section 1",
Position = 0,
CategoryId = category_id
});

var section2 = api.HelpCenter.Sections.CreateSection(new Section {
Name = "My Test section 2",
Position = 0,
CategoryId = category_id
});

const int count = 2;
var sectionsAsync = api.HelpCenter.Sections.GetSectionsAsync(count, 1).Result;

Assert.That(sectionsAsync.Count, Is.GreaterThan(0));

var sectionById1 = api.HelpCenter.Sections.GetSectionById(sectionsAsync.Sections[0].Id.Value);

Assert.That(sectionById1.Section.Id, Is.EqualTo(sectionsAsync.Sections[0].Id.Value));

const int page = 2;
var secondPage = api.HelpCenter.Sections.GetSectionsAsync(count, page).Result;
var sectionById2 = api.HelpCenter.Sections.GetSectionById(secondPage.Sections[0].Id.Value);

Assert.That(count, Is.EqualTo(secondPage.Sections.Count));
Assert.That(sectionById2.Section.Id, Is.EqualTo(secondPage.Sections[0].Id.Value));

var nextPage = secondPage.NextPage.GetQueryStringDict()
.Where(x => x.Key == "page")
.Select(x => x.Value)
.FirstOrDefault();

Assert.That(nextPage, Is.Not.Null);
Assert.That(nextPage, Is.EqualTo((page + 1).ToString()));
Assert.That(api.HelpCenter.Sections.DeleteSection(section1.Section.Id.Value), Is.True);
Assert.That(api.HelpCenter.Sections.DeleteSection(section2.Section.Id.Value), Is.True);
}

[Test]
public void CanCreateUpdateAndDeleteSections()
{
Expand Down

0 comments on commit 59b3328

Please sign in to comment.