forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorStoreTextSearchTests.cs
More file actions
87 lines (70 loc) · 3.17 KB
/
VectorStoreTextSearchTests.cs
File metadata and controls
87 lines (70 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft. All rights reserved.
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SemanticKernel.AotTests.UnitTests.Search;
internal sealed class VectorStoreTextSearchTests
{
[RequiresDynamicCode("Calls Microsoft.SemanticKernel.Data.VectorStoreTextSearch<TRecord>.GetTextSearchResultsAsync(String, TextSearchOptions, CancellationToken)")]
public static async Task GetTextSearchResultsAsync()
{
// Arrange
var testData = new List<VectorSearchResult<DataModel>>
{
new(new DataModel { Key = "test-name", Text = "test-result", Link = "test-link" }, 0.5)
};
VectorStoreTextSearch<DataModel> textSearch = new(new MockVectorizableTextSearch<DataModel>(testData));
// Act
KernelSearchResults<TextSearchResult> searchResults = await textSearch.GetTextSearchResultsAsync("query");
List<TextSearchResult> results = [];
await foreach (TextSearchResult result in searchResults.Results)
{
results.Add(result);
}
// Assert
Assert.AreEqual(1, results.Count);
Assert.AreEqual("test-name", results[0].Name);
Assert.AreEqual("test-result", results[0].Value);
Assert.AreEqual("test-link", results[0].Link);
}
[RequiresDynamicCode("Calls Microsoft.SemanticKernel.Data.VectorStoreTextSearch<TRecord>.GetTextSearchResultsAsync(String, TextSearchOptions, CancellationToken)")]
public static async Task AddVectorStoreTextSearch()
{
// Arrange
var testData = new List<VectorSearchResult<DataModel>>
{
new(new DataModel { Key = "test-name", Text = "test-result", Link = "test-link" }, 0.5)
};
var vectorizableTextSearch = new MockVectorizableTextSearch<DataModel>(testData);
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IVectorSearchable<DataModel>>(vectorizableTextSearch);
// Act
serviceCollection.AddVectorStoreTextSearch<DataModel>();
var textSearch = serviceCollection.BuildServiceProvider().GetService<VectorStoreTextSearch<DataModel>>();
Assert.IsNotNull(textSearch);
// Assert
KernelSearchResults<TextSearchResult> searchResults = await textSearch.GetTextSearchResultsAsync("query");
List<TextSearchResult> results = [];
await foreach (TextSearchResult result in searchResults.Results)
{
results.Add(result);
}
// Assert
Assert.AreEqual(1, results.Count);
Assert.AreEqual("test-name", results[0].Name);
Assert.AreEqual("test-result", results[0].Value);
Assert.AreEqual("test-link", results[0].Link);
}
private sealed class DataModel
{
[TextSearchResultName]
public required string Key { get; init; }
[TextSearchResultValue]
public required string Text { get; init; }
[TextSearchResultLinkAttribute]
public required string Link { get; init; }
}
}