Files
CBDD/tests/CBDD.Tests/DocumentCollectionIndexApiTests.cs
Joseph Doherty 3ffd468c79
All checks were successful
NuGet Publish / build-and-pack (push) Successful in 45s
NuGet Publish / publish-to-gitea (push) Successful in 52s
Fix audit findings for coverage, architecture checks, and XML docs
2026-02-20 15:43:25 -05:00

73 lines
2.4 KiB
C#

using ZB.MOM.WW.CBDD.Core.Indexing;
using ZB.MOM.WW.CBDD.Shared;
namespace ZB.MOM.WW.CBDD.Tests;
public class DocumentCollectionIndexApiTests : IDisposable
{
private readonly string _dbPath;
private readonly Shared.TestDbContext _db;
/// <summary>
/// Initializes a new instance of the <see cref="DocumentCollectionIndexApiTests"/> class.
/// </summary>
public DocumentCollectionIndexApiTests()
{
_dbPath = Path.Combine(Path.GetTempPath(), $"collection_index_api_{Guid.NewGuid():N}.db");
_db = new Shared.TestDbContext(_dbPath);
}
/// <summary>
/// Verifies vector index creation and deletion behavior.
/// </summary>
[Fact]
public void CreateVectorIndex_And_DropIndex_Should_Work()
{
_db.VectorItems.Insert(new VectorEntity { Title = "A", Embedding = [1f, 1f, 1f] });
_db.VectorItems.Insert(new VectorEntity { Title = "B", Embedding = [2f, 2f, 2f] });
_db.SaveChanges();
_db.VectorItems.CreateVectorIndex(v => v.Embedding, 3, VectorMetric.DotProduct, "idx_vector_extra");
var indexNames = _db.VectorItems.GetIndexes().Select(x => x.Name).ToList();
indexNames.ShouldContain("idx_vector_extra");
_db.VectorItems.DropIndex("idx_vector_extra").ShouldBeTrue();
_db.VectorItems.DropIndex("idx_vector_extra").ShouldBeFalse();
_db.VectorItems.GetIndexes().Select(x => x.Name).ShouldNotContain("idx_vector_extra");
}
/// <summary>
/// Verifies ensure-index returns existing indexes when already present.
/// </summary>
[Fact]
public void EnsureIndex_Should_Return_Existing_Index_When_Already_Present()
{
var first = _db.People.EnsureIndex(p => p.Age, name: "idx_people_age");
var second = _db.People.EnsureIndex(p => p.Age, name: "idx_people_age");
ReferenceEquals(first, second).ShouldBeTrue();
}
/// <summary>
/// Verifies dropping the primary index name is rejected.
/// </summary>
[Fact]
public void DropIndex_Should_Reject_Primary_Index_Name()
{
Should.Throw<InvalidOperationException>(() => _db.People.DropIndex("_id"));
}
/// <summary>
/// Disposes test resources and removes temporary files.
/// </summary>
public void Dispose()
{
_db.Dispose();
if (File.Exists(_dbPath)) File.Delete(_dbPath);
var wal = Path.ChangeExtension(_dbPath, ".wal");
if (File.Exists(wal)) File.Delete(wal);
}
}