chore: import upstream snapshot with attribution
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,78 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
from pytest import fixture, mark, raises
|
||||
|
||||
from semantic_kernel import Kernel
|
||||
from semantic_kernel.exceptions.filter_exceptions import FilterManagementException
|
||||
|
||||
|
||||
@fixture
|
||||
def custom_filter():
|
||||
async def custom_filter(context, next):
|
||||
await next(context)
|
||||
|
||||
return custom_filter
|
||||
|
||||
|
||||
@mark.parametrize(
|
||||
"filter_type, filter_attr",
|
||||
[("function_invocation", "function_invocation_filters"), ("prompt_rendering", "prompt_rendering_filters")],
|
||||
)
|
||||
@mark.usefixtures("custom_filter")
|
||||
class TestKernelFilterExtension:
|
||||
def test_add_filter(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
def test_add_multiple_filters(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
custom_filter2 = custom_filter
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
kernel.add_filter(filter_type, custom_filter2)
|
||||
assert len(getattr(kernel, filter_attr)) == 2
|
||||
|
||||
def test_filter_decorator(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.filter(filter_type)(custom_filter)
|
||||
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
def test_remove_filter(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
kernel.remove_filter(filter_id=id(custom_filter))
|
||||
assert len(getattr(kernel, filter_attr)) == 0
|
||||
|
||||
def test_remove_filter_with_type(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
kernel.remove_filter(filter_type=filter_type, filter_id=id(custom_filter))
|
||||
assert len(getattr(kernel, filter_attr)) == 0
|
||||
|
||||
def test_remove_filter_by_position(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
kernel.remove_filter(filter_type, position=0)
|
||||
assert len(getattr(kernel, filter_attr)) == 0
|
||||
|
||||
def test_remove_filter_without_type(self, kernel: Kernel, custom_filter, filter_type: str, filter_attr: str):
|
||||
kernel.add_filter(filter_type, custom_filter)
|
||||
assert len(getattr(kernel, filter_attr)) == 1
|
||||
|
||||
kernel.remove_filter(filter_id=id(custom_filter))
|
||||
assert len(getattr(kernel, filter_attr)) == 0
|
||||
|
||||
|
||||
def test_unknown_filter_type(kernel: Kernel, custom_filter):
|
||||
with raises(FilterManagementException):
|
||||
kernel.add_filter("unknown", custom_filter)
|
||||
|
||||
|
||||
def test_remove_filter_fail(kernel: Kernel):
|
||||
with raises(FilterManagementException):
|
||||
kernel.remove_filter()
|
||||
|
||||
|
||||
def test_remove_filter_fail_position(kernel: Kernel):
|
||||
with raises(FilterManagementException):
|
||||
kernel.remove_filter(position=0)
|
||||
@@ -0,0 +1,32 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from semantic_kernel import Kernel
|
||||
from semantic_kernel.exceptions.function_exceptions import FunctionInitializationError
|
||||
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
||||
from semantic_kernel.functions.kernel_function import KernelFunction
|
||||
|
||||
|
||||
async def test_register_valid_native_function(kernel: Kernel, decorated_native_function: Callable):
|
||||
kernel.add_function(plugin_name="TestPlugin", function=decorated_native_function)
|
||||
registered_func = kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus")
|
||||
|
||||
assert isinstance(registered_func, KernelFunction)
|
||||
assert kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus") == registered_func
|
||||
func_result = await registered_func.invoke(kernel, KernelArguments(arg1="testtest"))
|
||||
assert str(func_result) == "test"
|
||||
|
||||
|
||||
def test_register_undecorated_native_function(kernel: Kernel, not_decorated_native_function: Callable):
|
||||
with pytest.raises(FunctionInitializationError):
|
||||
kernel.add_function("TestPlugin", not_decorated_native_function)
|
||||
|
||||
|
||||
def test_register_with_none_plugin_name(kernel: Kernel, decorated_native_function: Callable):
|
||||
with pytest.raises(ValidationError):
|
||||
kernel.add_function(function=decorated_native_function, plugin_name=None)
|
||||
Reference in New Issue
Block a user