Skip to content

Commit 186fda2

Browse files
committed
port existing tests to Testcontainers.MsSql and re-enable them
1 parent 160e08e commit 186fda2

4 files changed

Lines changed: 58 additions & 27 deletions

File tree

‎dotnet/Directory.Packages.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<PackageVersion Include="NRedisStack" Version="0.12.0" />
121121
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1" />
122122
<PackageVersion Include="Testcontainers.Milvus" Version="4.0.0" />
123+
<PackageVersion Include="Testcontainers.MsSql" Version="4.2.0" />
123124
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2" />
124125
<PackageVersion Include="Qdrant.Client" Version="1.12.0" />
125126
<!-- Symbols -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Testcontainers.MsSql;
4+
using Xunit;
5+
6+
namespace SqlServerIntegrationTests;
7+
8+
public sealed class SqlServerContainerFixture : IAsyncLifetime
9+
{
10+
private MsSqlContainer? Container { get; set; }
11+
12+
public string GetConnectionString() => this.Container?.GetConnectionString() ??
13+
throw new InvalidOperationException("The test container was not initialized.");
14+
15+
public async Task DisposeAsync()
16+
{
17+
if (this.Container is not null)
18+
{
19+
await this.Container.DisposeAsync();
20+
}
21+
}
22+
23+
public async Task InitializeAsync() => this.Container = await CreateContainerAsync();
24+
25+
private static async Task<MsSqlContainer> CreateContainerAsync()
26+
{
27+
var container = new MsSqlBuilder()
28+
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
29+
.Build();
30+
31+
await container.StartAsync();
32+
33+
return container;
34+
}
35+
}

‎dotnet/src/VectorDataIntegrationTests/SqlServerIntegrationTests/SqlServerIntegrationTests.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
1010

11-
<NoWarn>$(NoWarn);CA2007,SKEXP0001,SKEXP0020,VSTHRD111</NoWarn>
11+
<NoWarn>$(NoWarn);CS1591,CA1861,CA2007,SKEXP0001,SKEXP0020,VSTHRD111</NoWarn>
1212
</PropertyGroup>
1313

1414
<ItemGroup>
@@ -27,6 +27,7 @@
2727
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
2828
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
2929
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
30+
<PackageReference Include="Testcontainers.MsSql" />
3031
</ItemGroup>
3132

3233
<ItemGroup>

‎dotnet/src/VectorDataIntegrationTests/SqlServerIntegrationTests/SqlServerMemoryStoreTests.cs‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73
using Microsoft.Data.SqlClient;
84
using Microsoft.Extensions.Configuration;
95
using Microsoft.SemanticKernel.Connectors.SqlServer;
106
using Microsoft.SemanticKernel.Memory;
7+
using SqlServerIntegrationTests;
118
using Xunit;
129

1310
namespace SemanticKernel.IntegrationTests.Connectors.SqlServer;
1411

