forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryDbFunctionalTest.cs
More file actions
71 lines (59 loc) · 2.98 KB
/
MemoryDbFunctionalTest.cs
File metadata and controls
71 lines (59 loc) · 2.98 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
// Copyright (c) Microsoft. All rights reserved.
using Elastic.Clients.Elasticsearch;
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.AI.OpenAI;
using Microsoft.KernelMemory.MemoryDb.Elasticsearch;
using Microsoft.KernelMemory.MemoryDb.Elasticsearch.Internals;
using Microsoft.KernelMemory.MemoryStorage;
using Microsoft.KM.TestHelpers;
using Xunit;
namespace Microsoft.Elasticsearch.FunctionalTests.Additional;
/// <summary>
/// A simple base class for Elasticsearch tests.
/// It ensures that all indices created by the test methods of the derived class are
/// deleted before and after the tests. This ensures that Elasticsearch is left in a clean state
/// or that subsequent tests don't fail because of left-over indices.
/// </summary>
#pragma warning disable CA1033
public abstract class MemoryDbFunctionalTest : BaseFunctionalTestCase, IAsyncLifetime
{
protected MemoryDbFunctionalTest(IConfiguration cfg, ITestOutputHelper output)
: base(cfg, output)
{
this.Output = output ?? throw new ArgumentNullException(nameof(output));
#pragma warning disable KMEXP01 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
this.TextEmbeddingGenerator = new OpenAITextEmbeddingGenerator(
config: base.OpenAiConfig,
textTokenizer: default,
loggerFactory: default);
#pragma warning restore KMEXP01 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
this.Client = new ElasticsearchClient(base.ElasticsearchConfig.ToElasticsearchClientSettings());
this.MemoryDb = new ElasticsearchMemory(base.ElasticsearchConfig, this.TextEmbeddingGenerator);
}
public ITestOutputHelper Output { get; }
public ElasticsearchClient Client { get; }
public IMemoryDb MemoryDb { get; }
public ITextEmbeddingGenerator TextEmbeddingGenerator { get; }
async ValueTask IAsyncLifetime.InitializeAsync()
{
// Within a single test class, the tests are executed sequentially by default so
// there is no chance for a method to finish and delete indices of other methods before the next
// method starts executing.
var indicesFound = await this.Client.DeleteIndicesOfTestAsync(this.GetType(), base.ElasticsearchConfig).ConfigureAwait(false);
if (indicesFound.Any())
{
this.Output.WriteLine($"Deleted left-over test indices: {string.Join(", ", indicesFound)}");
this.Output.WriteLine("");
}
}
public async ValueTask DisposeAsync()
{
var indicesFound = await this.Client.DeleteIndicesOfTestAsync(this.GetType(), base.ElasticsearchConfig).ConfigureAwait(false);
if (indicesFound.Any())
{
this.Output.WriteLine($"Deleted test indices: {string.Join(", ", indicesFound)}");
this.Output.WriteLine("");
}
GC.SuppressFinalize(this);
}
}