-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.Net: Add unit tests for Text Search AOT enhancements #10143
Open
markwallace-microsoft
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
markwallace-microsoft:users/markwallace/issue_9342
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...t/src/SemanticKernel.AotTests/JsonSerializerContexts/CustomResultJsonSerializerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text.Json.Serialization; | ||
using Microsoft.SemanticKernel.Data; | ||
using SemanticKernel.AotTests.Plugins; | ||
|
||
namespace SemanticKernel.AotTests.JsonSerializerContexts; | ||
|
||
[JsonSerializable(typeof(CustomResult))] | ||
[JsonSerializable(typeof(int))] | ||
[JsonSerializable(typeof(KernelSearchResults<string>))] | ||
[JsonSerializable(typeof(KernelSearchResults<TextSearchResult>))] | ||
[JsonSerializable(typeof(KernelSearchResults<object>))] | ||
internal sealed partial class CustomResultJsonSerializerContext : JsonSerializerContext | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
dotnet/src/SemanticKernel.AotTests/Plugins/CustomResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace SemanticKernel.AotTests.Plugins; | ||
internal sealed class CustomResult | ||
{ | ||
public string Value { get; set; } | ||
|
||
public CustomResult(string value) | ||
{ | ||
this.Value = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
dotnet/src/SemanticKernel.AotTests/UnitTests/Search/MockTextSearch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel.Data; | ||
|
||
namespace SemanticKernel.AotTests.UnitTests.Search; | ||
|
||
internal sealed class MockTextSearch : ITextSearch | ||
{ | ||
private readonly KernelSearchResults<object>? _objectResults; | ||
private readonly KernelSearchResults<TextSearchResult>? _textSearchResults; | ||
private readonly KernelSearchResults<string>? _stringResults; | ||
|
||
public MockTextSearch(KernelSearchResults<object>? objectResults) | ||
{ | ||
this._objectResults = objectResults; | ||
} | ||
|
||
public MockTextSearch(KernelSearchResults<TextSearchResult>? textSearchResults) | ||
{ | ||
this._textSearchResults = textSearchResults; | ||
} | ||
|
||
public MockTextSearch(KernelSearchResults<string>? stringResults) | ||
{ | ||
this._stringResults = stringResults; | ||
} | ||
|
||
public Task<KernelSearchResults<object>> GetSearchResultsAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(this._objectResults!); | ||
} | ||
|
||
public Task<KernelSearchResults<TextSearchResult>> GetTextSearchResultsAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(this._textSearchResults!); | ||
} | ||
|
||
public Task<KernelSearchResults<string>> SearchAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(this._stringResults!); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
dotnet/src/SemanticKernel.AotTests/UnitTests/Search/TextSearchExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,94 @@ | ||||||||||||||||||||||||||
// Copyright (c) Microsoft. All rights reserved. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
using System.Text.Json; | ||||||||||||||||||||||||||
using Microsoft.SemanticKernel; | ||||||||||||||||||||||||||
using Microsoft.SemanticKernel.Data; | ||||||||||||||||||||||||||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||||||||||||||||||||||||
using SemanticKernel.AotTests.JsonSerializerContexts; | ||||||||||||||||||||||||||
using SemanticKernel.AotTests.Plugins; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
namespace SemanticKernel.AotTests.UnitTests.Search; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
internal sealed class TextSearchExtensionsTests | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
TypeInfoResolverChain = { CustomResultJsonSerializerContext.Default } | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public static async Task CreateWithSearch() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Arrange | ||||||||||||||||||||||||||
var testData = new List<string> { "test-value" }; | ||||||||||||||||||||||||||
KernelSearchResults<string> results = new(testData.ToAsyncEnumerable()); | ||||||||||||||||||||||||||
ITextSearch textSearch = new MockTextSearch(results); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Act | ||||||||||||||||||||||||||
var plugin = textSearch.CreateWithSearch("SearchPlugin", s_jsonSerializerOptions); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Assert | ||||||||||||||||||||||||||
await AssertSearchFunctionSchemaAndInvocationResult<string>(plugin["Search"], testData[0]); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public static async Task CreateWithGetTextSearchResults() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Arrange | ||||||||||||||||||||||||||
var testData = new List<TextSearchResult> { new("test-value") }; | ||||||||||||||||||||||||||
KernelSearchResults<TextSearchResult> results = new(testData.ToAsyncEnumerable()); | ||||||||||||||||||||||||||
ITextSearch textSearch = new MockTextSearch(results); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Act | ||||||||||||||||||||||||||
var plugin = textSearch.CreateWithGetTextSearchResults("SearchPlugin", s_jsonSerializerOptions); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Assert | ||||||||||||||||||||||||||
await AssertSearchFunctionSchemaAndInvocationResult<TextSearchResult>(plugin["GetTextSearchResults"], testData[0]); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public static async Task CreateWithGetSearchResults() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Arrange | ||||||||||||||||||||||||||
var testData = new List<CustomResult> { new("test-value") }; | ||||||||||||||||||||||||||
KernelSearchResults<object> results = new(testData.ToAsyncEnumerable()); | ||||||||||||||||||||||||||
ITextSearch textSearch = new MockTextSearch(results); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Act | ||||||||||||||||||||||||||
var plugin = textSearch.CreateWithGetSearchResults("SearchPlugin", s_jsonSerializerOptions); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Assert | ||||||||||||||||||||||||||
await AssertSearchFunctionSchemaAndInvocationResult<object>(plugin["GetSearchResults"], testData[0]); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
#region assert | ||||||||||||||||||||||||||
internal static async Task AssertSearchFunctionSchemaAndInvocationResult<T>(KernelFunction function, T expectedResult) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Assert input parameter schema | ||||||||||||||||||||||||||
AssertSearchFunctionMetadata<T>(function.Metadata); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Assert the function result | ||||||||||||||||||||||||||
FunctionResult functionResult = await function.InvokeAsync(new(), new() { ["query"] = "Mock Query" }); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
var result = functionResult.GetValue<List<T>>()!; | ||||||||||||||||||||||||||
Assert.AreEqual(1, result.Count); | ||||||||||||||||||||||||||
Assert.AreEqual(expectedResult, result[0]); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
internal static void AssertSearchFunctionMetadata<T>(KernelFunctionMetadata metadata) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// Assert input parameter schema | ||||||||||||||||||||||||||
Assert.AreEqual(3, metadata.Parameters.Count); | ||||||||||||||||||||||||||
Assert.AreEqual("{\"description\":\"What to search for\",\"type\":\"string\"}", metadata.Parameters[0].Schema!.ToString()); | ||||||||||||||||||||||||||
Assert.AreEqual("{\"description\":\"Number of results (default value: 2)\",\"type\":\"integer\"}", metadata.Parameters[1].Schema!.ToString()); | ||||||||||||||||||||||||||
Assert.AreEqual("{\"description\":\"Number of results to skip (default value: 0)\",\"type\":\"integer\"}", metadata.Parameters[2].Schema!.ToString()); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Assert return type schema | ||||||||||||||||||||||||||
var type = typeof(T).Name; | ||||||||||||||||||||||||||
var expectedSchema = type switch | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
"String" => "{\"type\":\"object\",\"properties\":{\"TotalCount\":{\"type\":[\"integer\",\"null\"],\"default\":null},\"Metadata\":{\"type\":[\"object\",\"null\"],\"default\":null},\"Results\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"Results\"]}", | ||||||||||||||||||||||||||
"TextSearchResult" => "{\"type\":\"object\",\"properties\":{\"TotalCount\":{\"type\":[\"integer\",\"null\"],\"default\":null},\"Metadata\":{\"type\":[\"object\",\"null\"],\"default\":null},\"Results\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"Name\":{\"type\":[\"string\",\"null\"]},\"Link\":{\"type\":[\"string\",\"null\"]},\"Value\":{\"type\":\"string\"}},\"required\":[\"Value\"]}}},\"required\":[\"Results\"]}", | ||||||||||||||||||||||||||
_ => "{\"type\":\"object\",\"properties\":{\"TotalCount\":{\"type\":[\"integer\",\"null\"],\"default\":null},\"Metadata\":{\"type\":[\"object\",\"null\"],\"default\":null},\"Results\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"Name\":{\"type\":[\"string\",\"null\"]},\"Link\":{\"type\":[\"string\",\"null\"]},\"Value\":{\"type\":\"string\"}},\"required\":[\"Value\"]}}},\"required\":[\"Results\"]}", | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
Comment on lines
+85
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
Assert.AreEqual(expectedSchema, metadata.ReturnParameter.Schema!.ToString()); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
#endregion | ||||||||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: new line between namespace and the class declaration.