1512
/// <summary>
1613
/// Unit tests for <see cref="SqlServerMemoryStore"/> class.
1714
/// </summary>
18-
public class SqlServerMemoryStoreTests : IAsyncLifetime
15+
public class SqlServerMemoryStoreTests(SqlServerContainerFixture fixture) : IClassFixture<SqlServerContainerFixture>, IAsyncLifetime
1916
{
20-
private const string? SkipReason = "Configure SQL Server or Azure SQL connection string and then set this to 'null'.";
21-
//private const string? SkipReason = null;
2217
private const string SchemaName = "sk_it";
2318
private const string DefaultCollectionName = "test";
2419
private const int TestEmbeddingDimensionsCount = 5;
@@ -27,16 +22,13 @@ public class SqlServerMemoryStoreTests : IAsyncLifetime
2722

2823
private SqlServerMemoryStore Store { get; set; } = null!;
2924

25+
private SqlServerContainerFixture Fixture { get; } = fixture;
26+
3027
public async Task InitializeAsync()
3128
{
32-
var configuration = new ConfigurationBuilder()
33-
.AddJsonFile(path: "testsettings.json", optional: false, reloadOnChange: true)
34-
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
35-
.AddEnvironmentVariables()
36-
.AddUserSecrets<SqlServerMemoryStore>()
37-
.Build();
29+
await this.Fixture.InitializeAsync();
3830

39-
var connectionString = configuration["SqlServer:ConnectionString"];
31+
string connectionString = this.Fixture.GetConnectionString();
4032

4133
if (string.IsNullOrWhiteSpace(connectionString))
4234
{
@@ -54,9 +46,11 @@ public async Task InitializeAsync()
5446
public async Task DisposeAsync()
5547
{
5648
await this.CleanupDatabaseAsync();
49+
50+
await this.Fixture.DisposeAsync();
5751
}
5852

59-
[Fact(Skip = SkipReason)]
53+
[Fact]
6054
public async Task CreateCollectionAsync()
6155
{
6256
Assert.False(await this.Store.DoesCollectionExistAsync(DefaultCollectionName));
@@ -65,15 +59,15 @@ public async Task CreateCollectionAsync()
6559
Assert.True(await this.Store.DoesCollectionExistAsync(DefaultCollectionName));
6660
}
6761

68-
[Fact(Skip = SkipReason)]
62+
[Fact]
6963
public async Task DropCollectionAsync()
7064
{
7165
await this.Store.CreateCollectionAsync(DefaultCollectionName);
7266
await this.Store.DeleteCollectionAsync(DefaultCollectionName);
7367
Assert.False(await this.Store.DoesCollectionExistAsync(DefaultCollectionName));
7468
}
7569

76-
[Fact(Skip = SkipReason)]
70+
[Fact]
7771
public async Task GetCollectionsAsync()
7872
{
7973
await this.Store.CreateCollectionAsync("collection1");
@@ -84,7 +78,7 @@ public async Task GetCollectionsAsync()
8478
Assert.Contains("collection2", collections);
8579
}
8680

87-
[Fact(Skip = SkipReason)]
81+
[Fact]
8882
public async Task UpsertAsync()
8983
{
9084
await this.Store.CreateCollectionAsync(DefaultCollectionName);
@@ -104,7 +98,7 @@ public async Task UpsertAsync()
10498
Assert.Equal("Some id", id);
10599
}
106100

107-
[Theory(Skip = SkipReason)]
101+
[Theory]
108102
[InlineData(true)]
109103
[InlineData(false)]
110104
public async Task GetAsync(bool withEmbeddings)
@@ -128,7 +122,7 @@ public async Task GetAsync(bool withEmbeddings)
128122
record.Embedding.ToArray());
129123
}
130124

131-
[Fact(Skip = SkipReason)]
125+
[Fact]
132126
public async Task UpsertBatchAsync()
133127
{
134128
await this.Store.CreateCollectionAsync(DefaultCollectionName);
@@ -139,7 +133,7 @@ public async Task UpsertBatchAsync()
139133
id => Assert.Equal("Some other id", id));
140134
}
141135

142-
[Theory(Skip = SkipReason)]
136+
[Theory]
143137
[InlineData(true)]
144138
[InlineData(false)]
145139
public async Task GetBatchAsync(bool withEmbeddings)
@@ -180,7 +174,7 @@ public async Task GetBatchAsync(bool withEmbeddings)
180174
});
181175
}
182176

183-
[Fact(Skip = SkipReason)]
177+
[Fact]
184178
public async Task RemoveAsync()
185179
{
186180
await this.Store.CreateCollectionAsync(DefaultCollectionName);
@@ -191,7 +185,7 @@ public async Task RemoveAsync()
191185
Assert.Null(await this.Store.GetAsync(DefaultCollectionName, "Some id"));
192186
}
193187

194-
[Fact(Skip = SkipReason)]
188+
[Fact]
195189
public async Task RemoveBatchAsync()
196190
{
197191
await this.Store.CreateCollectionAsync(DefaultCollectionName);
@@ -204,7 +198,7 @@ public async Task RemoveBatchAsync()
204198
Assert.Null(await this.Store.GetAsync(DefaultCollectionName, "Some other id"));
205199
}
206200

207-
[Theory(Skip = SkipReason)]
201+
[Theory]
208202
[InlineData(true)]
209203
[InlineData(false)]
210204
public async Task GetNearestMatchesAsync(bool withEmbeddings)
@@ -248,7 +242,7 @@ public async Task GetNearestMatchesAsync(bool withEmbeddings)
248242
});
249243
}
250244

251-
[Fact(Skip = SkipReason)]
245+
[Fact]
252246
public async Task GetNearestMatchesWithMinRelevanceScoreAsync()
253247
{
254248
await this.Store.CreateCollectionAsync(DefaultCollectionName);
@@ -265,7 +259,7 @@ public async Task GetNearestMatchesWithMinRelevanceScoreAsync()
265259
Assert.DoesNotContain(firstId, results.Select(r => r.Record.Metadata.Id));
266260
}
267261

268-
[Theory(Skip = SkipReason)]
262+
[Theory]
269263
[InlineData(true)]
270264
[InlineData(false)]
271265
public async Task GetNearestMatchAsync(bool withEmbeddings)

0 commit comments

Comments
 (0)