Files
VectorDBDemo/MilvusDemo/MilvusServiceTests.cs
2025-10-17 02:46:25 +08:00

125 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Milvus.Client;
namespace MilvusDemo
{
/// <summary>
/// 用于测试MilvusService的模拟类
/// </summary>
public class MilvusServiceTests
{
// 模拟的MilvusService实例
private readonly MilvusService _milvusService;
// 标记各个方法是否被调用
public bool InitializeCollectionCalled { get; private set; }
public bool CreateIndexAndLoadCalled { get; private set; }
public bool InsertRandomDataCalled { get; private set; }
public bool SearchVectorCalled { get; private set; }
// 记录传入的参数
public int InsertDataCount { get; private set; }
public int SearchTopK { get; private set; }
public MilvusServiceTests()
{
// 创建真实的MilvusService实例但我们将拦截其方法调用
_milvusService = new MilvusService("localhost", 19530, "test_collection", 8);
}
/// <summary>
/// 模拟初始化集合的方法
/// </summary>
public async Task MockInitializeCollectionAsync()
{
InitializeCollectionCalled = true;
Console.WriteLine("[TEST] InitializeCollectionAsync called");
// 模拟操作延迟
await Task.Delay(100);
Console.WriteLine("[TEST] Collection dropped and created");
}
/// <summary>
/// 模拟创建索引并加载集合的方法
/// </summary>
public async Task MockCreateIndexAndLoadAsync()
{
CreateIndexAndLoadCalled = true;
Console.WriteLine("[TEST] CreateIndexAndLoadAsync called");
// 模拟操作延迟
await Task.Delay(100);
Console.WriteLine("[TEST] Index created");
Console.WriteLine("[TEST] Collection loaded");
}
/// <summary>
/// 模拟插入随机数据的方法
/// </summary>
public async Task MockInsertRandomDataAsync(int count)
{
InsertRandomDataCalled = true;
InsertDataCount = count;
Console.WriteLine($"[TEST] InsertRandomDataAsync called with count: {count}");
// 模拟操作延迟
await Task.Delay(100);
Console.WriteLine("[TEST] Data inserted");
}
/// <summary>
/// 模拟向量搜索的方法
/// </summary>
public async Task<SearchResults> MockSearchVectorAsync(int topK = 5)
{
SearchVectorCalled = true;
SearchTopK = topK;
Console.WriteLine($"[TEST] SearchVectorAsync called with topK: {topK}");
// 模拟操作延迟
await Task.Delay(100);
// 创建模拟的搜索结果
var mockIds = new List<long>();
for (int i = 0; i < topK; i++)
{
mockIds.Add(1000 + i); // 使用1000+i作为模拟ID
}
Console.WriteLine("[TEST] Search completed");
// 注意这里我们无法直接创建SearchResults实例因为它可能是内部类
// 在实际测试中您可能需要使用模拟框架如Moq来模拟这个返回值
// 这里我们返回null实际使用时需要处理这种情况
return null;
}
/// <summary>
/// 运行完整的测试流程
/// </summary>
public async Task RunFullTestAsync()
{
Console.WriteLine("=== Starting MilvusService Test ===");
await MockInitializeCollectionAsync();
await MockCreateIndexAndLoadAsync();
await MockInsertRandomDataAsync(1000);
await MockSearchVectorAsync(5);
// 打印测试结果摘要
Console.WriteLine("\n=== Test Summary ===");
Console.WriteLine($"InitializeCollection Called: {InitializeCollectionCalled}");
Console.WriteLine($"CreateIndexAndLoad Called: {CreateIndexAndLoadCalled}");
Console.WriteLine($"InsertRandomData Called: {InsertRandomDataCalled} (Count: {InsertDataCount})");
Console.WriteLine($"SearchVector Called: {SearchVectorCalled} (TopK: {SearchTopK})");
Console.WriteLine("=== Test Completed ===");
}
}
}