From 748948c99440a6f4736d4e23bf074ce256ce5b9e Mon Sep 17 00:00:00 2001 From: Valentin Porchet Date: Wed, 25 Jan 2023 21:29:04 +0100 Subject: [PATCH] Added locale optional query parameter on heroes, roles and gamemodes routes (#10) --- README.md | 1 - .../commands/check_and_update_cache.py | 5 +- overfastapi/commands/update_test_fixtures.py | 7 +- overfastapi/common/enums.py | 18 +++++ overfastapi/config.example.py | 8 +-- .../search_players_request_handler.py | 4 +- overfastapi/main.py | 1 - overfastapi/parsers/api_parser.py | 9 +-- overfastapi/parsers/helpers.py | 5 ++ overfastapi/parsers/hero_parser.py | 7 +- overfastapi/parsers/player_parser.py | 2 +- overfastapi/parsers/roles_parser.py | 3 +- overfastapi/routers/gamemodes.py | 7 +- overfastapi/routers/heroes.py | 8 ++- overfastapi/routers/roles.py | 10 +-- pyproject.toml | 2 +- tests/commands/test_check_and_update_cache.py | 65 ++++++++++++------- tests/common/test_cache_manager.py | 18 +++-- tests/views/test_heroes_route.py | 6 +- 19 files changed, 120 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 071b331..4cf050a 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ ## 👷 W.I.P. 👷 -- Translations for specific heroes pages (will be available using a query parameter) - Additional data about gamemodes and maps ## Table of contents diff --git a/overfastapi/commands/check_and_update_cache.py b/overfastapi/commands/check_and_update_cache.py index cee0846..153e75f 100644 --- a/overfastapi/commands/check_and_update_cache.py +++ b/overfastapi/commands/check_and_update_cache.py @@ -47,10 +47,11 @@ def get_request_parser_class(cache_key: str) -> tuple[type, dict]: ) cache_parser_class = PARSER_CLASSES_MAPPING[parser_class_name] + cache_kwargs["locale"] = uri[1] if parser_class_name in ["PlayerParser", "PlayerStatsSummaryParser"]: - cache_kwargs = {"player_id": uri[3]} + cache_kwargs["player_id"] = uri[3] elif parser_class_name == "HeroParser": - cache_kwargs = {"hero_key": uri[3]} + cache_kwargs["hero_key"] = uri[3] return cache_parser_class, cache_kwargs diff --git a/overfastapi/commands/update_test_fixtures.py b/overfastapi/commands/update_test_fixtures.py index aea8046..e958455 100644 --- a/overfastapi/commands/update_test_fixtures.py +++ b/overfastapi/commands/update_test_fixtures.py @@ -6,7 +6,7 @@ import requests -from overfastapi.common.enums import HeroKey +from overfastapi.common.enums import HeroKey, Locale from overfastapi.common.helpers import players_ids from overfastapi.common.logging import logger from overfastapi.config import ( @@ -109,14 +109,15 @@ def main(): # Initialize data route_file_mapping = list_routes_to_update(args) + locale = Locale.ENGLISH_US # Do the job test_data_path = f"{TEST_FIXTURES_ROOT_PATH}/html" with requests.Session() as session: for route, filepath in route_file_mapping.items(): logger.info(f"Updating {test_data_path}{filepath}...") - logger.info(f"GET {BLIZZARD_HOST}{route}...") - response = session.get(f"{BLIZZARD_HOST}{route}") + logger.info(f"GET {BLIZZARD_HOST}/{locale}{route}...") + response = session.get(f"{BLIZZARD_HOST}/{locale}{route}") logger.debug( f"HTTP {response.status_code} / Time : {response.elapsed.total_seconds()}" ) diff --git a/overfastapi/common/enums.py b/overfastapi/common/enums.py index c793d89..3341135 100644 --- a/overfastapi/common/enums.py +++ b/overfastapi/common/enums.py @@ -170,3 +170,21 @@ class CompetitiveDivision(StrEnum): DIAMOND = "diamond" MASTER = "master" GRANDMASTER = "grandmaster" + + +class Locale(StrEnum): + """Locales supported by Blizzard""" + + GERMAN = "de-de" + ENGLISH_EU = "en-gb" + ENGLISH_US = "en-us" + SPANISH_EU = "es-es" + SPANISH_LATIN = "es-mx" + FRENCH = "fr-fr" + ITALIANO = "it-it" + JAPANESE = "ja-jp" + KOREAN = "ko-kr" + POLISH = "pl-pl" + PORTUGUESE_BRAZIL = "pt-br" + RUSSIAN = "ru-ru" + CHINESE_TAIWAN = "zh-tw" diff --git a/overfastapi/config.example.py b/overfastapi/config.example.py index 9a33df3..0967abf 100644 --- a/overfastapi/config.example.py +++ b/overfastapi/config.example.py @@ -66,16 +66,16 @@ BLIZZARD_HOST = "https://overwatch.blizzard.com" # Blizzard home page with some details -HOME_PATH = "/en-us" +HOME_PATH = "/" # Route for Overwatch heroes pages -HEROES_PATH = "/en-us/heroes" +HEROES_PATH = "/heroes" # Route for players career pages -CAREER_PATH = "/en-us/career" +CAREER_PATH = "/career" # Route for searching Overwatch accounts by name -SEARCH_ACCOUNT_PATH = "/en-us/search/account-by-name" +SEARCH_ACCOUNT_PATH = "/search/account-by-name" ############ # CRITICAL ERROR DISCORD WEBHOOK diff --git a/overfastapi/handlers/search_players_request_handler.py b/overfastapi/handlers/search_players_request_handler.py index 06a29bc..80b97ef 100644 --- a/overfastapi/handlers/search_players_request_handler.py +++ b/overfastapi/handlers/search_players_request_handler.py @@ -4,6 +4,7 @@ from fastapi import Request from overfastapi.common.cache_manager import CacheManager +from overfastapi.common.enums import Locale from overfastapi.common.helpers import ( blizzard_response_error_from_request, overfast_request, @@ -128,4 +129,5 @@ def apply_ordering(players: list[dict], order_by: str) -> list[dict]: @staticmethod def get_blizzard_url(**kwargs) -> str: """URL used when requesting data to Blizzard.""" - return f"{BLIZZARD_HOST}{SEARCH_ACCOUNT_PATH}/{kwargs.get('name')}" + locale = Locale.ENGLISH_US + return f"{BLIZZARD_HOST}/{locale}{SEARCH_ACCOUNT_PATH}/{kwargs.get('name')}" diff --git a/overfastapi/main.py b/overfastapi/main.py index 503de7a..b17cec3 100644 --- a/overfastapi/main.py +++ b/overfastapi/main.py @@ -24,7 +24,6 @@ ## 👷 W.I.P. 👷 -- Translations for specific heroes pages (will be available using a query parameter) - Additional data about gamemodes and maps """ diff --git a/overfastapi/parsers/api_parser.py b/overfastapi/parsers/api_parser.py index c378e77..d35d7a2 100644 --- a/overfastapi/parsers/api_parser.py +++ b/overfastapi/parsers/api_parser.py @@ -5,6 +5,7 @@ from bs4 import BeautifulSoup from overfastapi.common.cache_manager import CacheManager +from overfastapi.common.enums import Locale from overfastapi.common.exceptions import ParserParsingError from overfastapi.common.helpers import ( blizzard_response_error_from_request, @@ -102,18 +103,14 @@ def parse_data(self) -> dict | list[dict]: raise an error if there is an issue when parsing the data. """ - @cached_property - def blizzard_root_url(self) -> str: - """Property containing Root URL for requesting data to Blizzard""" - return f"{BLIZZARD_HOST}{self.root_path}" - def get_blizzard_url(self, **kwargs) -> str: """URL used when requesting data to Blizzard. It usually is a concatenation of root url and query data (kwargs) if the RequestHandler supports it. For example : single hero page (hero key), player career page (player id, etc.). Default is just the blizzard root url. """ - return self.blizzard_root_url + locale = kwargs.get("locale") or Locale.ENGLISH_US + return f"{BLIZZARD_HOST}/{locale}{self.root_path}" def filter_request_using_query(self, **kwargs) -> dict | list: """If the route contains subroutes accessible using GET queries, this method diff --git a/overfastapi/parsers/helpers.py b/overfastapi/parsers/helpers.py index e98fa72..146ec78 100644 --- a/overfastapi/parsers/helpers.py +++ b/overfastapi/parsers/helpers.py @@ -132,3 +132,8 @@ def get_hero_role(hero_key: HeroKey) -> Role: ]: return Role.TANK return Role.DAMAGE + + +def get_role_from_icon_url(url: str) -> str: + """Extracts the role key name from the associated icon URL""" + return url.split("/")[-1].split(".")[0].lower() diff --git a/overfastapi/parsers/hero_parser.py b/overfastapi/parsers/hero_parser.py index aede2f2..20f6b4c 100644 --- a/overfastapi/parsers/hero_parser.py +++ b/overfastapi/parsers/hero_parser.py @@ -4,7 +4,7 @@ from overfastapi.common.enums import MediaType from overfastapi.config import HERO_PATH_CACHE_TIMEOUT, HEROES_PATH from overfastapi.parsers.api_parser import APIParser -from overfastapi.parsers.helpers import get_full_url +from overfastapi.parsers.helpers import get_full_url, get_role_from_icon_url class HeroParser(APIParser): @@ -14,7 +14,7 @@ class HeroParser(APIParser): timeout = HERO_PATH_CACHE_TIMEOUT def get_blizzard_url(self, **kwargs) -> str: - return f"{self.blizzard_root_url}/{kwargs.get('hero_key')}" + return f"{super().get_blizzard_url(**kwargs)}/{kwargs.get('hero_key')}" def parse_data(self) -> dict: overview_section = self.root_tag.find("blz-page-header", recursive=False) @@ -33,12 +33,13 @@ def parse_data(self) -> dict: def __get_summary(overview_section: Tag) -> dict: header_section = overview_section.find("blz-header") extra_list_items = overview_section.find("blz-list").find_all("blz-list-item") + return { "name": header_section.find("h2").get_text(), "description": ( header_section.find("p", slot="description").get_text().strip() ), - "role": extra_list_items[0].get_text().lower(), + "role": get_role_from_icon_url(extra_list_items[0].find("image")["href"]), "location": extra_list_items[1].get_text(), } diff --git a/overfastapi/parsers/player_parser.py b/overfastapi/parsers/player_parser.py index b73df64..00fcec1 100644 --- a/overfastapi/parsers/player_parser.py +++ b/overfastapi/parsers/player_parser.py @@ -49,7 +49,7 @@ def __init__(self, **kwargs): self.player_id = kwargs.get("player_id") def get_blizzard_url(self, **kwargs) -> str: - return f"{self.blizzard_root_url}/{kwargs.get('player_id')}/" + return f"{super().get_blizzard_url(**kwargs)}/{kwargs.get('player_id')}/" def filter_request_using_query(self, **kwargs) -> dict: if kwargs.get("summary"): diff --git a/overfastapi/parsers/roles_parser.py b/overfastapi/parsers/roles_parser.py index 7a09b0c..4fb4b38 100644 --- a/overfastapi/parsers/roles_parser.py +++ b/overfastapi/parsers/roles_parser.py @@ -1,6 +1,7 @@ """Roles Parser module""" from overfastapi.config import HEROES_PATH_CACHE_TIMEOUT, HOME_PATH from overfastapi.parsers.api_parser import APIParser +from overfastapi.parsers.helpers import get_role_from_icon_url class RolesParser(APIParser): @@ -23,7 +24,7 @@ def parse_data(self) -> list[dict]: return [ { - "key": role_div.find("blz-header").find("h2").get_text().lower(), + "key": get_role_from_icon_url(roles_icons[role_index]), "name": role_div.find("blz-header").find("h2").get_text().capitalize(), "icon": roles_icons[role_index], "description": ( diff --git a/overfastapi/routers/gamemodes.py b/overfastapi/routers/gamemodes.py index eae780d..99f3a3e 100644 --- a/overfastapi/routers/gamemodes.py +++ b/overfastapi/routers/gamemodes.py @@ -1,8 +1,8 @@ """Gamemodes endpoints router : gamemodes list, etc.""" -from fastapi import APIRouter, BackgroundTasks, Request +from fastapi import APIRouter, BackgroundTasks, Query, Request from overfastapi.common.decorators import validation_error_handler -from overfastapi.common.enums import RouteTag +from overfastapi.common.enums import Locale, RouteTag from overfastapi.common.helpers import routes_responses from overfastapi.handlers.list_gamemodes_request_handler import ( ListGamemodesRequestHandler, @@ -26,7 +26,8 @@ async def list_map_gamemodes( background_tasks: BackgroundTasks, request: Request, + locale: Locale = Query(Locale.ENGLISH_US, title="Locale to be displayed"), ) -> list[GamemodeDetails]: return ListGamemodesRequestHandler(request).process_request( - background_tasks=background_tasks + background_tasks=background_tasks, locale=locale ) diff --git a/overfastapi/routers/heroes.py b/overfastapi/routers/heroes.py index a7a9dab..06829e5 100644 --- a/overfastapi/routers/heroes.py +++ b/overfastapi/routers/heroes.py @@ -3,7 +3,7 @@ from fastapi import APIRouter, BackgroundTasks, Path, Query, Request from overfastapi.common.decorators import validation_error_handler -from overfastapi.common.enums import HeroKey, Role, RouteTag +from overfastapi.common.enums import HeroKey, Locale, Role, RouteTag from overfastapi.common.helpers import routes_responses from overfastapi.handlers.get_hero_request_handler import GetHeroRequestHandler from overfastapi.handlers.list_heroes_request_handler import ListHeroesRequestHandler @@ -27,9 +27,10 @@ async def list_heroes( background_tasks: BackgroundTasks, request: Request, role: Role | None = Query(None, title="Role filter"), + locale: Locale = Query(Locale.ENGLISH_US, title="Locale to be displayed"), ) -> list[HeroShort]: return ListHeroesRequestHandler(request).process_request( - background_tasks=background_tasks, role=role + background_tasks=background_tasks, role=role, locale=locale ) @@ -48,7 +49,8 @@ async def get_hero( background_tasks: BackgroundTasks, request: Request, hero_key: HeroKey = Path(title="Key name of the hero"), + locale: Locale = Query(Locale.ENGLISH_US, title="Locale to be displayed"), ) -> Hero: return GetHeroRequestHandler(request).process_request( - background_tasks=background_tasks, hero_key=hero_key + background_tasks=background_tasks, hero_key=hero_key, locale=locale ) diff --git a/overfastapi/routers/roles.py b/overfastapi/routers/roles.py index 5605095..6b433a4 100644 --- a/overfastapi/routers/roles.py +++ b/overfastapi/routers/roles.py @@ -1,9 +1,9 @@ """Roles endpoints router : roles list, etc.""" -from fastapi import APIRouter, BackgroundTasks, Request +from fastapi import APIRouter, BackgroundTasks, Query, Request from overfastapi.common.decorators import validation_error_handler -from overfastapi.common.enums import RouteTag +from overfastapi.common.enums import Locale, RouteTag from overfastapi.common.helpers import routes_responses from overfastapi.handlers.list_roles_request_handler import ListRolesRequestHandler from overfastapi.models.heroes import RoleDetail @@ -20,8 +20,10 @@ ) @validation_error_handler(response_model=RoleDetail) async def list_roles( - background_tasks: BackgroundTasks, request: Request + background_tasks: BackgroundTasks, + request: Request, + locale: Locale = Query(Locale.ENGLISH_US, title="Locale to be displayed"), ) -> list[RoleDetail]: return ListRolesRequestHandler(request).process_request( - background_tasks=background_tasks + background_tasks=background_tasks, locale=locale ) diff --git a/pyproject.toml b/pyproject.toml index 2354413..85e6d62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "overfast-api" -version = "2.5.0" +version = "2.6.0" description = "Overwatch API giving data about heroes, maps, and players statistics." authors = ["TeKrop "] license = "MIT" diff --git a/tests/commands/test_check_and_update_cache.py b/tests/commands/test_check_and_update_cache.py index 2076c5d..7b209a0 100644 --- a/tests/commands/test_check_and_update_cache.py +++ b/tests/commands/test_check_and_update_cache.py @@ -8,6 +8,7 @@ main as check_and_update_cache_main, ) from overfastapi.common.cache_manager import CacheManager +from overfastapi.common.enums import Locale from overfastapi.config import ( BLIZZARD_HOST, CAREER_PATH, @@ -23,20 +24,28 @@ def cache_manager(): return CacheManager() +@pytest.fixture(scope="function") +def locale(): + return Locale.ENGLISH_US + + def test_check_and_update_gamemodes_cache_to_update( - cache_manager: CacheManager, home_html_data: list, gamemodes_json_data: dict + cache_manager: CacheManager, + locale: str, + home_html_data: list, + gamemodes_json_data: dict, ): - gamemodes_cache_key = f"GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}" + gamemodes_cache_key = f"GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}" complete_cache_key = f"{PARSER_CACHE_KEY_PREFIX}:{gamemodes_cache_key}" # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"PlayerParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217", + f"PlayerParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217", {}, EXPIRED_CACHE_REFRESH_LIMIT + 30, ) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT + 5, ) @@ -67,9 +76,9 @@ def test_check_and_update_gamemodes_cache_to_update( indirect=["hero_html_data", "hero_json_data"], ) def test_check_and_update_specific_hero_to_update( - cache_manager: CacheManager, hero_html_data: str, hero_json_data: dict + cache_manager: CacheManager, locale: str, hero_html_data: str, hero_json_data: dict ): - ana_cache_key = f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana" + ana_cache_key = f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana" complete_cache_key = f"{PARSER_CACHE_KEY_PREFIX}:{ana_cache_key}" # Add some data (to update and not to update) @@ -100,20 +109,20 @@ def test_check_and_update_specific_hero_to_update( assert cache_manager.get_parser_cache(ana_cache_key) == hero_data -def test_check_and_update_cache_no_update(cache_manager: CacheManager): +def test_check_and_update_cache_no_update(cache_manager: CacheManager, locale: str): # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"PlayerParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217", + f"PlayerParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217", {}, EXPIRED_CACHE_REFRESH_LIMIT + 30, ) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT + 5, ) cache_manager.update_parser_cache( - f"GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}", + f"GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}", [], EXPIRED_CACHE_REFRESH_LIMIT + 10, ) @@ -135,11 +144,14 @@ def test_check_and_update_cache_no_update(cache_manager: CacheManager): ) def test_check_and_update_specific_player_to_update( cache_manager: CacheManager, + locale: str, player_id: str, player_html_data: str, player_json_data: dict, ): - player_cache_key = f"PlayerParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217/" + player_cache_key = ( + f"PlayerParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217/" + ) complete_cache_key = f"{PARSER_CACHE_KEY_PREFIX}:{player_cache_key}" # Add some data (to update and not to update) @@ -147,12 +159,12 @@ def test_check_and_update_specific_player_to_update( player_cache_key, {}, EXPIRED_CACHE_REFRESH_LIMIT - 5 ) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT + 5, ) cache_manager.update_parser_cache( - f"GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}", + f"GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}", [], EXPIRED_CACHE_REFRESH_LIMIT + 10, ) @@ -186,12 +198,13 @@ def test_check_and_update_specific_player_to_update( ) def test_check_and_update_player_stats_summary_to_update( cache_manager: CacheManager, + locale: str, player_id: str, player_html_data: str, player_stats_json_data: dict, ): player_stats_cache_key = ( - f"PlayerStatsSummaryParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217/" + f"PlayerStatsSummaryParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217/" ) complete_cache_key = f"{PARSER_CACHE_KEY_PREFIX}:{player_stats_cache_key}" @@ -200,12 +213,12 @@ def test_check_and_update_player_stats_summary_to_update( player_stats_cache_key, {}, EXPIRED_CACHE_REFRESH_LIMIT - 5 ) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT + 5, ) cache_manager.update_parser_cache( - f"GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}", + f"GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}", [], EXPIRED_CACHE_REFRESH_LIMIT + 10, ) @@ -234,10 +247,10 @@ def test_check_and_update_player_stats_summary_to_update( ) -def test_check_internal_error_from_blizzard(cache_manager: CacheManager): +def test_check_internal_error_from_blizzard(cache_manager: CacheManager, locale: str): # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT - 5, ) @@ -254,10 +267,10 @@ def test_check_internal_error_from_blizzard(cache_manager: CacheManager): ) -def test_check_timeout_from_blizzard(cache_manager: CacheManager): +def test_check_timeout_from_blizzard(cache_manager: CacheManager, locale: str): # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT - 5, ) @@ -280,10 +293,12 @@ def test_check_timeout_from_blizzard(cache_manager: CacheManager): @pytest.mark.parametrize("player_html_data", ["TeKrop-2217"], indirect=True) -def test_check_parser_parsing_error(cache_manager: CacheManager, player_html_data: str): +def test_check_parser_parsing_error( + cache_manager: CacheManager, locale: str, player_html_data: str +): # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"PlayerParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217", + f"PlayerParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217", {}, EXPIRED_CACHE_REFRESH_LIMIT - 5, ) @@ -307,10 +322,12 @@ def test_check_parser_parsing_error(cache_manager: CacheManager, player_html_dat @pytest.mark.parametrize("player_html_data", ["Unknown-1234"], indirect=True) -def test_check_parser_init_error(cache_manager: CacheManager, player_html_data: str): +def test_check_parser_init_error( + cache_manager: CacheManager, locale: str, player_html_data: str +): # Add some data (to update and not to update) cache_manager.update_parser_cache( - f"PlayerParser-{BLIZZARD_HOST}{CAREER_PATH}/TeKrop-2217", + f"PlayerParser-{BLIZZARD_HOST}/{locale}{CAREER_PATH}/TeKrop-2217", {}, EXPIRED_CACHE_REFRESH_LIMIT - 5, ) diff --git a/tests/common/test_cache_manager.py b/tests/common/test_cache_manager.py index 72b259c..a305129 100644 --- a/tests/common/test_cache_manager.py +++ b/tests/common/test_cache_manager.py @@ -6,6 +6,7 @@ from redis.exceptions import RedisError from overfastapi.common.cache_manager import CacheManager +from overfastapi.common.enums import Locale from overfastapi.config import ( BLIZZARD_HOST, EXPIRED_CACHE_REFRESH_LIMIT, @@ -20,6 +21,11 @@ def cache_manager(): return CacheManager() +@pytest.fixture(scope="function") +def locale(): + return Locale.ENGLISH_US + + @pytest.mark.parametrize( "req,expected", [ @@ -118,8 +124,8 @@ def test_update_and_get_parser_cache( ( True, { - f"{PARSER_CACHE_KEY_PREFIX}:GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}", - f"{PARSER_CACHE_KEY_PREFIX}:HeroesParser-{BLIZZARD_HOST}{HEROES_PATH}", + f"{PARSER_CACHE_KEY_PREFIX}:GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}", + f"{PARSER_CACHE_KEY_PREFIX}:HeroesParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}", }, ), (False, set()), @@ -133,17 +139,17 @@ def test_get_soon_expired_parser_cache_keys( is_redis_server_up, ): cache_manager.update_parser_cache( - f"HeroParser-{BLIZZARD_HOST}{HEROES_PATH}/ana", + f"HeroParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}/ana", {}, EXPIRED_CACHE_REFRESH_LIMIT + 5, ) cache_manager.update_parser_cache( - f"GamemodesParser-{BLIZZARD_HOST}{HOME_PATH}", + f"GamemodesParser-{BLIZZARD_HOST}/{locale}{HOME_PATH}", [], EXPIRED_CACHE_REFRESH_LIMIT - 5, ) cache_manager.update_parser_cache( - f"HeroesParser-{BLIZZARD_HOST}{HEROES_PATH}", + f"HeroesParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}", [{"name": "Sojourn"}], EXPIRED_CACHE_REFRESH_LIMIT - 10, ) @@ -155,7 +161,7 @@ def test_redis_connection_error(cache_manager: CacheManager): redis_connection_error = RedisError( "Error 111 connecting to 127.0.0.1:6379. Connection refused." ) - heroes_cache_key = f"HeroesParser-{BLIZZARD_HOST}{HEROES_PATH}" + heroes_cache_key = f"HeroesParser-{BLIZZARD_HOST}/{locale}{HEROES_PATH}" with patch( "overfastapi.common.cache_manager.redis.Redis.get", side_effect=redis_connection_error, diff --git a/tests/views/test_heroes_route.py b/tests/views/test_heroes_route.py index 7e2e248..b2951e6 100644 --- a/tests/views/test_heroes_route.py +++ b/tests/views/test_heroes_route.py @@ -4,7 +4,7 @@ from fastapi.testclient import TestClient from overfastapi.common.cache_manager import CacheManager -from overfastapi.common.enums import Role +from overfastapi.common.enums import Locale, Role from overfastapi.config import BLIZZARD_HOST, HEROES_PATH from overfastapi.main import app @@ -46,7 +46,9 @@ def test_get_heroes_from_api_cache(heroes_json_data: list): def test_get_heroes_from_parser_cache(heroes_json_data: list): cache_manager = CacheManager() cache_manager.update_parser_cache( - f"HeroesParser-{BLIZZARD_HOST}{HEROES_PATH}", heroes_json_data, 100 + f"HeroesParser-{BLIZZARD_HOST}/{Locale.ENGLISH_US}{HEROES_PATH}", + heroes_json_data, + 100, ) response = client.get("/heroes")