|
| 1 | +from danoan.llm_assistant.common import config, exception |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +def test_get_configuration_folder_cwd(monkeypatch): |
| 11 | + # Configuration file in the current working dir |
| 12 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 13 | + tmpdir = Path(_tmpdir) |
| 14 | + config_file = tmpdir / config.LLM_ASSISTANT_CONFIGURATION_FILENAME |
| 15 | + config_file.touch() |
| 16 | + |
| 17 | + monkeypatch.chdir(tmpdir) |
| 18 | + assert config.get_configuration_folder() == tmpdir |
| 19 | + |
| 20 | + |
| 21 | +def test_get_configuration_folder_parent_cwd(monkeypatch): |
| 22 | + # Configuration file in a parent directory of current working dir |
| 23 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 24 | + tmpdir = Path(_tmpdir) |
| 25 | + config_file = tmpdir / config.LLM_ASSISTANT_CONFIGURATION_FILENAME |
| 26 | + config_file.touch() |
| 27 | + |
| 28 | + working_dir = tmpdir / "A" / "B" / "C" |
| 29 | + working_dir.mkdir(parents=True) |
| 30 | + |
| 31 | + monkeypatch.chdir(working_dir) |
| 32 | + assert config.get_configuration_folder() == tmpdir |
| 33 | + |
| 34 | + |
| 35 | +def test_get_configuration_folder_envvar(monkeypatch): |
| 36 | + # Configuration file defined by environment variable |
| 37 | + with tempfile.TemporaryDirectory() as _envvar_dir: |
| 38 | + envvar_dir = Path(_envvar_dir) |
| 39 | + monkeypatch.setenv(config.LLM_ASSISTANT_ENV_VARIABLE, str(envvar_dir)) |
| 40 | + |
| 41 | + assert config.get_configuration_folder() == envvar_dir |
| 42 | + |
| 43 | + |
| 44 | +def test_get_configuration_folder_error(): |
| 45 | + # No configuration file and not environment variable defined |
| 46 | + with pytest.raises(exception.EnvironmentVariableNotDefinedError) as ex: |
| 47 | + config.get_configuration_folder() |
| 48 | + |
| 49 | + |
| 50 | +def test_get_configuration_filepath(monkeypatch): |
| 51 | + monkeypatch.setenv(config.LLM_ASSISTANT_ENV_VARIABLE, os.getcwd()) |
| 52 | + assert ( |
| 53 | + config.get_configuration_filepath() |
| 54 | + == Path(os.getcwd()) / config.LLM_ASSISTANT_CONFIGURATION_FILENAME |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_get_configuration(monkeypatch): |
| 59 | + config_file_toml = """ |
| 60 | + [runner] |
| 61 | + openai_key = "key" |
| 62 | + model = "gpt-3.5-turbo" |
| 63 | + use_cache = true |
| 64 | + cache_path = "cache.db" |
| 65 | + local_folder = "local" |
| 66 | + """ |
| 67 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 68 | + tmpdir = Path(_tmpdir) |
| 69 | + config_file = tmpdir / config.LLM_ASSISTANT_CONFIGURATION_FILENAME |
| 70 | + with open(config_file, "w") as f: |
| 71 | + f.write(config_file_toml) |
| 72 | + |
| 73 | + monkeypatch.chdir(tmpdir) |
| 74 | + config_obj = config.get_configuration() |
| 75 | + assert config_obj.runner.openai_key == "key" |
| 76 | + assert config_obj.runner.model == "gpt-3.5-turbo" |
| 77 | + assert config_obj.runner.use_cache == True |
| 78 | + |
| 79 | + |
| 80 | +def test_get_configuration_error(monkeypatch): |
| 81 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 82 | + tmpdir = Path(_tmpdir) |
| 83 | + monkeypatch.chdir(tmpdir) |
| 84 | + with pytest.raises(exception.EnvironmentVariableNotDefinedError): |
| 85 | + config.get_configuration() |
| 86 | + |
| 87 | + monkeypatch.setenv(str(config.LLM_ASSISTANT_ENV_VARIABLE), str(tmpdir)) |
| 88 | + with pytest.raises(exception.ConfigurationFileDoesNotExistError): |
| 89 | + config.get_configuration() |
| 90 | + |
| 91 | + |
| 92 | +def test_get_prompt_configuration(monkeypatch): |
| 93 | + config_file_toml = """ |
| 94 | + [runner] |
| 95 | + openai_key = "key" |
| 96 | + model = "gpt-3.5-turbo" |
| 97 | + use_cache = true |
| 98 | + cache_path = "cache.db" |
| 99 | + local_folder = "prompts" |
| 100 | + """ |
| 101 | + |
| 102 | + prompt_config_file_toml = """ |
| 103 | + name = "" |
| 104 | + system_prompt = "" |
| 105 | + user_prompt = "" |
| 106 | + """ |
| 107 | + |
| 108 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 109 | + tmpdir = Path(_tmpdir) |
| 110 | + |
| 111 | + prompt_folder = tmpdir / "prompts" / "correct-text" |
| 112 | + prompt_folder.mkdir(parents=True) |
| 113 | + prompt_config_file = prompt_folder / "config.toml" |
| 114 | + with open(prompt_config_file, "w") as f: |
| 115 | + f.write(prompt_config_file_toml) |
| 116 | + |
| 117 | + config_file = tmpdir / config.LLM_ASSISTANT_CONFIGURATION_FILENAME |
| 118 | + with open(config_file, "w") as f: |
| 119 | + f.write(config_file_toml) |
| 120 | + |
| 121 | + monkeypatch.chdir(tmpdir) |
| 122 | + assert config.get_prompt_configuration("correct-text") is not None |
0 commit comments