diff options
| author | Aotrix <[email protected]> | 2026-07-30 04:31:49 +0200 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-07-30 04:31:49 +0200 |
| commit | 99421df7033148e6abb4e9f7a4d5d43f5cbd2ab4 (patch) | |
| tree | 8d03c98500709b6f1ef03b787fbde47ec2e0efaf /battleNetApi/tests/src | |
| parent | 1fbfd13f5d7220122b2a127627cc9105013b4893 (diff) | |
Fixed tests for HearthstoneApi
Diffstat (limited to 'battleNetApi/tests/src')
| -rw-r--r-- | battleNetApi/tests/src/hearthstoneApiTests.cpp | 156 | ||||
| -rw-r--r-- | battleNetApi/tests/src/tests.cpp | 144 | ||||
| -rw-r--r-- | battleNetApi/tests/src/tokenTests.cpp | 2 |
3 files changed, 157 insertions, 145 deletions
diff --git a/battleNetApi/tests/src/hearthstoneApiTests.cpp b/battleNetApi/tests/src/hearthstoneApiTests.cpp new file mode 100644 index 0000000..2c39d0c --- /dev/null +++ b/battleNetApi/tests/src/hearthstoneApiTests.cpp @@ -0,0 +1,156 @@ +#include "apiUrl.hpp" +#include "hearthstoneApi.hpp" +#include "hearthstoneTokenBuilder.hpp" +#include <catch2/benchmark/catch_benchmark.hpp> +#include <catch2/benchmark/catch_chronometer.hpp> +#include <catch2/benchmark/detail/catch_analyse.hpp> +#include <catch2/benchmark/detail/catch_benchmark_function.hpp> +#include <catch2/benchmark/detail/catch_estimate_clock.hpp> +#include <catch2/catch_approx.hpp> +#include <catch2/catch_config.hpp> +#include <catch2/catch_test_macros.hpp> +#include <cstdint> +#include <fstream> +#include <nlohmann/json.hpp> +#include <string> +#include <tokenBuilder.hpp> + +TEST_CASE("Retrieve number of cards", "[countCards]") { + const std::string aContentType{"cards"}; + const std::string aRegion{"eu"}; + bnet::ApiUrl aApiUrl(aContentType, aRegion); + aApiUrl.setLocale("fr_FR"); + aApiUrl.setGameMode(bnet::GameMode::constructed); + aApiUrl.setPageSize(1); + bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); + const nlohmann::json aResponse = bnet::HearthstoneApi::makeApiRequest( + aApiUrl, aHearthstoneTokenBuilder.allowBattleNetApiCall() + .allowUpdateSourceFile() + .buildToken() + .getToken() + ->text() + .value()); + REQUIRE(aResponse.size() > 0); + REQUIRE(aResponse.contains("pageCount")); + std::uint64_t aNumberOfCards{aResponse["pageCount"]}; + std::uint64_t aExpectedNumberOfCards{6000}; + REQUIRE(aNumberOfCards >= aExpectedNumberOfCards); +} + +TEST_CASE("Retrieve page once", "[retrievePageOnce]") { + const std::string aContentType{"cards"}; + const std::string aRegion{"eu"}; + bnet::ApiUrl aApiUrl(aContentType, aRegion); + aApiUrl.setLocale("fr_FR"); + aApiUrl.setGameMode(bnet::GameMode::constructed); + aApiUrl.setPageSize(16); + bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); + const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() + .allowUpdateSourceFile() + .buildToken() + .getToken() + ->text() + .value()}; + const nlohmann::json aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); + REQUIRE(aApiResponse["cards"].size() == 16); +} + +TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoParallel]") { + const std::string aContentType{"cards"}; + const std::string aRegion{"eu"}; + bnet::ApiUrl aApiUrl(aContentType, aRegion); + aApiUrl.setLocale("fr_FR"); + aApiUrl.setGameMode(bnet::GameMode::constructed); + aApiUrl.setPageSize(16); + aApiUrl.setPage("1"); + bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); + const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() + .allowUpdateSourceFile() + .buildToken() + .getToken() + ->text() + .value()}; + bnet::json aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); + + // Retrieve number of pages + const int aNumberOfRetrievals{10}; + const int aNumberOfPages{aApiResponse["pageCount"]}; + REQUIRE(aNumberOfPages >= aNumberOfRetrievals); + for (int i{1}; i < aNumberOfRetrievals; ++i) { + REQUIRE(aApiResponse["cards"].size() == 16); + aApiUrl.setPage(std::to_string(i + 1)); + aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); + } + REQUIRE(aApiResponse["cards"].size() >= 1); + REQUIRE(aApiResponse["cards"].size() <= 16); +} + +TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParallel]") { + const std::string aContentType{"cards"}; + const std::string aRegion{"eu"}; + bnet::ApiUrl aApiUrl(aContentType, aRegion); + aApiUrl.setLocale("fr_FR"); + aApiUrl.setGameMode(bnet::GameMode::constructed); + aApiUrl.setPageSize(16); + aApiUrl.setPage("1"); + std::vector<std::string> aRequests; + bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); + const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() + .allowUpdateSourceFile() + .buildToken() + .getToken() + ->text() + .value()}; + bnet::json aFirstPageJson = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); + + const int aNumberOfPages{aFirstPageJson["pageCount"]}; + const int aNumberOfRetrievals{10}; + REQUIRE(aNumberOfPages >= aNumberOfRetrievals); + for (int i{1}; i < aNumberOfRetrievals; ++i) { + aApiUrl.setPage(std::to_string(i + 1)); + aRequests.emplace_back(aApiUrl.text()); + } + std::vector<nlohmann::json> aApiResponses = + bnet::HearthstoneApi::makeApiRequest(aRequests, aTokenText); + aApiResponses.emplace(aApiResponses.begin(), aFirstPageJson); + REQUIRE(aApiResponses.size() == aNumberOfRetrievals); + for (const nlohmann::json &aApiResponse : aApiResponses) { + REQUIRE(aApiResponse["cards"].size() == 16); + } +} + +TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") { + std::ifstream aFirstPageFile{"resources/page_merge_00.json"}; + nlohmann::json aFirstPageJson = nlohmann::json::parse(aFirstPageFile); + std::ifstream aSecondPageFile{"resources/page_merge_01.json"}; + const nlohmann::json aSecondPageJson = nlohmann::json::parse(aSecondPageFile); + + nlohmann::json::reference aMergedCards = aFirstPageJson["cards"]; + for (auto &card : aSecondPageJson["cards"]) { + aMergedCards.emplace_back(std::move(card)); + } + REQUIRE(aMergedCards.size() == 32); + std::ofstream aOutputJson("build/output_pages_merge.json"); + aOutputJson << aMergedCards.dump(4); +} + +TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards", + "[retrievePageOnceHearthstoneApi]") { + const std::string aContentType{"cards"}; + const std::string aRegion{"eu"}; + bnet::ApiUrl aApiUrl(aContentType, aRegion); + aApiUrl.setPage(3); + bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); + const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() + .allowUpdateSourceFile() + .buildToken() + .getToken() + ->text() + .value()}; + nlohmann::json aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); + const nlohmann::json::size_type aExpectedNumberOfCards{aApiResponse["cards"].size()}; + aApiResponse = + bnet::HearthstoneApi::downloadCards(aApiUrl, *aHearthstoneTokenBuilder.getToken()); + const nlohmann::json::size_type aCurrentNumberOfCards{aApiResponse["cards"].size()}; + REQUIRE(aCurrentNumberOfCards == aExpectedNumberOfCards); +} diff --git a/battleNetApi/tests/src/tests.cpp b/battleNetApi/tests/src/tests.cpp deleted file mode 100644 index edc0641..0000000 --- a/battleNetApi/tests/src/tests.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "apiUrl.hpp" -#include "hearthstoneApi.hpp" -#include <catch2/benchmark/catch_benchmark.hpp> -#include <catch2/benchmark/catch_chronometer.hpp> -#include <catch2/benchmark/detail/catch_analyse.hpp> -#include <catch2/benchmark/detail/catch_benchmark_function.hpp> -#include <catch2/benchmark/detail/catch_estimate_clock.hpp> -#include <catch2/catch_approx.hpp> -#include <catch2/catch_config.hpp> -#include <catch2/catch_test_macros.hpp> -#include <cstdint> -#include <fstream> -#include <nlohmann/json.hpp> -#include <string> -#include <tokenBuilder.hpp> - -TEST_CASE("Retrieve API Token", "[generateApiToken]") { - const std::string aCredentialsFile{"tests/resources/credentials.json"}; - Token aToken(aCredentialsFile); - const std::string aTokenContent{aToken.text()}; - const std::string::size_type aTokenSize{aTokenContent.size()}; - REQUIRE(aTokenSize >= 30); - REQUIRE(aTokenSize <= 40); -} - -TEST_CASE("Retrieve number of cards", "[countCards]") { - const std::string aCredentialsFile{}; - const std::string aContentType{"cards"}; - const std::string aRegion{"eu"}; - ApiUrl aApiUrl(aContentType, aRegion); - aApiUrl.setLocale("fr_FR"); - aApiUrl.setGameMode("constructed"); - aApiUrl.setPageSize("1"); - TokenPool::feedTokenSource("tests/resources/credentials.json"); - const json aResponse = - HearthstoneApi::makeApiRequest(aApiUrl, TokenPool::build().value().text()); - REQUIRE(aResponse.size() > 0); - REQUIRE(aResponse.contains("pageCount")); - std::uint64_t aNumberOfCards{aResponse["pageCount"]}; - std::uint64_t aExpectedNumberOfCards{6178}; - REQUIRE(aNumberOfCards == aExpectedNumberOfCards); -} - -TEST_CASE("Retrieve page once", "[retrievePageOnce]") { - const std::string aCredentialsFile{"tests/resources/credentials.json"}; - Token aToken(kCredentialsFile); - const std::string aContentType{"cards"}; - const std::string aRegion{"eu"}; - ApiUrl aApiUrl(aContentType, aRegion); - aApiUrl.setLocale("fr_FR"); - aApiUrl.setGameMode("constructed"); - aApiUrl.setPageSize("16"); - TokenPool::feedTokenSource("tests/resources/credentials.json"); - const json aApiResponse = - HearthstoneApi::makeApiRequest(aApiUrl, TokenPool::build().value().text()); - REQUIRE(aApiResponse["cards"].size() == 16); -} - -TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoParallel]") { - const std::string aCredentialsFile{"tests/resources/credentials.json"}; - Token aToken(aCredentialsFile); - const std::string aContentType{"cards"}; - const std::string aRegion{"eu"}; - ApiUrl aApiUrl(aContentType, aRegion); - aApiUrl.setLocale("fr_FR"); - aApiUrl.setGameMode("constructed"); - aApiUrl.setPageSize("16"); - aApiUrl.setPage("1"); - TokenPool::feedTokenSource("tests/resources/credentials.json"); - std::string aTokenText{TokenPool::build().value().text()}; - json aApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); - - // Retrieve number of pages - const int aNumberOfRetrievals{10}; - const int aNumberOfPages{aApiResponse["pageCount"]}; - REQUIRE(aNumberOfPages >= aNumberOfRetrievals); - for (int i{1}; i < aNumberOfRetrievals; ++i) { - REQUIRE(aApiResponse["cards"].size() == 16); - aApiUrl.setPage(std::to_string(i + 1)); - aApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); - } - REQUIRE(aApiResponse["cards"].size() >= 1); - REQUIRE(aApiResponse["cards"].size() <= 16); -} - -TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParallel]") { - const std::string aCredentialsFile{"tests/resources/credentials.json"}; - Token aToken(kCredentialsFile); - const std::string aContentType{"cards"}; - const std::string aRegion{"eu"}; - ApiUrl aApiUrl(aContentType, aRegion); - aApiUrl.setLocale("fr_FR"); - aApiUrl.setGameMode("constructed"); - aApiUrl.setPageSize("16"); - aApiUrl.setPage("1"); - std::vector<std::string> aRequests; - TokenPool::feedTokenSource("tests/resources/credentials.json"); - std::string aTokenText{TokenPool::build().value().text()}; - json aFirstPageJson = HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); - - int kNumberOfPages{aFirstPageJson["pageCount"]}; - const int aNumberOfRetrievals{10}; - REQUIRE(aNumberOfPages >= aNumberOfRetrievals); - for (int i{1}; i < aNumberOfRetrievals; ++i) { - aApiUrl.setPage(std::to_string(i + 1)); - aRequests.emplace_back(aApiUrl.text()); - } - std::vector<json> aApiResponses = HearthstoneApi::makeApiRequest(aRequests, aTokenText); - aApiResponses.emplace(aApiResponses.begin(), aFirstPageJson); - REQUIRE(aApiResponses.size() == aNumberOfRetrievals); - for (const json &aApiResponse : aApiResponses) { - REQUIRE(aApiResponse["cards"].size() == 16); - } -} - -TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") { - std::ifstream aFirstPageFile{"tests/resources/page_merge_00.json"}; - json aFirstPageJson = json::parse(aFirstPageFile); - std::ifstream aSecondPageFile{"tests/resources/page_merge_01.json"}; - const json aSecondPageJson = json::parse(aSecondPageFile); - - json::reference aMergedCards = aFirstPageJson["cards"]; - for (auto &card : aSecondPageJson["cards"]) { - aMergedCards.emplace_back(std::move(card)); - } - REQUIRE(aMergedCards.size() == 32); - std::ofstream aOutputJson("build/output_pages_merge.json"); - aOutputJson << aMergedCards.dump(4); -} - -TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards", - "[retrievePageOnceHearthstoneApi]") { - const std::string aContentType{"cards"}; - const std::string aRegion{"eu"}; - ApiUrl aApiUrl(aContentType, aRegion); - aApiUrl.setPage(3); - TokenPool::feedTokenSource("tests/resources/credentials.json"); - json aApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, TokenPool::build().value().text()); - const json::size_type aExpectedNumberOfCards{aApiResponse["cards"].size()}; - TokenPool::feedTokenSource("tests/resources/credentials.json"); - aApiResponse = HearthstoneApi::downloadCards(aApiUrl, TokenPool::build().value()); - const json::size_type aCurrentNumberOfCards{aApiResponse["cards"].size()}; - REQUIRE(aCurrentNumberOfCards == aExpectedNumberOfCards); -} diff --git a/battleNetApi/tests/src/tokenTests.cpp b/battleNetApi/tests/src/tokenTests.cpp index b702024..49e8350 100644 --- a/battleNetApi/tests/src/tokenTests.cpp +++ b/battleNetApi/tests/src/tokenTests.cpp @@ -35,7 +35,7 @@ TEST_CASE("Token", "[Change token text]") { } TEST_CASE("TokenBuilder", "[Retrieve token from JSON]") { - bnet::HearthstoneTokenBuilder aTokenBuilder{"resources/credentials.json"}; + bnet::HearthstoneTokenBuilder aTokenBuilder{"resources/dumb_credentials.json"}; REQUIRE(aTokenBuilder.allowExpiredToken().buildToken().getToken()); REQUIRE(aTokenBuilder.getToken()->text() == "ThisIsTheContentOfTokenContenthehe"); } |
