测试SearchPosts函数
(t *testing.T)
| 53 | |
| 54 | // 测试SearchPosts函数 |
| 55 | func TestSearchPosts(t *testing.T) { |
| 56 | client := createMockElasticsearchClient() |
| 57 | searchDAO := dao.NewSearchDAO(client, createMockLogger()) |
| 58 | ctx := context.Background() |
| 59 | keyword := fmt.Sprintf("integration-post-%d", time.Now().UnixNano()) |
| 60 | if err := searchDAO.InputPost(ctx, dao.PostSearch{ |
| 61 | Id: uint(time.Now().UnixNano()), |
| 62 | Title: keyword, |
| 63 | Content: keyword + "-content", |
| 64 | Status: 1, |
| 65 | }); err != nil { |
| 66 | t.Fatalf("InputPost failed: %v", err) |
| 67 | } |
| 68 | if _, err := client.Indices.Refresh().Index(dao.PostIndex).Do(ctx); err != nil { |
| 69 | t.Fatalf("Refresh post index failed: %v", err) |
| 70 | } |
| 71 | |
| 72 | keywords := []string{keyword} |
| 73 | posts, err := searchDAO.SearchPosts(context.Background(), keywords) |
| 74 | if err != nil { |
| 75 | t.Errorf("SearchPosts failed: %v", err) |
| 76 | return |
| 77 | } |
| 78 | if len(posts) == 0 { |
| 79 | t.Fatal("SearchPosts returned no results") |
| 80 | } |
| 81 | // 可以进一步验证返回的帖子数据结构是否符合预期 |
| 82 | for _, post := range posts { |
| 83 | postJSON, _ := json.Marshal(post) |
| 84 | t.Logf("Retrieved post: %s", postJSON) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // 测试SearchUsers函数 |
| 89 | func TestSearchUsers(t *testing.T) { |
nothing calls this directly
no test coverage detected