#include "apiUrl.hpp" #include "hearthstoneApi.hpp" #include #include #include #include #include #include #include #include #include #include #include #include TEST_CASE("Retrieve API Token", "[generateApiToken]") { const std::string kCredentialsFile{"tests/resources/credentials.json"}; Token aToken(kCredentialsFile); const std::string kTokenContent{aToken.text()}; const std::string::size_type kTokenSize{kTokenContent.size()}; REQUIRE(kTokenSize >= 30); REQUIRE(kTokenSize <= 40); } TEST_CASE("Retrieve number of cards", "[countCards]") { const std::string kCredentialsFile{"tests/resources/credentials.json"}; Token aToken(kCredentialsFile); HearthstoneApi aHearthstoneApi(aToken); const std::string kContentType{"cards"}; const std::string kRegion{"eu"}; ApiUrl aApiUrl(kContentType, kRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize("1"); const json kResponse = aHearthstoneApi.makeApiRequest(aApiUrl()); REQUIRE(kResponse.size() > 0); REQUIRE(kResponse.contains("pageCount")); std::uint64_t kNumberOfCards{kResponse["pageCount"]}; REQUIRE(kNumberOfCards == 6033); } TEST_CASE("Retrieve cards", "[retrieveCardsOnce]") { const std::string kCredentialsFile{"tests/resources/credentials.json"}; Token aToken(kCredentialsFile); HearthstoneApi aHearthstoneApi(aToken); const std::string kContentType{"cards"}; const std::string kRegion{"eu"}; ApiUrl aApiUrl(kContentType, kRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize("16"); const json kApiResponse = aHearthstoneApi.makeApiRequest(aApiUrl()); REQUIRE(kApiResponse["cards"].size() == 16); } TEST_CASE("Retrieve cards 10 times without parallelization", "[retrieveCardsNoParallel]") { const std::string kCredentialsFile{"tests/resources/credentials.json"}; Token aToken(kCredentialsFile); HearthstoneApi aHearthstoneApi(aToken); const std::string kContentType{"cards"}; const std::string kRegion{"eu"}; ApiUrl aApiUrl(kContentType, kRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize("16"); aApiUrl.setPage("1"); json kApiResponse = aHearthstoneApi.makeApiRequest(aApiUrl()); // Retrieve number of pages const int kNumberOfRetrievals{10}; const int kNumberOfPages{kApiResponse["pageCount"]}; REQUIRE(kNumberOfPages >= kNumberOfRetrievals); for (int i{1}; i < kNumberOfRetrievals; ++i) { REQUIRE(kApiResponse["cards"].size() == 16); aApiUrl.setPage(std::to_string(i + 1)); kApiResponse = aHearthstoneApi.makeApiRequest(aApiUrl()); } REQUIRE(kApiResponse["cards"].size() >= 1); REQUIRE(kApiResponse["cards"].size() <= 16); } TEST_CASE("Retrieve cards 30 times with parallelization", "[retrieveCardsParallel]") { const std::string kCredentialsFile{"tests/resources/credentials.json"}; Token aToken(kCredentialsFile); HearthstoneApi aHearthstoneApi(aToken); const std::string kContentType{"cards"}; const std::string kRegion{"eu"}; ApiUrl aApiUrl(kContentType, kRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize("16"); aApiUrl.setPage("1"); std::vector aRequests; json kFirstPageJson = aHearthstoneApi.makeApiRequest(aApiUrl()); int kNumberOfPages{kFirstPageJson["pageCount"]}; const int kNumberOfRetrievals{30}; REQUIRE(kNumberOfPages >= kNumberOfRetrievals); for (int i{1}; i < kNumberOfRetrievals; ++i) { aApiUrl.setPage(std::to_string(i + 1)); aRequests.push_back(aApiUrl()); } std::vector kApiResponses = aHearthstoneApi.makeApiRequest(aRequests); kApiResponses.emplace(kApiResponses.begin(), kFirstPageJson); REQUIRE(kApiResponses.size() == kNumberOfRetrievals); for (const json &kApiResponse : kApiResponses) { REQUIRE(kApiResponse["cards"].size() == 16); } }