|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +"""Tests for KernelArguments merge optimization (Issue #2: Lazy dict copy).""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | + |
| 8 | +from semantic_kernel.functions.kernel_arguments import KernelArguments |
| 9 | + |
| 10 | + |
| 11 | +class TestKernelArgumentsMergeOptimization: |
| 12 | + """Test suite for KernelArguments merge operator optimization.""" |
| 13 | + |
| 14 | + def test_or_operator_no_execution_settings_copy(self): |
| 15 | + """Test that | operator doesn't copy when execution_settings is None or empty. |
| 16 | + |
| 17 | + This tests the optimization where dict copy is avoided when not needed. |
| 18 | + """ |
| 19 | + args = KernelArguments(a=1, b=2) |
| 20 | + args.execution_settings = None |
| 21 | + |
| 22 | + result = args | {"c": 3} |
| 23 | + |
| 24 | + # Result should have merged values |
| 25 | + assert result["a"] == 1 |
| 26 | + assert result["b"] == 2 |
| 27 | + assert result["c"] == 3 |
| 28 | + |
| 29 | + def test_or_operator_with_kernel_arguments_merge(self): |
| 30 | + """Test | operator with another KernelArguments with execution_settings.""" |
| 31 | + settings1 = {"model": "gpt-4", "temperature": 0.7} |
| 32 | + settings2 = {"temperature": 0.9, "max_tokens": 100} |
| 33 | + |
| 34 | + args1 = KernelArguments(a=1, settings=settings1) |
| 35 | + args2 = KernelArguments(b=2, settings=settings2) |
| 36 | + |
| 37 | + result = args1 | args2 |
| 38 | + |
| 39 | + # Check merged arguments |
| 40 | + assert result["a"] == 1 |
| 41 | + assert result["b"] == 2 |
| 42 | + |
| 43 | + # Check merged execution settings |
| 44 | + assert result.execution_settings["model"] == "gpt-4" |
| 45 | + assert result.execution_settings["temperature"] == 0.9 # args2 overwrites |
| 46 | + assert result.execution_settings["max_tokens"] == 100 |
| 47 | + |
| 48 | + def test_ror_operator_lazy_copy(self): |
| 49 | + """Test reverse | operator avoids copy when execution_settings is empty.""" |
| 50 | + args = KernelArguments(a=1, b=2) |
| 51 | + args.execution_settings = {} |
| 52 | + |
| 53 | + result = {"c": 3} | args |
| 54 | + |
| 55 | + # Result should have merged values |
| 56 | + assert result["a"] == 1 |
| 57 | + assert result["b"] == 2 |
| 58 | + assert result["c"] == 3 |
| 59 | + |
| 60 | + def test_ior_operator_lazy_copy(self): |
| 61 | + """Test in-place |= operator avoids copy when execution_settings exists.""" |
| 62 | + settings1 = {"model": "gpt-4", "temperature": 0.7} |
| 63 | + args1 = KernelArguments(a=1, settings=settings1) |
| 64 | + |
| 65 | + settings2 = {"temperature": 0.9} |
| 66 | + args2 = KernelArguments(b=2, settings=settings2) |
| 67 | + |
| 68 | + args1 |= args2 |
| 69 | + |
| 70 | + # Check merged in-place |
| 71 | + assert args1["a"] == 1 |
| 72 | + assert args1["b"] == 2 |
| 73 | + |
| 74 | + # Check in-place merge of settings |
| 75 | + assert args1.execution_settings["model"] == "gpt-4" |
| 76 | + assert args1.execution_settings["temperature"] == 0.9 |
| 77 | + |
| 78 | + def test_ior_operator_creates_copy_when_needed(self): |
| 79 | + """Test that |= creates new dict only when target has no execution_settings.""" |
| 80 | + args1 = KernelArguments(a=1) |
| 81 | + args1.execution_settings = None |
| 82 | + |
| 83 | + settings2 = {"model": "gpt-4"} |
| 84 | + args2 = KernelArguments(b=2, settings=settings2) |
| 85 | + |
| 86 | + args1 |= args2 |
| 87 | + |
| 88 | + # Should have new execution_settings dict |
| 89 | + assert args1.execution_settings is not None |
| 90 | + assert args1.execution_settings["model"] == "gpt-4" |
| 91 | + # Should not be the same reference (it's a copy) |
| 92 | + assert args1.execution_settings is not args2.execution_settings |
| 93 | + |
| 94 | + def test_or_operator_preserves_original_settings(self): |
| 95 | + """Test that | operator doesn't mutate original execution_settings.""" |
| 96 | + original_settings = {"model": "gpt-4", "temperature": 0.7} |
| 97 | + args1 = KernelArguments(a=1, settings=original_settings.copy()) |
| 98 | + args2 = KernelArguments(b=2, settings={"temperature": 0.9}) |
| 99 | + |
| 100 | + result = args1 | args2 |
| 101 | + |
| 102 | + # Original args1 settings should be unchanged |
| 103 | + assert args1.execution_settings["temperature"] == 0.7 |
| 104 | + # Result should have merged settings |
| 105 | + assert result.execution_settings["temperature"] == 0.9 |
| 106 | + |
| 107 | + def test_ior_operator_merges_into_existing_dict(self): |
| 108 | + """Test that |= merges into existing settings dict when present.""" |
| 109 | + settings1 = {"model": "gpt-4", "temperature": 0.7} |
| 110 | + args1 = KernelArguments(a=1, settings=settings1) |
| 111 | + original_dict = args1.execution_settings |
| 112 | + |
| 113 | + settings2 = {"temperature": 0.9, "max_tokens": 100} |
| 114 | + args2 = KernelArguments(b=2, settings=settings2) |
| 115 | + |
| 116 | + args1 |= args2 |
| 117 | + |
| 118 | + # Should have reused and updated the original dict |
| 119 | + assert args1.execution_settings is original_dict |
| 120 | + assert args1.execution_settings["temperature"] == 0.9 |
| 121 | + assert args1.execution_settings["max_tokens"] == 100 |
0 commit comments