|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 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 click |
| 17 | +import pytest |
| 18 | + |
| 19 | +from aiq.cli.cli_utils import config_override |
| 20 | +from aiq.data_models.function import FunctionBaseConfig |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(name="base_config") |
| 24 | +def fixture_base_config() -> dict: |
| 25 | + return {"a": {"b": 1, "c": 2}, "d": 3, "bool_val": True} |
| 26 | + |
| 27 | + |
| 28 | +def test_layered_config_set_override(base_config: dict): |
| 29 | + layered_config = config_override.LayeredConfig(base_config) |
| 30 | + |
| 31 | + # Override a value that already exists |
| 32 | + layered_config.set_override("a.b", '10') |
| 33 | + |
| 34 | + # Override a value that doesn't exist |
| 35 | + layered_config.set_override("a.e", '20') |
| 36 | + |
| 37 | + # override a nested value |
| 38 | + layered_config.set_override("f.g", '30') |
| 39 | + |
| 40 | + layered_config.set_override("bool_val", '\tfALse ') |
| 41 | + |
| 42 | + assert layered_config.get_effective_config() == { |
| 43 | + "a": { |
| 44 | + "b": 10, "c": 2, "e": '20' |
| 45 | + }, "d": 3, "f": { |
| 46 | + "g": '30' |
| 47 | + }, "bool_val": False |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def test_layered_config_set_override_error(base_config: dict): |
| 52 | + layered_config = config_override.LayeredConfig(base_config) |
| 53 | + |
| 54 | + # Attempt to set an override with an invalid path |
| 55 | + with pytest.raises(click.BadParameter, match="Cannot navigate through non-dictionary value at 'a.b'"): |
| 56 | + layered_config.set_override("a.b.c", '10') |
| 57 | + |
| 58 | + # Attempt to set an override a boolean value with an invalid string |
| 59 | + with pytest.raises(click.BadParameter, match="Boolean value must be 'true' or 'false', got 'not_a_bool'"): |
| 60 | + layered_config.set_override("bool_val", 'not_a_bool') |
| 61 | + |
| 62 | + # Attempt to set a value with a type that doesn't match the original |
| 63 | + with pytest.raises(click.BadParameter, match=r"Type mismatch for 'a\.b'"): |
| 64 | + layered_config.set_override("a.b", 'not_a_number') |
| 65 | + |
| 66 | + |
| 67 | +def test_layered_config_constructor_error(base_config: dict): |
| 68 | + # Attempt to set an override with an invalid base config |
| 69 | + with pytest.raises(ValueError, match="Base config must be a dictionary"): |
| 70 | + config_override.LayeredConfig("invalid_base_config") |
| 71 | + |
| 72 | + |
| 73 | +def test_config_casting(): |
| 74 | + """ |
| 75 | + Test to verify that pydantic's casting works as expected in situations where LayeredConfig |
| 76 | + is unable to determine the type of the value being set. |
| 77 | + """ |
| 78 | + |
| 79 | + class TestConfig(FunctionBaseConfig, name="TestConfig"): |
| 80 | + a: bool |
| 81 | + b: int |
| 82 | + c: float |
| 83 | + |
| 84 | + layered_config = config_override.LayeredConfig({}) |
| 85 | + for (field, value) in ( |
| 86 | + ("a", "false"), |
| 87 | + ("b", "45"), |
| 88 | + ("c", "5.6"), |
| 89 | + ): |
| 90 | + layered_config.set_override(field, value) |
| 91 | + |
| 92 | + effective_config = layered_config.get_effective_config() |
| 93 | + config = TestConfig(**effective_config) |
| 94 | + assert config.a is False |
| 95 | + assert config.b == 45 |
| 96 | + assert config.c == 5.6 |
0 commit comments