Skip to content

Commit

Permalink
test_util: Add test for get_dict_key_from_value
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic2kk committed Aug 1, 2024
1 parent f2573d5 commit 8751228
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pupgui2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import zstandard

from configparser import ConfigParser
from typing import Dict, List, Union, Tuple, Optional, Callable
from typing import Any, Dict, List, Union, Tuple, Optional, Callable

import PySide6
from PySide6.QtCore import QCoreApplication
Expand Down Expand Up @@ -686,11 +686,15 @@ def compat_tool_available(compat_tool: str, ctobjs: List[dict]) -> bool:
return compat_tool in [ctobj['name'] for ctobj in ctobjs]


def get_dict_key_from_value(d, searchval):
def get_dict_key_from_value(d: dict[Any, Any], searchval: Any) -> Any:

"""
Fetch a given dictionary key from a given value.
Returns the given value if found, otherwise None.
Return Type: Any
"""

for key, value in d.items():
if value == searchval:
return key
Expand Down
32 changes: 32 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@
from pupgui2.datastructures import SteamApp, LutrisGame, HeroicGame, Launcher


def test_get_dict_key_from_value() -> None:

"""
Test whether get_dict_key_from_value can retrieve the expected key from a dict by a value,
where the key and value can be of any type.
"""

dict_with_str_keys: dict[str, str] = {
'steam': 'Steam',
'lutris': 'Lutris',
}

dict_with_int_keys: dict[int, str] = {
2: 'two',
4: 'four'
}

dict_with_enum_keys: dict[Launcher, bool] = {
Launcher.WINEZGUI: True,
Launcher.HEROIC: False
}

test_dicts: list[dict[Any, Any]] = [
dict_with_str_keys,
dict_with_int_keys,
dict_with_enum_keys,
]

for test_dict in test_dicts:
assert all( get_dict_key_from_value(test_dict, value) == key for key, value in test_dict.items() )


def test_get_launcher_from_installdir() -> None:

"""
Expand Down

0 comments on commit 8751228

Please sign in to comment.