|
| 1 | +import os |
| 2 | + |
1 | 3 | import pytest |
| 4 | +from openai import AzureOpenAI, OpenAI |
2 | 5 |
|
| 6 | +from codemodder.context import DEFAULT_AZURE_OPENAI_API_VERSION |
3 | 7 | from codemodder.context import CodemodExecutionContext as Context |
| 8 | +from codemodder.context import MisconfiguredAIClient |
4 | 9 | from codemodder.dependency import Security |
5 | 10 | from codemodder.project_analysis.python_repo_manager import PythonRepoManager |
6 | 11 | from codemodder.registry import load_registered_codemods |
@@ -77,3 +82,120 @@ def test_failed_dependency_description(self, mocker): |
77 | 82 | ```""" |
78 | 83 | in description |
79 | 84 | ) |
| 85 | + |
| 86 | + def test_setup_llm_client_no_env_vars(self, mocker): |
| 87 | + mocker.patch.dict(os.environ, clear=True) |
| 88 | + context = Context( |
| 89 | + mocker.Mock(), |
| 90 | + True, |
| 91 | + False, |
| 92 | + load_registered_codemods(), |
| 93 | + PythonRepoManager(mocker.Mock()), |
| 94 | + [], |
| 95 | + [], |
| 96 | + ) |
| 97 | + assert context.llm_client is None |
| 98 | + |
| 99 | + def test_setup_openai_llm_client(self, mocker): |
| 100 | + mocker.patch.dict(os.environ, {"CODEMODDER_OPENAI_API_KEY": "test"}) |
| 101 | + context = Context( |
| 102 | + mocker.Mock(), |
| 103 | + True, |
| 104 | + False, |
| 105 | + load_registered_codemods(), |
| 106 | + PythonRepoManager(mocker.Mock()), |
| 107 | + [], |
| 108 | + [], |
| 109 | + ) |
| 110 | + assert isinstance(context.llm_client, OpenAI) |
| 111 | + |
| 112 | + def test_setup_azure_llm_client(self, mocker): |
| 113 | + mocker.patch.dict( |
| 114 | + os.environ, |
| 115 | + { |
| 116 | + "CODEMODDER_AZURE_OPENAI_API_KEY": "test", |
| 117 | + "CODEMODDER_AZURE_OPENAI_ENDPOINT": "test", |
| 118 | + }, |
| 119 | + ) |
| 120 | + context = Context( |
| 121 | + mocker.Mock(), |
| 122 | + True, |
| 123 | + False, |
| 124 | + load_registered_codemods(), |
| 125 | + PythonRepoManager(mocker.Mock()), |
| 126 | + [], |
| 127 | + [], |
| 128 | + ) |
| 129 | + assert isinstance(context.llm_client, AzureOpenAI) |
| 130 | + assert context.llm_client._api_version == DEFAULT_AZURE_OPENAI_API_VERSION |
| 131 | + |
| 132 | + @pytest.mark.parametrize( |
| 133 | + "env_var", |
| 134 | + ["CODEMODDER_AZURE_OPENAI_API_KEY", "CODEMODDER_AZURE_OPENAI_ENDPOINT"], |
| 135 | + ) |
| 136 | + def test_setup_azure_llm_client_missing_one(self, mocker, env_var): |
| 137 | + mocker.patch.dict(os.environ, {env_var: "test"}) |
| 138 | + with pytest.raises(MisconfiguredAIClient): |
| 139 | + Context( |
| 140 | + mocker.Mock(), |
| 141 | + True, |
| 142 | + False, |
| 143 | + load_registered_codemods(), |
| 144 | + PythonRepoManager(mocker.Mock()), |
| 145 | + [], |
| 146 | + [], |
| 147 | + ) |
| 148 | + |
| 149 | + def test_get_model_name(self, mocker): |
| 150 | + context = Context( |
| 151 | + mocker.Mock(), |
| 152 | + True, |
| 153 | + False, |
| 154 | + load_registered_codemods(), |
| 155 | + PythonRepoManager(mocker.Mock()), |
| 156 | + [], |
| 157 | + [], |
| 158 | + ) |
| 159 | + assert context.gpt_4_turbo_2024_04_09 == "gpt-4-turbo-2024-04-09" |
| 160 | + |
| 161 | + @pytest.mark.parametrize("model", ["gpt-4-turbo-2024-04-09", "gpt-4o-2024-05-13"]) |
| 162 | + def test_model_get_name_from_env(self, mocker, model): |
| 163 | + name = "my-awesome-deployment" |
| 164 | + mocker.patch.dict( |
| 165 | + os.environ, |
| 166 | + { |
| 167 | + f"CODEMODDER_AZURE_OPENAI_{model.upper()}_DEPLOYMENT": name, |
| 168 | + }, |
| 169 | + ) |
| 170 | + context = Context( |
| 171 | + mocker.Mock(), |
| 172 | + True, |
| 173 | + False, |
| 174 | + load_registered_codemods(), |
| 175 | + PythonRepoManager(mocker.Mock()), |
| 176 | + [], |
| 177 | + [], |
| 178 | + ) |
| 179 | + assert getattr(context, model.replace("-", "_")) == name |
| 180 | + |
| 181 | + def test_get_api_version_from_env(self, mocker): |
| 182 | + version = "fake-version" |
| 183 | + mocker.patch.dict( |
| 184 | + os.environ, |
| 185 | + { |
| 186 | + "CODEMODDER_AZURE_OPENAI_API_KEY": "test", |
| 187 | + "CODEMODDER_AZURE_OPENAI_ENDPOINT": "test", |
| 188 | + "CODEMODDER_AZURE_OPENAI_API_VERSION": version, |
| 189 | + }, |
| 190 | + ) |
| 191 | + context = Context( |
| 192 | + mocker.Mock(), |
| 193 | + True, |
| 194 | + False, |
| 195 | + load_registered_codemods(), |
| 196 | + PythonRepoManager(mocker.Mock()), |
| 197 | + [], |
| 198 | + [], |
| 199 | + ) |
| 200 | + assert isinstance(context.llm_client, AzureOpenAI) |
| 201 | + assert context.llm_client._api_version == version |
0 commit comments