|
| 1 | +# Copyright (c) 2025, Salesforce, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +import tempfile |
| 18 | + |
| 19 | +import yaml |
| 20 | + |
| 21 | +from datacustomcode.llm_gateway.default import DefaultLLMGateway |
| 22 | +from datacustomcode.llm_gateway_config import ( |
| 23 | + LLMGatewayConfig, |
| 24 | + LLMGatewayObjectConfig, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class TestLLMGatewayConfigUpdate: |
| 29 | + def test_update_replaces_config_without_force(self): |
| 30 | + config1 = LLMGatewayConfig( |
| 31 | + llm_gateway_config=LLMGatewayObjectConfig( |
| 32 | + type_config_name="OldImplementation", options={"old": True} |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + config2 = LLMGatewayConfig( |
| 37 | + llm_gateway_config=LLMGatewayObjectConfig( |
| 38 | + type_config_name="NewImplementation", options={"new": True} |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + config1.update(config2) |
| 43 | + |
| 44 | + assert config1.llm_gateway_config.type_config_name == "NewImplementation" |
| 45 | + assert config1.llm_gateway_config.options == {"new": True} |
| 46 | + |
| 47 | + def test_update_respects_force_flag(self): |
| 48 | + config1 = LLMGatewayConfig( |
| 49 | + llm_gateway_config=LLMGatewayObjectConfig( |
| 50 | + type_config_name="ForcedImplementation", |
| 51 | + options={"forced": True}, |
| 52 | + force=True, |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + config2 = LLMGatewayConfig( |
| 57 | + llm_gateway_config=LLMGatewayObjectConfig( |
| 58 | + type_config_name="NewImplementation", options={"new": True} |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + config1.update(config2) |
| 63 | + |
| 64 | + assert ( |
| 65 | + config1.llm_gateway_config.type_config_name == "ForcedImplementation" |
| 66 | + ) |
| 67 | + assert config1.llm_gateway_config.options == {"forced": True} |
| 68 | + assert config1.llm_gateway_config.force is True |
| 69 | + |
| 70 | + |
| 71 | +class TestLLMGatewayConfigLoad: |
| 72 | + def test_load_from_yaml_file(self): |
| 73 | + config_data = { |
| 74 | + "llm_gateway_config": {"type_config_name": "DefaultLLMGateway"} |
| 75 | + } |
| 76 | + |
| 77 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 78 | + yaml.dump(config_data, f) |
| 79 | + temp_file = f.name |
| 80 | + |
| 81 | + try: |
| 82 | + config = LLMGatewayConfig() |
| 83 | + config.load(temp_file) |
| 84 | + |
| 85 | + assert config.llm_gateway_config is not None |
| 86 | + assert ( |
| 87 | + config.llm_gateway_config.type_config_name == "DefaultLLMGateway" |
| 88 | + ) |
| 89 | + llm_gateway = config.llm_gateway_config.to_object() |
| 90 | + assert llm_gateway is not None |
| 91 | + assert isinstance(llm_gateway, DefaultLLMGateway) |
| 92 | + finally: |
| 93 | + os.unlink(temp_file) |
0 commit comments