chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.ModelTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests.ModelTests;
public class SqliteBasicModelTests(SqliteBasicModelTests.Fixture fixture)
: BasicModelTests<string>(fixture), IClassFixture<SqliteBasicModelTests.Fixture>
{
public new class Fixture : BasicModelTests<string>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.ModelTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests.ModelTests;
public class SqliteMultiVectorModelTests(SqliteMultiVectorModelTests.Fixture fixture)
: MultiVectorModelTests<string>(fixture), IClassFixture<SqliteMultiVectorModelTests.Fixture>
{
public new class Fixture : MultiVectorModelTests<string>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.ModelTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests.ModelTests;
public class SqliteNoDataModelTests(SqliteNoDataModelTests.Fixture fixture)
: NoDataModelTests<string>(fixture), IClassFixture<SqliteNoDataModelTests.Fixture>
{
public new class Fixture : NoDataModelTests<string>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.ModelTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests.ModelTests;
public class SqliteNoVectorModelTests(SqliteNoVectorModelTests.Fixture fixture)
: NoVectorModelTests<string>(fixture), IClassFixture<SqliteNoVectorModelTests.Fixture>
{
public new class Fixture : NoVectorModelTests<string>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
using Xunit;
[assembly: SqliteVec.ConformanceTests.Support.SqliteVecRequired]
// Disable test parallelization in order to prevent from "database is locked" errors
[assembly: CollectionBehavior(DisableTestParallelization = true)]
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests;
using Xunit;
namespace SqliteVec.ConformanceTests;
public class SqliteCollectionManagementTests(SqliteFixture fixture)
: CollectionManagementTests<string>(fixture), IClassFixture<SqliteFixture>
{
}
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel.Connectors.SqliteVec;
using VectorData.ConformanceTests;
using Xunit;
namespace SqliteVec.ConformanceTests;
public class SqliteDependencyInjectionTests
: DependencyInjectionTests<SqliteVectorStore, SqliteCollection<string, DependencyInjectionTests<string>.Record>, string, DependencyInjectionTests<string>.Record>
{
protected const string ConnectionString = "Data Source=:memory:";
protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null)
=> configuration.AddInMemoryCollection(
[
new(CreateConfigKey("Sqlite", serviceKey, "ConnectionString"), ConnectionString),
]);
private static string ConnectionStringProvider(IServiceProvider sp)
=> sp.GetRequiredService<IConfiguration>().GetRequiredSection("Sqlite:ConnectionString").Value!;
private static string ConnectionStringProvider(IServiceProvider sp, object serviceKey)
=> sp.GetRequiredService<IConfiguration>().GetRequiredSection(CreateConfigKey("Sqlite", serviceKey, "ConnectionString")).Value!;
public override IEnumerable<Func<IServiceCollection, object?, string, ServiceLifetime, IServiceCollection>> CollectionDelegates
{
get
{
yield return (services, serviceKey, name, lifetime) => serviceKey is null
? services.AddSqliteCollection<string, Record>(
name, connectionString: ConnectionString, lifetime: lifetime)
: services.AddKeyedSqliteCollection<string, Record>(
serviceKey, name, connectionString: ConnectionString, lifetime: lifetime);
yield return (services, serviceKey, name, lifetime) => serviceKey is null
? services.AddSqliteCollection<string, Record>(
name, ConnectionStringProvider, lifetime: lifetime)
: services.AddKeyedSqliteCollection<string, Record>(
serviceKey, name, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime);
}
}
public override IEnumerable<Func<IServiceCollection, object?, ServiceLifetime, IServiceCollection>> StoreDelegates
{
get
{
yield return (services, serviceKey, lifetime) => serviceKey is null
? services.AddSqliteVectorStore(
ConnectionStringProvider, lifetime: lifetime)
: services.AddKeyedSqliteVectorStore(
serviceKey, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime);
}
}
[Fact]
public void ConnectionStringProviderCantBeNull()
{
IServiceCollection services = new ServiceCollection();
Assert.Throws<ArgumentNullException>(() => services.AddSqliteVectorStore(connectionStringProvider: null!));
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteVectorStore(serviceKey: "notNull", connectionStringProvider: null!));
Assert.Throws<ArgumentNullException>(() => services.AddSqliteCollection<string, Record>(name: "notNull", connectionStringProvider: null!));
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteCollection<string, Record>(serviceKey: "notNull", name: "notNull", connectionStringProvider: null!));
}
[Fact]
public void ConnectionStringCantBeNullOrEmpty()
{
IServiceCollection services = new ServiceCollection();
Assert.Throws<ArgumentNullException>(() => services.AddSqliteCollection<string, Record>(
name: "notNull", connectionString: null!));
Assert.Throws<ArgumentException>(() => services.AddSqliteCollection<string, Record>(
name: "notNull", connectionString: ""));
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteCollection<string, Record>(
serviceKey: "notNull", name: "notNull", connectionString: null!));
Assert.Throws<ArgumentException>(() => services.AddKeyedSqliteCollection<string, Record>(
serviceKey: "notNull", name: "notNull", connectionString: ""));
}
}
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests;
public class SqliteDistanceFunctionTests(SqliteDistanceFunctionTests.Fixture fixture)
: DistanceFunctionTests<int>(fixture), IClassFixture<SqliteDistanceFunctionTests.Fixture>
{
public override Task CosineSimilarity() => Assert.ThrowsAsync<NotSupportedException>(base.CosineSimilarity);
public override Task DotProductSimilarity() => Assert.ThrowsAsync<NotSupportedException>(base.DotProductSimilarity);
public override Task EuclideanSquaredDistance() => Assert.ThrowsAsync<NotSupportedException>(base.EuclideanSquaredDistance);
public override Task HammingDistance() => Assert.ThrowsAsync<NotSupportedException>(base.HammingDistance);
public override Task NegativeDotProductSimilarity() => Assert.ThrowsAsync<NotSupportedException>(base.NegativeDotProductSimilarity);
public new class Fixture() : DistanceFunctionTests<int>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.VectorData;
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests;
public class SqliteEmbeddingGenerationTests(SqliteEmbeddingGenerationTests.StringVectorFixture stringVectorFixture, SqliteEmbeddingGenerationTests.RomOfFloatVectorFixture romOfFloatVectorFixture)
: EmbeddingGenerationTests<string>(stringVectorFixture, romOfFloatVectorFixture), IClassFixture<SqliteEmbeddingGenerationTests.StringVectorFixture>, IClassFixture<SqliteEmbeddingGenerationTests.RomOfFloatVectorFixture>
{
public new class StringVectorFixture : EmbeddingGenerationTests<string>.StringVectorFixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
public override VectorStore CreateVectorStore(IEmbeddingGenerator? embeddingGenerator)
=> SqliteTestStore.Instance.GetVectorStore(new() { EmbeddingGenerator = embeddingGenerator });
public override Func<IServiceCollection, IServiceCollection>[] DependencyInjectionStoreRegistrationDelegates =>
[
services => services.AddSqliteVectorStore(_ => SqliteTestStore.Instance.ConnectionString)
];
public override Func<IServiceCollection, IServiceCollection>[] DependencyInjectionCollectionRegistrationDelegates =>
[
services => services.AddSqliteCollection<string, RecordWithAttributes>(this.CollectionName, SqliteTestStore.Instance.ConnectionString)
];
}
public new class RomOfFloatVectorFixture : EmbeddingGenerationTests<string>.RomOfFloatVectorFixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
public override VectorStore CreateVectorStore(IEmbeddingGenerator? embeddingGenerator)
=> SqliteTestStore.Instance.GetVectorStore(new() { EmbeddingGenerator = embeddingGenerator });
public override Func<IServiceCollection, IServiceCollection>[] DependencyInjectionStoreRegistrationDelegates =>
[
services => services.AddSqliteVectorStore(_ => SqliteTestStore.Instance.ConnectionString)
];
public override Func<IServiceCollection, IServiceCollection>[] DependencyInjectionCollectionRegistrationDelegates =>
[
services => services.AddSqliteCollection<string, RecordWithAttributes>(this.CollectionName, SqliteTestStore.Instance.ConnectionString),
services => services.AddSqliteCollection<string, RecordWithAttributes>(this.CollectionName, _ => SqliteTestStore.Instance.ConnectionString)
];
}
}
@@ -0,0 +1,86 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.VectorData;
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.Support;
using Xunit;
using Xunit.Sdk;
namespace SqliteVec.ConformanceTests;
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
public class SqliteFilterTests(SqliteFilterTests.Fixture fixture)
: FilterTests<long>(fixture), IClassFixture<SqliteFilterTests.Fixture>
{
public override async Task Not_over_Or()
{
// Test sends: WHERE (NOT (("Int" = 8) OR ("String" = 'foo')))
// There's a NULL string in the database, and relational null semantics in conjunction with negation makes the default implementation fail.
await Assert.ThrowsAsync<FailException>(base.Not_over_Or);
// Compensate by adding a null check:
await this.TestFilterAsync(
r => r.String != null && !(r.Int == 8 || r.String == "foo"),
r => r["String"] != null && !((int)r["Int"]! == 8 || r["String"] == "foo"));
}
public override async Task NotEqual_with_string()
{
// As above, null semantics + negation
await Assert.ThrowsAsync<FailException>(base.NotEqual_with_string);
await this.TestFilterAsync(
r => r.String != null && r.String != "foo",
r => r["String"] != null && r["String"] != "foo");
}
// Array fields not (currently) supported on SQLite (see #10343)
public override Task Contains_over_field_string_array()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Contains_over_field_string_array);
// List fields not (currently) supported on SQLite (see #10343)
public override Task Contains_over_field_string_List()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Contains_over_field_string_List);
// List fields not (currently) supported on SQLite (see #10343)
public override Task Contains_with_Enumerable_Contains()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Contains_with_Enumerable_Contains);
#if !NETFRAMEWORK
// List fields not (currently) supported on SQLite (see #10343)
public override Task Contains_with_MemoryExtensions_Contains()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Contains_with_MemoryExtensions_Contains);
#endif
#if NET10_0_OR_GREATER
public override Task Contains_with_MemoryExtensions_Contains_with_null_comparer()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Contains_with_MemoryExtensions_Contains_with_null_comparer);
#endif
// Any over array fields not (currently) supported on SQLite (see #10343)
public override Task Any_with_Contains_over_inline_string_array()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Any_with_Contains_over_inline_string_array);
public override Task Any_with_Contains_over_captured_string_array()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Any_with_Contains_over_captured_string_array);
public override Task Any_with_Contains_over_captured_string_list()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Any_with_Contains_over_captured_string_list);
public override Task Any_over_List_with_Contains_over_captured_string_array()
=> Assert.ThrowsAsync<InvalidOperationException>(base.Any_over_List_with_Contains_over_captured_string_array);
public new class Fixture : FilterTests<long>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
// Override to remove the string array property, which isn't (currently) supported on SQLite
public override VectorStoreCollectionDefinition CreateRecordDefinition()
=> new()
{
Properties = base.CreateRecordDefinition().Properties.Where(p => p.Type != typeof(string[]) && p.Type != typeof(List<string>)).ToList()
};
}
}
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.Support;
using Xunit;
namespace SqliteVec.ConformanceTests;
public class SqliteIndexKindTests(SqliteIndexKindTests.Fixture fixture)
: IndexKindTests<int>(fixture), IClassFixture<SqliteIndexKindTests.Fixture>
{
public new class Fixture() : IndexKindTests<int>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
using VectorData.ConformanceTests;
using VectorData.ConformanceTests.ModelTests;
namespace SqliteVec.ConformanceTests;
public class SqliteTestSuiteImplementationTests : TestSuiteImplementationTests
{
protected override ICollection<Type> IgnoredTestBases { get; } =
[
typeof(DynamicModelTests<>),
// Hybrid search not supported
typeof(HybridSearchTests<>)
];
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net10.0;net472</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<RootNamespace>Sqlite.ConformanceTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\VectorData\SqliteVec\SqliteVec.csproj" />
<ProjectReference Include="..\VectorData.ConformanceTests\VectorData.ConformanceTests.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
using VectorData.ConformanceTests.Support;
namespace SqliteVec.ConformanceTests.Support;
public class SqliteFixture : VectorStoreFixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Data.Sqlite;
namespace SqliteVec.ConformanceTests.Support;
internal static class SqliteTestEnvironment
{
private static readonly Lazy<bool> s_canUseSqlite = new(CanCreateConnectionAndLoadExtension);
internal static bool CanUseSqlite => s_canUseSqlite.Value;
private static bool CanCreateConnectionAndLoadExtension()
{
try
{
using var connection = new SqliteConnection("Data Source=:memory:;");
connection.Open();
connection.LoadVector();
}
catch (TypeInitializationException ex)
{
Console.WriteLine("Failed to load sqlite native dependency: " + ex.Message);
return false;
}
catch (SqliteException ex)
{
Console.WriteLine("Failed to load sqlite_vec extension: " + ex.Message);
return false;
}
return true;
}
}
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel.Connectors.SqliteVec;
using VectorData.ConformanceTests.Support;
namespace SqliteVec.ConformanceTests.Support;
internal sealed class SqliteTestStore : TestStore
{
private string? _databasePath;
private string? _connectionString;
public string ConnectionString => this._connectionString ?? throw new InvalidOperationException("Not initialized");
public static SqliteTestStore Instance { get; } = new();
public override string DefaultDistanceFunction => Microsoft.Extensions.VectorData.DistanceFunction.CosineDistance;
public SqliteVectorStore GetVectorStore(SqliteVectorStoreOptions options)
=> new(this.ConnectionString, options);
private SqliteTestStore()
{
}
protected override Task StartAsync()
{
this._databasePath = Path.GetTempFileName();
this._connectionString = $"Data Source={this._databasePath};Pooling=false";
this.DefaultVectorStore = new SqliteVectorStore(this._connectionString);
return Task.CompletedTask;
}
protected override Task StopAsync()
{
File.Delete(this._databasePath!);
this._databasePath = null;
return Task.CompletedTask;
}
}
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using VectorData.ConformanceTests.Xunit;
namespace SqliteVec.ConformanceTests.Support;
/// <summary>
/// Checks whether the sqlite_vec extension is properly installed, and skips the test(s) otherwise.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class SqliteVecRequiredAttribute : Attribute, ITestCondition
{
public ValueTask<bool> IsMetAsync() => new(SqliteTestEnvironment.CanUseSqlite);
public string Skip { get; set; } = "Some native Sqlite dependencies are missing.";
public string SkipReason
=> this.Skip;
}
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.Support;
using VectorData.ConformanceTests.TypeTests;
using Xunit;
namespace SqliteVec.ConformanceTests.TypeTests;
public class SqliteDataTypeTests(SqliteDataTypeTests.Fixture fixture)
: DataTypeTests<int, DataTypeTests<int>.DefaultRecord>(fixture), IClassFixture<SqliteDataTypeTests.Fixture>
{
public new class Fixture : DataTypeTests<int, DataTypeTests<int>.DefaultRecord>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
public override Type[] UnsupportedDefaultTypes { get; } =
[
typeof(byte),
typeof(decimal),
typeof(string[]),
];
}
}
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.Support;
using VectorData.ConformanceTests.TypeTests;
using Xunit;
#pragma warning disable CA2000 // Dispose objects before losing scope
namespace SqliteVec.ConformanceTests;
public class SqliteEmbeddingTypeTests(SqliteEmbeddingTypeTests.Fixture fixture)
: EmbeddingTypeTests<string>(fixture), IClassFixture<SqliteEmbeddingTypeTests.Fixture>
{
public new class Fixture : EmbeddingTypeTests<string>.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.
using SqliteVec.ConformanceTests.Support;
using VectorData.ConformanceTests.Support;
using VectorData.ConformanceTests.TypeTests;
using VectorData.ConformanceTests.Xunit;
using Xunit;
namespace SqliteVec.ConformanceTests.TypeTests;
public class SqliteKeyTypeTests(SqliteKeyTypeTests.Fixture fixture)
: KeyTypeTests(fixture), IClassFixture<SqliteKeyTypeTests.Fixture>
{
[ConditionalFact]
public virtual Task Int() => this.Test<int>(8, supportsAutoGeneration: true);
[ConditionalFact]
public virtual Task Long() => this.Test<long>(8L, supportsAutoGeneration: true);
[ConditionalFact]
public virtual Task String() => this.Test<string>("foo", "bar");
public new class Fixture : KeyTypeTests.Fixture
{
public override TestStore TestStore => SqliteTestStore.Instance;
}
}