|
| 1 | +"""Tests for XML prompt configuration. |
| 2 | +
|
| 3 | +Module: prompts/config.py (77 lines) |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from empathy_os.prompts.config import XmlPromptConfig |
| 9 | + |
| 10 | + |
| 11 | +# ============================================================================ |
| 12 | +# XmlPromptConfig Creation Tests |
| 13 | +# ============================================================================ |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.unit |
| 17 | +class TestXmlPromptConfigCreation: |
| 18 | + """Test suite for XmlPromptConfig creation.""" |
| 19 | + |
| 20 | + def test_create_default_config(self): |
| 21 | + """Test creating XmlPromptConfig with defaults.""" |
| 22 | + # When |
| 23 | + config = XmlPromptConfig() |
| 24 | + |
| 25 | + # Then |
| 26 | + assert config.enabled is False |
| 27 | + assert config.schema_version == "1.0" |
| 28 | + assert config.enforce_response_xml is False |
| 29 | + assert config.fallback_on_parse_error is True |
| 30 | + assert config.template_name is None |
| 31 | + assert config.custom_template is None |
| 32 | + assert config.extra == {} |
| 33 | + |
| 34 | + def test_create_with_custom_values(self): |
| 35 | + """Test creating XmlPromptConfig with custom values.""" |
| 36 | + # When |
| 37 | + config = XmlPromptConfig( |
| 38 | + enabled=True, |
| 39 | + schema_version="2.0", |
| 40 | + enforce_response_xml=True, |
| 41 | + fallback_on_parse_error=False, |
| 42 | + template_name="test_template", |
| 43 | + custom_template="<xml>test</xml>", |
| 44 | + extra={"key": "value"} |
| 45 | + ) |
| 46 | + |
| 47 | + # Then |
| 48 | + assert config.enabled is True |
| 49 | + assert config.schema_version == "2.0" |
| 50 | + assert config.enforce_response_xml is True |
| 51 | + assert config.fallback_on_parse_error is False |
| 52 | + assert config.template_name == "test_template" |
| 53 | + assert config.custom_template == "<xml>test</xml>" |
| 54 | + assert config.extra == {"key": "value"} |
| 55 | + |
| 56 | + |
| 57 | +# ============================================================================ |
| 58 | +# merge_with Tests |
| 59 | +# ============================================================================ |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.unit |
| 63 | +class TestMergeWith: |
| 64 | + """Test suite for merge_with method.""" |
| 65 | + |
| 66 | + def test_merge_overwrites_enabled(self): |
| 67 | + """Test that merge_with overwrites enabled when other is True.""" |
| 68 | + # Given |
| 69 | + base = XmlPromptConfig(enabled=False) |
| 70 | + other = XmlPromptConfig(enabled=True) |
| 71 | + |
| 72 | + # When |
| 73 | + merged = base.merge_with(other) |
| 74 | + |
| 75 | + # Then |
| 76 | + assert merged.enabled is True |
| 77 | + |
| 78 | + def test_merge_preserves_base_when_other_false(self): |
| 79 | + """Test that merge preserves base enabled when other is False.""" |
| 80 | + # Given |
| 81 | + base = XmlPromptConfig(enabled=True) |
| 82 | + other = XmlPromptConfig(enabled=False) |
| 83 | + |
| 84 | + # When |
| 85 | + merged = base.merge_with(other) |
| 86 | + |
| 87 | + # Then |
| 88 | + assert merged.enabled is True |
| 89 | + |
| 90 | + def test_merge_combines_extra_dicts(self): |
| 91 | + """Test that merge combines extra dictionaries.""" |
| 92 | + # Given |
| 93 | + base = XmlPromptConfig(extra={"a": 1, "b": 2}) |
| 94 | + other = XmlPromptConfig(extra={"b": 3, "c": 4}) |
| 95 | + |
| 96 | + # When |
| 97 | + merged = base.merge_with(other) |
| 98 | + |
| 99 | + # Then |
| 100 | + assert merged.extra == {"a": 1, "b": 3, "c": 4} # other takes precedence |
| 101 | + |
| 102 | + def test_merge_template_name(self): |
| 103 | + """Test that merge uses other template_name if set.""" |
| 104 | + # Given |
| 105 | + base = XmlPromptConfig(template_name="base_template") |
| 106 | + other = XmlPromptConfig(template_name="other_template") |
| 107 | + |
| 108 | + # When |
| 109 | + merged = base.merge_with(other) |
| 110 | + |
| 111 | + # Then |
| 112 | + assert merged.template_name == "other_template" |
| 113 | + |
| 114 | + def test_merge_preserves_base_template_when_other_none(self): |
| 115 | + """Test that merge preserves base template when other is None.""" |
| 116 | + # Given |
| 117 | + base = XmlPromptConfig(template_name="base_template") |
| 118 | + other = XmlPromptConfig(template_name=None) |
| 119 | + |
| 120 | + # When |
| 121 | + merged = base.merge_with(other) |
| 122 | + |
| 123 | + # Then |
| 124 | + assert merged.template_name == "base_template" |
| 125 | + |
| 126 | + |
| 127 | +# ============================================================================ |
| 128 | +# from_dict Tests |
| 129 | +# ============================================================================ |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.unit |
| 133 | +class TestFromDict: |
| 134 | + """Test suite for from_dict class method.""" |
| 135 | + |
| 136 | + def test_from_dict_with_all_fields(self): |
| 137 | + """Test from_dict with all fields provided.""" |
| 138 | + # Given |
| 139 | + data = { |
| 140 | + "enabled": True, |
| 141 | + "schema_version": "2.0", |
| 142 | + "enforce_response_xml": True, |
| 143 | + "fallback_on_parse_error": False, |
| 144 | + "template_name": "test", |
| 145 | + "custom_template": "<xml/>", |
| 146 | + "extra": {"key": "val"} |
| 147 | + } |
| 148 | + |
| 149 | + # When |
| 150 | + config = XmlPromptConfig.from_dict(data) |
| 151 | + |
| 152 | + # Then |
| 153 | + assert config.enabled is True |
| 154 | + assert config.schema_version == "2.0" |
| 155 | + assert config.enforce_response_xml is True |
| 156 | + assert config.fallback_on_parse_error is False |
| 157 | + assert config.template_name == "test" |
| 158 | + assert config.custom_template == "<xml/>" |
| 159 | + assert config.extra == {"key": "val"} |
| 160 | + |
| 161 | + def test_from_dict_with_defaults(self): |
| 162 | + """Test from_dict uses defaults for missing fields.""" |
| 163 | + # Given |
| 164 | + data = {} |
| 165 | + |
| 166 | + # When |
| 167 | + config = XmlPromptConfig.from_dict(data) |
| 168 | + |
| 169 | + # Then |
| 170 | + assert config.enabled is False |
| 171 | + assert config.schema_version == "1.0" |
| 172 | + assert config.enforce_response_xml is False |
| 173 | + assert config.fallback_on_parse_error is True |
| 174 | + assert config.template_name is None |
| 175 | + assert config.custom_template is None |
| 176 | + assert config.extra == {} |
| 177 | + |
| 178 | + |
| 179 | +# ============================================================================ |
| 180 | +# to_dict Tests |
| 181 | +# ============================================================================ |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.unit |
| 185 | +class TestToDict: |
| 186 | + """Test suite for to_dict method.""" |
| 187 | + |
| 188 | + def test_to_dict_with_defaults(self): |
| 189 | + """Test to_dict with default values.""" |
| 190 | + # Given |
| 191 | + config = XmlPromptConfig() |
| 192 | + |
| 193 | + # When |
| 194 | + data = config.to_dict() |
| 195 | + |
| 196 | + # Then |
| 197 | + assert data == { |
| 198 | + "enabled": False, |
| 199 | + "schema_version": "1.0", |
| 200 | + "enforce_response_xml": False, |
| 201 | + "fallback_on_parse_error": True, |
| 202 | + "template_name": None, |
| 203 | + "custom_template": None, |
| 204 | + "extra": {} |
| 205 | + } |
| 206 | + |
| 207 | + def test_to_dict_with_custom_values(self): |
| 208 | + """Test to_dict with custom values.""" |
| 209 | + # Given |
| 210 | + config = XmlPromptConfig( |
| 211 | + enabled=True, |
| 212 | + schema_version="2.0", |
| 213 | + enforce_response_xml=True, |
| 214 | + fallback_on_parse_error=False, |
| 215 | + template_name="test", |
| 216 | + custom_template="<xml/>", |
| 217 | + extra={"key": "value"} |
| 218 | + ) |
| 219 | + |
| 220 | + # When |
| 221 | + data = config.to_dict() |
| 222 | + |
| 223 | + # Then |
| 224 | + assert data["enabled"] is True |
| 225 | + assert data["schema_version"] == "2.0" |
| 226 | + assert data["enforce_response_xml"] is True |
| 227 | + assert data["fallback_on_parse_error"] is False |
| 228 | + assert data["template_name"] == "test" |
| 229 | + assert data["custom_template"] == "<xml/>" |
| 230 | + assert data["extra"] == {"key": "value"} |
| 231 | + |
| 232 | + |
| 233 | +# ============================================================================ |
| 234 | +# Round-trip Tests |
| 235 | +# ============================================================================ |
| 236 | + |
| 237 | + |
| 238 | +@pytest.mark.unit |
| 239 | +class TestRoundTrip: |
| 240 | + """Test suite for round-trip serialization.""" |
| 241 | + |
| 242 | + def test_round_trip_from_dict_to_dict(self): |
| 243 | + """Test that from_dict and to_dict are inverses.""" |
| 244 | + # Given |
| 245 | + original_data = { |
| 246 | + "enabled": True, |
| 247 | + "schema_version": "2.0", |
| 248 | + "enforce_response_xml": True, |
| 249 | + "fallback_on_parse_error": False, |
| 250 | + "template_name": "test", |
| 251 | + "custom_template": "<xml/>", |
| 252 | + "extra": {"key": "value"} |
| 253 | + } |
| 254 | + |
| 255 | + # When |
| 256 | + config = XmlPromptConfig.from_dict(original_data) |
| 257 | + result_data = config.to_dict() |
| 258 | + |
| 259 | + # Then |
| 260 | + assert result_data == original_data |
0 commit comments