diff --git a/tests/files/dummy_json_api_key.json b/tests/files/dummy_json_api_key.json new file mode 100644 index 00000000..a3deea12 --- /dev/null +++ b/tests/files/dummy_json_api_key.json @@ -0,0 +1,3 @@ +{ + "key": 123456789 +} diff --git a/tests/test_util.py b/tests/test_util.py index b6b6c5fb..1348b46f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -116,9 +116,6 @@ def get_modules(*path: str): QUERIES_UTIL_DIR.relative_to(GREYNIR_ROOT_DIR) ) == get_modules("queries", "util") - # TODO: Test read_api_key - # from util import read_api_key - assert cap_first("yolo") == "Yolo" assert cap_first("YOLO") == "YOLO" assert cap_first("yoLo") == "YoLo" @@ -128,3 +125,34 @@ def get_modules(*path: str): assert icequote("sæll") == "„sæll“" assert icequote(" Góðan daginn ") == "„Góðan daginn“" + + +def test_read_json_api_key(): + """Test reading API keys from JSON files.""" + + from utility import read_json_api_key, GREYNIR_ROOT_DIR + + # Test reading a non-existent key + assert read_json_api_key("nonexistent") == {} + + # Test reading a key from a JSON file + assert read_json_api_key( + "dummy_json_api_key", folder=GREYNIR_ROOT_DIR / "tests/files" + ) == {"key": 123456789} + + +def test_read_txt_api_key(): + """Test reading API keys from .txt files.""" + + from utility import read_txt_api_key, GREYNIR_ROOT_DIR + + # Test reading a non-existent key + assert read_txt_api_key("nonexistent") == "" + + # Test reading a key from a .txt file + assert ( + read_txt_api_key( + "dummy_greynir_api_key", folder=GREYNIR_ROOT_DIR / "tests/files" + ) + == "123456789" + )