Skip to content
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

test_util: Add test for get_dict_key_from_value #438

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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