-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtest_prompts.py
199 lines (150 loc) · 6.91 KB
/
test_prompts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""Tests for the prompts module."""
from pathlib import Path
import pytest
import yaml
from codegate.config import Config
from codegate.exceptions import ConfigurationError
from codegate.prompts import PromptConfig
@pytest.fixture
def temp_prompts_file(tmp_path):
"""Create a temporary prompts file for testing."""
prompts_data = {
"test_prompt": "This is a test prompt",
"another_prompt": "Another test prompt",
}
prompts_file = tmp_path / "test_prompts.yaml"
with open(prompts_file, "w") as f:
yaml.safe_dump(prompts_data, f)
return prompts_file
@pytest.fixture
def temp_env_prompts_file(tmp_path):
"""Create a temporary prompts file for environment testing."""
prompts_data = {
"env_prompt": "This is an environment prompt",
"another_env": "Another environment prompt",
}
prompts_file = tmp_path / "env_prompts.yaml"
with open(prompts_file, "w") as f:
yaml.safe_dump(prompts_data, f)
return prompts_file
@pytest.fixture
def temp_config_file(tmp_path, temp_prompts_file):
"""Create a temporary config file for testing."""
config_data = {
"prompts": {
"inline_prompt": "This is an inline prompt",
"another_inline": "Another inline prompt",
}
}
config_file = tmp_path / "test_config.yaml"
with open(config_file, "w") as f:
yaml.safe_dump(config_data, f)
return config_file
@pytest.fixture
def temp_config_with_prompts_file(tmp_path, temp_prompts_file):
"""Create a temporary config file that references a prompts file."""
config_data = {
"prompts": str(temp_prompts_file),
}
config_file = tmp_path / "test_config_with_prompts.yaml"
with open(config_file, "w") as f:
yaml.safe_dump(config_data, f)
return config_file
def test_default_prompts():
"""Test loading default prompts."""
config = Config.load()
assert len(config.prompts.prompts) > 0
assert hasattr(config.prompts, "default_chat")
assert "You are CodeGate" in config.prompts.default_chat
def test_cli_prompts_override_default(temp_prompts_file):
"""Test that CLI prompts override default prompts."""
config = Config.load(prompts_path=temp_prompts_file)
assert len(config.prompts.prompts) == 2
assert config.prompts.test_prompt == "This is a test prompt"
assert not hasattr(config.prompts, "default_chat")
def test_env_prompts_override_default(temp_env_prompts_file, monkeypatch):
"""Test that environment prompts override default prompts."""
monkeypatch.setenv("CODEGATE_PROMPTS_FILE", str(temp_env_prompts_file))
config = Config.load()
assert len(config.prompts.prompts) == 2
assert config.prompts.env_prompt == "This is an environment prompt"
assert not hasattr(config.prompts, "default_chat")
def test_config_prompts_override_default(temp_config_file):
"""Test that config prompts override default prompts."""
config = Config.load(config_path=temp_config_file)
assert len(config.prompts.prompts) == 2
assert config.prompts.inline_prompt == "This is an inline prompt"
assert not hasattr(config.prompts, "default_chat")
def test_load_prompts_from_file(temp_prompts_file):
"""Test loading prompts from a YAML file."""
config = Config.load(prompts_path=temp_prompts_file)
assert len(config.prompts.prompts) == 2
assert config.prompts.test_prompt == "This is a test prompt"
assert config.prompts.another_prompt == "Another test prompt"
def test_load_prompts_from_config(temp_config_file):
"""Test loading inline prompts from config file."""
config = Config.load(config_path=temp_config_file)
assert len(config.prompts.prompts) == 2
assert config.prompts.inline_prompt == "This is an inline prompt"
assert config.prompts.another_inline == "Another inline prompt"
def test_load_prompts_from_config_file_reference(temp_config_with_prompts_file):
"""Test loading prompts from a file referenced in config."""
config = Config.load(config_path=temp_config_with_prompts_file)
assert len(config.prompts.prompts) == 2
assert config.prompts.test_prompt == "This is a test prompt"
assert config.prompts.another_prompt == "Another test prompt"
def test_prompt_attribute_access():
"""Test accessing prompts via attributes."""
prompts = PromptConfig(prompts={"test": "Test prompt"})
assert prompts.test == "Test prompt"
with pytest.raises(AttributeError):
_ = prompts.nonexistent
def test_prompt_validation():
"""Test prompt validation."""
# Valid prompts (all strings)
PromptConfig(prompts={"test": "Test prompt", "another": "Another prompt"})
# Invalid prompts (non-string value)
with pytest.raises(ConfigurationError):
PromptConfig.from_file(Path(__file__).parent / "data" / "invalid_prompts.yaml")
def test_environment_variable_override(temp_env_prompts_file, monkeypatch):
"""Test loading prompts from environment variable."""
monkeypatch.setenv("CODEGATE_PROMPTS_FILE", str(temp_env_prompts_file))
config = Config.load()
assert len(config.prompts.prompts) == 2
assert config.prompts.env_prompt == "This is an environment prompt"
assert config.prompts.another_env == "Another environment prompt"
def test_cli_override_takes_precedence(temp_prompts_file, temp_env_prompts_file, monkeypatch):
"""Test that CLI prompts override config and environment."""
# Set environment variable
monkeypatch.setenv("CODEGATE_PROMPTS_FILE", str(temp_env_prompts_file))
# Load with CLI override
config = Config.load(prompts_path=temp_prompts_file)
# Should use prompts from CLI-specified file
assert len(config.prompts.prompts) == 2
assert config.prompts.test_prompt == "This is a test prompt"
assert config.prompts.another_prompt == "Another test prompt"
def test_invalid_yaml_file():
"""Test handling of invalid YAML file."""
with pytest.raises(ConfigurationError):
PromptConfig.from_file(Path(__file__).parent / "nonexistent.yaml")
def test_empty_prompts_file(tmp_path):
"""Test handling of empty prompts file."""
empty_file = tmp_path / "empty.yaml"
empty_file.write_text("")
with pytest.raises(ConfigurationError):
PromptConfig.from_file(empty_file)
def test_non_dict_prompts_file(tmp_path):
"""Test handling of non-dictionary prompts file."""
invalid_file = tmp_path / "invalid.yaml"
invalid_file.write_text("- not a dictionary")
with pytest.raises(ConfigurationError):
PromptConfig.from_file(invalid_file)
def test_missing_default_prompts(monkeypatch):
"""Test graceful handling of missing default prompts file."""
# Temporarily modify the path to point to a nonexistent location
def mock_load_default_prompts():
return PromptConfig()
monkeypatch.setattr(Config, "_load_default_prompts", mock_load_default_prompts)
config = Config.load()
assert isinstance(config.prompts, PromptConfig)
assert len(config.prompts.prompts) == 0