package playletcache import "testing" func TestListHomeThemesByRankRanksEligibleThemes(t *testing.T) { cache.lock.Lock() previous := cache.themeByID cache.themeByID = map[int64]*ThemeItem{} for id := int64(1); id <= 10; id++ { cache.themeByID[id] = &ThemeItem{Id: id, Rank: id, Status: true, PlayletCount: 1} } cache.themeByID[11] = &ThemeItem{Id: 11, Rank: 100, Status: false, IsRecommend: true, PlayletCount: 1} cache.themeByID[12] = &ThemeItem{Id: 12, Rank: 100, Status: true, PlayletCount: 0} cache.themeByID[13] = &ThemeItem{Id: 13, Rank: 100, Status: true, PlayletCount: 0} cache.lock.Unlock() t.Cleanup(func() { cache.lock.Lock() cache.themeByID = previous cache.lock.Unlock() }) got := ListHomeThemesByRank(8) if len(got) != 8 { t.Fatalf("got %d themes, want 8", len(got)) } for index, item := range got { want := int64(10 - index) if item.Id != want { t.Fatalf("item %d has id %d, want %d", index, item.Id, want) } } } func TestListHotSearchLimitsAndCopies(t *testing.T) { cache.lock.Lock() previous := cache.hotSearch cache.hotSearch = make([]HotSearchItem, 16) for index := range cache.hotSearch { cache.hotSearch[index] = HotSearchItem{Keyword: string(rune('A' + index)), Count: int64(16 - index)} } cache.lock.Unlock() t.Cleanup(func() { cache.lock.Lock() cache.hotSearch = previous cache.lock.Unlock() }) home := ListHotSearch(15) if len(home) != 15 || home[0].Keyword != "A" || home[14].Keyword != "O" { t.Fatalf("home hot search = %+v, want first 15 terms", home) } if got := ListHotSearch(10); len(got) != 10 { t.Fatalf("hot-like has %d terms, want 10", len(got)) } home[0].Keyword = "changed" if got := ListHotSearch(1)[0].Keyword; got != "A" { t.Fatalf("cache term changed to %q", got) } }