|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from semantic_kernel import Kernel |
| 7 | +from semantic_kernel.kernel_exception import KernelException |
| 8 | +from semantic_kernel.orchestration.sk_function_base import SKFunctionBase |
| 9 | +from semantic_kernel.skill_definition.sk_function_decorator import sk_function |
| 10 | +from semantic_kernel.skill_definition.skill_collection import SkillCollection |
| 11 | + |
| 12 | + |
| 13 | +def not_decorated_native_function(arg1: str) -> str: |
| 14 | + return "test" |
| 15 | + |
| 16 | + |
| 17 | +@sk_function(name="getLightStatus") |
| 18 | +def decorated_native_function(arg1: str) -> str: |
| 19 | + return "test" |
| 20 | + |
| 21 | + |
| 22 | +def test_register_valid_native_function(): |
| 23 | + kernel = Kernel() |
| 24 | + |
| 25 | + registered_func = kernel.register_native_function( |
| 26 | + "TestSkill", decorated_native_function |
| 27 | + ) |
| 28 | + |
| 29 | + assert isinstance(registered_func, SKFunctionBase) |
| 30 | + assert ( |
| 31 | + kernel.skills.get_native_function("TestSkill", "getLightStatus") |
| 32 | + == registered_func |
| 33 | + ) |
| 34 | + assert registered_func.invoke("testtest").result == "test" |
| 35 | + |
| 36 | + |
| 37 | +def test_register_undecorated_native_function(): |
| 38 | + kernel = Kernel() |
| 39 | + |
| 40 | + with pytest.raises(KernelException): |
| 41 | + kernel.register_native_function("TestSkill", not_decorated_native_function) |
| 42 | + |
| 43 | + |
| 44 | +def test_register_with_none_skill_name(): |
| 45 | + kernel = Kernel() |
| 46 | + |
| 47 | + registered_func = kernel.register_native_function(None, decorated_native_function) |
| 48 | + assert registered_func.skill_name == SkillCollection.GLOBAL_SKILL |
| 49 | + |
| 50 | + |
| 51 | +def test_register_overloaded_native_function(): |
| 52 | + kernel = Kernel() |
| 53 | + |
| 54 | + kernel.register_native_function("TestSkill", decorated_native_function) |
| 55 | + |
| 56 | + with pytest.raises(KernelException): |
| 57 | + kernel.register_native_function("TestSkill", decorated_native_function) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + pytest.main([__file__]) |
0 commit comments