TestBasicFunctionality 测试基本功能
()
| 12 | |
| 13 | // TestBasicFunctionality 测试基本功能 |
| 14 | func TestBasicFunctionality() error { |
| 15 | logger.SysLogf("=== 测试向量数据库驱动基本功能 ===") |
| 16 | |
| 17 | // 1. 测试配置加载 |
| 18 | logger.SysLogf("1. 测试配置加载...") |
| 19 | config := LoadFromEnv() |
| 20 | if err := config.Validate(); err != nil { |
| 21 | return fmt.Errorf("配置验证失败: %v", err) |
| 22 | } |
| 23 | logger.SysLogf(" 配置类型: %s, 端点: %s", config.Type, config.Endpoint) |
| 24 | |
| 25 | // 2. 测试向量存储创建 |
| 26 | logger.SysLogf("2. 测试向量存储创建...") |
| 27 | store, err := NewVectorStore(config) |
| 28 | if err != nil { |
| 29 | return fmt.Errorf("创建向量存储失败: %v", err) |
| 30 | } |
| 31 | logger.SysLogf(" 向量存储类型: %s", store.Type()) |
| 32 | |
| 33 | // 3. 测试连接 |
| 34 | logger.SysLogf("3. 测试连接...") |
| 35 | ctx := context.Background() |
| 36 | if err := store.Connect(ctx); err != nil { |
| 37 | return fmt.Errorf("连接失败: %v", err) |
| 38 | } |
| 39 | logger.SysLogf(" 连接成功") |
| 40 | |
| 41 | // 4. 测试健康检查 |
| 42 | logger.SysLogf("4. 测试健康检查...") |
| 43 | if err := store.HealthCheck(ctx); err != nil { |
| 44 | return fmt.Errorf("健康检查失败: %v", err) |
| 45 | } |
| 46 | logger.SysLogf(" 健康检查通过") |
| 47 | |
| 48 | // 5. 测试集合操作 |
| 49 | logger.SysLogf("5. 测试集合操作...") |
| 50 | testCollection := "test_collection_" + fmt.Sprintf("%d", time.Now().Unix()) |
| 51 | |
| 52 | // 创建测试集合 |
| 53 | collectionConfig := CollectionConfig{ |
| 54 | Name: testCollection, |
| 55 | Dimension: 3, |
| 56 | Metric: "cosine", |
| 57 | IndexType: "HNSW", |
| 58 | } |
| 59 | |
| 60 | if err := store.CreateCollection(ctx, collectionConfig); err != nil { |
| 61 | return fmt.Errorf("创建集合失败: %v", err) |
| 62 | } |
| 63 | logger.SysLogf(" 创建集合成功: %s", testCollection) |
| 64 | |
| 65 | // 检查集合是否存在(通过获取集合信息的方式) |
| 66 | _, err = store.GetCollectionInfo(ctx, testCollection) |
| 67 | if err != nil { |
| 68 | if vsErr, ok := err.(*VectorStoreError); ok && vsErr.Code == ErrCodeCollectionNotFound { |
| 69 | return fmt.Errorf("集合应该存在但获取信息失败") |
| 70 | } |
| 71 | return fmt.Errorf("获取集合信息失败: %v", err) |
no test coverage detected