-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Paginator template and unit test #3681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+341
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/aws-cpp-sdk-core/include/aws/core/utils/pagination/Paginator.h
This file contains hidden or 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,101 @@ | ||
| /** | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <iterator> | ||
| #include <cstddef> | ||
| #include <memory> | ||
|
|
||
| namespace Aws | ||
| { | ||
| namespace Utils | ||
| { | ||
| namespace Pagination | ||
| { | ||
|
|
||
| template <class ServiceClient, class OperationRequest, class OperationTraits> | ||
| class PagePaginator | ||
| { | ||
| private: | ||
| struct EndSentinel {}; | ||
| public: | ||
| using OutcomeType = typename OperationTraits::OutcomeType; | ||
| using ResultType = typename OperationTraits::ResultType; | ||
|
|
||
| class PageIterator | ||
| { | ||
| public: | ||
| using iterator_category = std::input_iterator_tag; | ||
| using value_type = OutcomeType; | ||
| using difference_type = std::ptrdiff_t; | ||
| using pointer = const OutcomeType*; | ||
| using reference = const OutcomeType&; | ||
|
|
||
| PageIterator(std::shared_ptr<ServiceClient> client, const OperationRequest& firstReq) | ||
| : m_client(client), | ||
| m_request(firstReq), | ||
| m_currentOutcome{OperationTraits::Invoke(*m_client, m_request)} | ||
| {} | ||
|
|
||
| const OutcomeType& operator*() const { return m_currentOutcome; } | ||
|
|
||
| PageIterator& operator++() | ||
| { | ||
| if (m_atEnd) return *this; | ||
|
|
||
| if (!m_currentOutcome.IsSuccess()) | ||
| { | ||
| m_atEnd = true; | ||
| return *this; | ||
sbiscigl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (!OperationTraits::HasMoreResults(m_currentOutcome.GetResult())) | ||
| { | ||
| m_atEnd = true; | ||
| return *this; | ||
| } | ||
|
|
||
| OperationTraits::SetNextRequest(m_currentOutcome.GetResult(), m_request); | ||
| FetchPage(); | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| friend bool operator==(const PageIterator& lhs, const EndSentinel&) { | ||
| return lhs.m_atEnd; | ||
| } | ||
|
|
||
| friend bool operator!=(const PageIterator& lhs, const EndSentinel& rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| private: | ||
| void FetchPage() | ||
| { | ||
| m_currentOutcome = OperationTraits::Invoke(*m_client, m_request); | ||
| } | ||
|
|
||
| std::shared_ptr<ServiceClient> m_client; | ||
| OperationRequest m_request{}; | ||
| OutcomeType m_currentOutcome{}; | ||
| bool m_atEnd{false}; | ||
| }; | ||
|
|
||
| PagePaginator(std::shared_ptr<ServiceClient> client, const OperationRequest& firstReq) | ||
| : m_client(client), | ||
| m_firstRequest(firstReq) {} | ||
|
|
||
| PageIterator begin() const { return PageIterator(m_client, m_firstRequest); } | ||
| EndSentinel end() const { return EndSentinel{}; } | ||
|
|
||
| private: | ||
| std::shared_ptr<ServiceClient> m_client; | ||
| OperationRequest m_firstRequest{}; | ||
| }; | ||
|
|
||
| } // namespace Pagination | ||
| } // namespace Utils | ||
| } // namespace Aws | ||
This file contains hidden or 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
237 changes: 237 additions & 0 deletions
237
tests/aws-cpp-sdk-core-tests/utils/pagination/PaginatorTest.cpp
This file contains hidden or 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,237 @@ | ||
| /** | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0. | ||
| */ | ||
|
|
||
| #include <aws/testing/AwsCppSdkGTestSuite.h> | ||
| #include <aws/core/utils/pagination/Paginator.h> | ||
| #include <aws/core/utils/StringUtils.h> | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| using namespace Aws::Utils::Pagination; | ||
| using namespace Aws::Utils; | ||
|
|
||
| struct TestResult { | ||
| Aws::Vector<Aws::String> items; | ||
| Aws::String outputToken; | ||
| bool moreResults; | ||
|
|
||
| const Aws::Vector<Aws::String>& GetItems() const { return items; } | ||
| const Aws::String& GetOutputToken() const { return outputToken; } | ||
| bool GetMoreResults() const { return moreResults; } | ||
| }; | ||
|
|
||
| struct TestOutcome { | ||
| bool success; | ||
| Aws::String error; | ||
| TestResult result; | ||
|
|
||
| bool IsSuccess() const { return success; } | ||
| const Aws::String& GetError() const { return error; } | ||
| const TestResult& GetResult() const { return result; } | ||
| }; | ||
|
|
||
| struct TestRequest { | ||
| Aws::String inputToken; | ||
| int limitKey = 3; | ||
|
|
||
| void SetInputToken(const Aws::String& t) { inputToken = t; } | ||
| const Aws::String& GetInputToken() const { return inputToken; } | ||
| }; | ||
|
|
||
| class PaginatorTestClient { | ||
| private: | ||
| Aws::Vector<Aws::String> allData; | ||
| bool shouldFail = false; | ||
| int failOnPage = -1; | ||
| int currentPage = 0; | ||
|
|
||
| public: | ||
| PaginatorTestClient() : allData({"apple", "banana", "cherry", "dragon-fruit", "elderberry", "fig", "grape"}) {} | ||
|
|
||
| void SetData(const Aws::Vector<Aws::String>& data) { allData = data; } | ||
| void SetShouldFail(bool fail) { shouldFail = fail; } | ||
| void SetFailOnPage(int page) { failOnPage = page; } | ||
| void Reset() { | ||
| allData = {"apple", "banana", "cherry", "dragon-fruit", "elderberry", "fig", "grape"}; | ||
| shouldFail = false; | ||
| failOnPage = -1; | ||
| currentPage = 0; | ||
| } | ||
|
|
||
| TestOutcome ListItems(const TestRequest& request) { | ||
| if (shouldFail) { | ||
| return {false, "Request failed", {}}; | ||
| } | ||
|
|
||
| if (failOnPage >= 0 && currentPage == failOnPage) { | ||
| currentPage++; | ||
| return {false, "Page " + StringUtils::to_string(failOnPage) + " failed", {}}; | ||
| } | ||
| currentPage++; | ||
|
|
||
| int startIdx = 0; | ||
| if (!request.GetInputToken().empty()) { | ||
| startIdx = StringUtils::ConvertToInt32(request.GetInputToken().c_str()); | ||
| } | ||
|
|
||
| TestResult result; | ||
| for (size_t i = 0; i < static_cast<size_t>(request.limitKey) && startIdx + i < allData.size(); ++i) { | ||
| result.items.push_back(allData[startIdx + i]); | ||
| } | ||
|
|
||
| int nextIdx = startIdx + request.limitKey; | ||
| if (static_cast<size_t>(nextIdx) < allData.size()) { | ||
| result.outputToken = StringUtils::to_string(nextIdx); | ||
| result.moreResults = true; | ||
| } else { | ||
| result.moreResults = false; | ||
| } | ||
|
|
||
| return {true, "", result}; | ||
| } | ||
| }; | ||
|
|
||
| struct ListItemsTraits { | ||
| using OutcomeType = TestOutcome; | ||
| using ResultType = TestResult; | ||
|
|
||
| static OutcomeType Invoke(PaginatorTestClient& client, const TestRequest& request) { | ||
| return client.ListItems(request); | ||
| } | ||
|
|
||
| static bool HasMoreResults(const TestResult& result) { | ||
| return result.GetMoreResults(); | ||
| } | ||
|
|
||
| static void SetNextRequest(const TestResult& result, TestRequest& request) { | ||
| request.SetInputToken(result.GetOutputToken()); | ||
| } | ||
| }; | ||
|
|
||
| class PaginatorTest : public Aws::Testing::AwsCppSdkGTestSuite | ||
| { | ||
| protected: | ||
| void SetUp() override | ||
| { | ||
| client.Reset(); | ||
| } | ||
|
|
||
| void TearDown() override | ||
| { | ||
| client.Reset(); | ||
| } | ||
|
|
||
| PaginatorTestClient client; | ||
| TestRequest request; | ||
| }; | ||
|
|
||
| TEST_F(PaginatorTest, TestIteratesThroughAllPages) | ||
| { | ||
| auto clientPtr = std::make_shared<PaginatorTestClient>(client); | ||
| PagePaginator<PaginatorTestClient, TestRequest, ListItemsTraits> paginator(clientPtr, request); | ||
|
|
||
| Aws::Vector<Aws::String> allItems; | ||
| int pageCount = 0; | ||
|
|
||
| auto it = paginator.begin(); | ||
| auto end = paginator.end(); | ||
| while (it != end) | ||
| { | ||
| const auto& outcome = *it; | ||
| ASSERT_TRUE(outcome.IsSuccess()); | ||
| pageCount++; | ||
|
|
||
| const auto& page = outcome.GetResult(); | ||
| for (const auto& item : page.GetItems()) | ||
| { | ||
| allItems.push_back(item); | ||
| } | ||
| ++it; | ||
| } | ||
|
|
||
| EXPECT_EQ(pageCount, 3); // 7 items / 3 per page = 3 pages | ||
| EXPECT_EQ(allItems.size(), 7u); | ||
| EXPECT_STREQ(allItems[0].c_str(), "apple"); | ||
| EXPECT_STREQ(allItems[6].c_str(), "grape"); | ||
| } | ||
|
|
||
| TEST_F(PaginatorTest, TestHandlesErrorGracefully) | ||
| { | ||
| client.SetShouldFail(true); | ||
| auto clientPtr = std::make_shared<PaginatorTestClient>(client); | ||
| PagePaginator<PaginatorTestClient, TestRequest, ListItemsTraits> paginator(clientPtr, request); | ||
|
|
||
| auto it = paginator.begin(); | ||
| ASSERT_TRUE(it != paginator.end()); | ||
|
|
||
| const auto& outcome = *it; | ||
| EXPECT_FALSE(outcome.IsSuccess()); | ||
| EXPECT_STREQ(outcome.GetError().c_str(), "Request failed"); | ||
|
|
||
| ++it; | ||
| EXPECT_TRUE(it == paginator.end()); | ||
| } | ||
|
|
||
| TEST_F(PaginatorTest, TestEmptyResultSet) | ||
| { | ||
| client.SetData({}); | ||
| auto clientPtr = std::make_shared<PaginatorTestClient>(client); | ||
| PagePaginator<PaginatorTestClient, TestRequest, ListItemsTraits> paginator(clientPtr, request); | ||
|
|
||
| auto it = paginator.begin(); | ||
| ASSERT_TRUE(it != paginator.end()); | ||
|
|
||
| const auto& outcome = *it; | ||
| EXPECT_TRUE(outcome.IsSuccess()); | ||
| EXPECT_TRUE(outcome.GetResult().GetItems().empty()); | ||
| EXPECT_FALSE(outcome.GetResult().GetMoreResults()); | ||
|
|
||
| ++it; | ||
| EXPECT_TRUE(it == paginator.end()); | ||
| } | ||
|
|
||
| TEST_F(PaginatorTest, TestBeginEndIteratorComparison) | ||
| { | ||
| auto clientPtr = std::make_shared<PaginatorTestClient>(client); | ||
| PagePaginator<PaginatorTestClient, TestRequest, ListItemsTraits> paginator(clientPtr, request); | ||
|
|
||
| auto begin = paginator.begin(); | ||
| auto end = paginator.end(); | ||
|
|
||
| EXPECT_FALSE(begin == end); | ||
|
|
||
| while (begin != end) | ||
| { | ||
| ++begin; | ||
| } | ||
|
|
||
| EXPECT_TRUE(begin == end); | ||
| } | ||
|
|
||
| TEST_F(PaginatorTest, TestHandlesErrorOnSecondPage) | ||
| { | ||
| client.SetFailOnPage(1); // Fail on second page (0-indexed) | ||
| auto clientPtr = std::make_shared<PaginatorTestClient>(client); | ||
| PagePaginator<PaginatorTestClient, TestRequest, ListItemsTraits> paginator(clientPtr, request); | ||
|
|
||
| auto it = paginator.begin(); | ||
|
|
||
| // First page should succeed | ||
| ASSERT_TRUE(it != paginator.end()); | ||
| EXPECT_TRUE((*it).IsSuccess()); | ||
| EXPECT_EQ((*it).GetResult().GetItems().size(), 3u); | ||
|
|
||
| // Move to second page | ||
| ++it; | ||
|
|
||
| // Second page should have the error | ||
| ASSERT_TRUE(it != paginator.end()); | ||
| EXPECT_FALSE((*it).IsSuccess()); | ||
| EXPECT_TRUE((*it).GetError().find("Page 1 failed") != Aws::String::npos); | ||
|
|
||
| // After error, iteration should end | ||
| ++it; | ||
| EXPECT_TRUE(it == paginator.end()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.