|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// TestSearchClassesWithHighlighting 测试带高亮的类搜索功能 |
| 12 | +func TestSearchClassesWithHighlighting(t *testing.T) { |
| 13 | + // 使用真实客户端 |
| 14 | + client := createRealClient(t) |
| 15 | + |
| 16 | + // 设置更长的超时时间 |
| 17 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 18 | + defer cancel() |
| 19 | + |
| 20 | + // 测试全限定类名 |
| 21 | + className := "org.apache.commons.io.FileUtils" |
| 22 | + |
| 23 | + // 设置子测试的超时时间 |
| 24 | + subCtx, subCancel := context.WithTimeout(ctx, 20*time.Second) |
| 25 | + defer subCancel() |
| 26 | + |
| 27 | + // 执行带高亮的搜索 |
| 28 | + result, err := client.SearchClassesWithHighlighting(subCtx, className, 3) |
| 29 | + |
| 30 | + // 如果API连接失败,跳过测试 |
| 31 | + if err != nil { |
| 32 | + t.Logf("搜索 %s 时出错: %v", className, err) |
| 33 | + t.Skip("无法连接到Maven Central API") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // 验证返回结果 |
| 38 | + assert.NotNil(t, result, "搜索结果不应为空") |
| 39 | + assert.NotNil(t, result.ResponseBody, "响应体不应为空") |
| 40 | + assert.NotEmpty(t, result.ResponseBody.Docs, "搜索结果不应为空") |
| 41 | + |
| 42 | + // 验证高亮信息 |
| 43 | + assert.NotNil(t, result.Highlighting, "高亮信息不应为空") |
| 44 | + |
| 45 | + // 输出结果信息 |
| 46 | + t.Logf("找到 %d 个包含 %s 的结果", result.ResponseBody.NumFound, className) |
| 47 | + for i, doc := range result.ResponseBody.Docs { |
| 48 | + t.Logf("结果 %d: %s:%s:%s", i+1, doc.GroupId, doc.ArtifactId, doc.Version) |
| 49 | + |
| 50 | + // 显示高亮信息 |
| 51 | + if highlightInfo, exists := result.Highlighting[doc.ID]; exists { |
| 52 | + if fchHighlights, hasFch := highlightInfo["fch"]; hasFch && len(fchHighlights) > 0 { |
| 53 | + t.Logf(" 高亮: %s", fchHighlights[0]) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// TestExtractHighlightedClasses 测试高亮提取函数 |
| 60 | +func TestExtractHighlightedClasses(t *testing.T) { |
| 61 | + // 使用真实客户端 |
| 62 | + client := createRealClient(t) |
| 63 | + |
| 64 | + // 设置超时 |
| 65 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + // 测试全限定类名 |
| 69 | + className := "org.junit.Assert" |
| 70 | + |
| 71 | + // 设置子测试的超时时间 |
| 72 | + subCtx, subCancel := context.WithTimeout(ctx, 20*time.Second) |
| 73 | + defer subCancel() |
| 74 | + |
| 75 | + // 执行带高亮的搜索 |
| 76 | + result, err := client.SearchClassesWithHighlighting(subCtx, className, 3) |
| 77 | + |
| 78 | + // 如果API连接失败,跳过测试 |
| 79 | + if err != nil { |
| 80 | + t.Logf("搜索 %s 时出错: %v", className, err) |
| 81 | + t.Skip("无法连接到Maven Central API") |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + // 提取高亮信息 |
| 86 | + highlights := ExtractHighlightedClasses(result) |
| 87 | + |
| 88 | + // 验证提取的高亮信息 |
| 89 | + assert.NotNil(t, highlights, "提取的高亮信息不应为空") |
| 90 | + assert.NotEmpty(t, highlights, "提取的高亮信息应该包含数据") |
| 91 | + |
| 92 | + // 输出结果信息 |
| 93 | + t.Logf("从 %d 个结果中提取了 %d 个高亮类名", len(result.ResponseBody.Docs), len(highlights)) |
| 94 | + for docId, highlightedClasses := range highlights { |
| 95 | + t.Logf("文档 ID: %s", docId) |
| 96 | + for i, cls := range highlightedClasses { |
| 97 | + t.Logf(" 高亮类 %d: %s", i+1, cls) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// TestSearchFullyQualifiedClassNames 测试完全限定类名搜索 |
| 103 | +func TestSearchFullyQualifiedClassNames(t *testing.T) { |
| 104 | + // 使用真实客户端 |
| 105 | + client := createRealClient(t) |
| 106 | + |
| 107 | + // 设置超时 |
| 108 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 109 | + defer cancel() |
| 110 | + |
| 111 | + // 测试全限定类名 |
| 112 | + className := "org.apache.commons.lang3.StringUtils" |
| 113 | + |
| 114 | + // 设置子测试的超时时间 |
| 115 | + subCtx, subCancel := context.WithTimeout(ctx, 20*time.Second) |
| 116 | + defer subCancel() |
| 117 | + |
| 118 | + // 执行搜索 |
| 119 | + versions, highlights, err := client.SearchFullyQualifiedClassNames(subCtx, className, 3) |
| 120 | + |
| 121 | + // 如果API连接失败,跳过测试 |
| 122 | + if err != nil { |
| 123 | + t.Logf("搜索 %s 时出错: %v", className, err) |
| 124 | + t.Skip("无法连接到Maven Central API") |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + // 验证搜索结果 |
| 129 | + assert.NotNil(t, versions, "搜索结果不应为空") |
| 130 | + assert.NotEmpty(t, versions, "搜索结果应该包含数据") |
| 131 | + assert.NotNil(t, highlights, "高亮信息不应为空") |
| 132 | + |
| 133 | + // 输出结果信息 |
| 134 | + t.Logf("找到 %d 个包含 %s 的结果", len(versions), className) |
| 135 | + for i, v := range versions { |
| 136 | + t.Logf("结果 %d: %s:%s:%s", i+1, v.GroupId, v.ArtifactId, v.Version) |
| 137 | + |
| 138 | + // 显示对应的高亮信息 |
| 139 | + if hlClasses, exists := highlights[v.ID]; exists { |
| 140 | + for j, cls := range hlClasses { |
| 141 | + t.Logf(" 高亮类 %d: %s", j+1, cls) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments