Skip to content

Commit

Permalink
.Net: Unit test showing some strategies to unit test kernel functions (
Browse files Browse the repository at this point in the history
…#5068)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
markwallace-microsoft committed Feb 22, 2024
1 parent 9dfa674 commit fe29d1a
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Moq;
using Xunit;

namespace SemanticKernel.UnitTests.Functions;

public sealed class KernelFunctionUnitTestStrategies
{
[Fact]
public async Task CreateFromFunctionDelegateVoidAsync()
{
// Arrange
var kernel = new Kernel();
object expected = new();
object FunctionDelegate() => expected;
var function = KernelFunctionFactory.CreateFromMethod(FunctionDelegate, "MyFunction");

// Act
var result = await function.InvokeAsync(kernel);

// Assert
Assert.Equal(expected, result.GetValue<object>());
}

[Fact]
public async Task CreatePluginFromFunctionDelegateVoidAsync()
{
// Arrange
var kernel = new Kernel();
object expected = new();
object FunctionDelegate() => expected;
var function = KernelFunctionFactory.CreateFromMethod(FunctionDelegate, "MyFunction");
var plugin = KernelPluginFactory.CreateFromFunctions("MyPlugin", new[] { function });
kernel.Plugins.Add(plugin);

// Act
var result = await kernel.InvokeAsync("MyPlugin", "MyFunction");

// Assert
Assert.Equal(expected, result.GetValue<object>());
}

[Fact]
public async Task CreatePluginFromMockObjectAsync()
{
// Arrange
var kernel = new Kernel();
object expected = new();
Mock<MyPlugin> mockPlugin = new();
mockPlugin.Setup(m => m.MyFunction(It.IsAny<string>(), It.IsAny<int>())).Returns(expected);
var plugin = KernelPluginFactory.CreateFromObject(mockPlugin.Object, "MyPlugin");
kernel.Plugins.Add(plugin);

// Act
var arguments = new KernelArguments
{
{ "param1", "value1" },
{ "param2", 2 }
};
var result = await kernel.InvokeAsync("MyPlugin", "MyFunction", arguments);

// Assert
Assert.Equal(expected, result.GetValue<object>());
mockPlugin.Verify(mock => mock.MyFunction("value1", 2), Times.Once());
}

[Fact]
public async Task MockChatCompletionServiceForPromptAsync()
{
// Arrange
var mockService = new Mock<IChatCompletionService>();
var mockResult = mockService
.Setup(s => s.GetChatMessageContentsAsync(It.IsAny<ChatHistory>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<ChatMessageContent>() { new(AuthorRole.User, "Expected response") });
KernelBuilder builder = new();
builder.Services.AddTransient<IChatCompletionService>((sp) => mockService.Object);
Kernel kernel = builder.Build();
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Some prompt");

// Act
var result = await kernel.InvokeAsync(function);

// Assert
Assert.Equal("Expected response", result.GetValue<string>());
}

public class MyPlugin
{
[KernelFunction]
public virtual object MyFunction(string param1, int param2)
{
return new();
}
}
}

0 comments on commit fe29d1a

Please sign in to comment.