MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / TestSearchPosts

Function TestSearchPosts

internal/repository/search_test.go:55–86  ·  view source on GitHub ↗

测试SearchPosts函数

(t *testing.T)

Source from the content-addressed store, hash-verified

53
54// 测试SearchPosts函数
55func 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函数
89func TestSearchUsers(t *testing.T) {

Callers

nothing calls this directly

Calls 5

InputPostMethod · 0.95
SearchPostsMethod · 0.95
NewSearchDAOFunction · 0.92
createMockLoggerFunction · 0.85

Tested by

no test coverage detected