Skip to content

Commit e371bab

Browse files
committed
Skip package search when query is empty
An empty search returns hundreds of 100% popularity packages which is not useful. Only call FindAll when the user has provided an explicit search query.
1 parent 0c459ab commit e371bab

2 files changed

Lines changed: 77 additions & 17 deletions

File tree

internal/ui/packagepage/handler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ func (h *Handler) HandlePackages(w http.ResponseWriter, r *http.Request) {
3737

3838
currentMonth := web.GetLastCompleteMonth()
3939

40-
list, err := h.repo.FindAll(r.Context(), query, currentMonth, currentMonth, limit, offset)
41-
if err != nil {
42-
layout.ServerError(w, "failed to fetch packages", err)
43-
return
40+
var list *packages.PackagePopularityList
41+
if query != "" {
42+
var err error
43+
list, err = h.repo.FindAll(r.Context(), query, currentMonth, currentMonth, limit, offset)
44+
if err != nil {
45+
layout.ServerError(w, "failed to fetch packages", err)
46+
return
47+
}
48+
} else {
49+
list = &packages.PackagePopularityList{}
4450
}
4551

4652
selectedPackages, err := h.fetchComparePackages(r, compare, currentMonth)

internal/ui/packagepage/handler_test.go

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,42 @@ func (m *mockRepo) FindByName(ctx context.Context, name string, startMonth, endM
2525
}
2626

2727
func (m *mockRepo) FindAll(ctx context.Context, query string, startMonth, endMonth, limit, offset int) (*packages.PackagePopularityList, error) {
28-
return m.findAllFunc(ctx, query, startMonth, endMonth, limit, offset)
28+
if m.findAllFunc != nil {
29+
return m.findAllFunc(ctx, query, startMonth, endMonth, limit, offset)
30+
}
31+
return &packages.PackagePopularityList{Total: 0}, nil
2932
}
3033

3134
func (m *mockRepo) FindSeriesByName(ctx context.Context, name string, startMonth, endMonth, limit, offset int) (*packages.PackagePopularityList, error) {
3235
return nil, nil
3336
}
3437

3538
func TestHandlePackages(t *testing.T) {
39+
manifest, _ := layout.NewManifest([]byte(`{}`))
40+
repo := &mockRepo{
41+
findAllFunc: func(ctx context.Context, query string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
42+
t.Error("FindAll should not be called without a query")
43+
return &packages.PackagePopularityList{Total: 0}, nil
44+
},
45+
}
46+
handler := NewHandler(repo, manifest)
47+
48+
req := httptest.NewRequest(http.MethodGet, "/packages", nil)
49+
rr := httptest.NewRecorder()
50+
51+
handler.HandlePackages(rr, req)
52+
53+
if rr.Code != http.StatusOK {
54+
t.Errorf("expected status 200, got %d", rr.Code)
55+
}
56+
57+
body := rr.Body.String()
58+
if !strings.Contains(body, "Package statistics") {
59+
t.Error("expected body to contain title")
60+
}
61+
}
62+
63+
func TestHandlePackages_WithQuery(t *testing.T) {
3664
manifest, _ := layout.NewManifest([]byte(`{}`))
3765
repo := &mockRepo{
3866
findAllFunc: func(ctx context.Context, query string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
@@ -46,7 +74,7 @@ func TestHandlePackages(t *testing.T) {
4674
}
4775
handler := NewHandler(repo, manifest)
4876

49-
req := httptest.NewRequest(http.MethodGet, "/packages", nil)
77+
req := httptest.NewRequest(http.MethodGet, "/packages?query=pacman", nil)
5078
rr := httptest.NewRecorder()
5179

5280
handler.HandlePackages(rr, req)
@@ -56,9 +84,6 @@ func TestHandlePackages(t *testing.T) {
5684
}
5785

5886
body := rr.Body.String()
59-
if !strings.Contains(body, "Package statistics") {
60-
t.Error("expected body to contain title")
61-
}
6287
if !strings.Contains(body, "pacman") {
6388
t.Error("expected body to contain package name")
6489
}
@@ -68,6 +93,7 @@ func TestHandlePackages_WithCompare(t *testing.T) {
6893
manifest, _ := layout.NewManifest([]byte(`{}`))
6994
repo := &mockRepo{
7095
findAllFunc: func(ctx context.Context, query string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
96+
t.Error("FindAll should not be called when compare is set without query")
7197
return &packages.PackagePopularityList{Total: 0}, nil
7298
},
7399
}
@@ -88,6 +114,41 @@ func TestHandlePackages_WithCompare(t *testing.T) {
88114
}
89115
}
90116

117+
func TestHandlePackages_WithCompareAndQuery(t *testing.T) {
118+
manifest, _ := layout.NewManifest([]byte(`{}`))
119+
findAllCalled := false
120+
repo := &mockRepo{
121+
findAllFunc: func(ctx context.Context, query string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
122+
findAllCalled = true
123+
return &packages.PackagePopularityList{
124+
Total: 1,
125+
PackagePopularities: []packages.PackagePopularity{
126+
{Name: "pacman", Popularity: 10.5},
127+
},
128+
}, nil
129+
},
130+
}
131+
handler := NewHandler(repo, manifest)
132+
133+
req := httptest.NewRequest(http.MethodGet, "/packages?query=pac&compare=glibc", nil)
134+
rr := httptest.NewRecorder()
135+
136+
handler.HandlePackages(rr, req)
137+
138+
if rr.Code != http.StatusOK {
139+
t.Errorf("expected status 200, got %d", rr.Code)
140+
}
141+
142+
if !findAllCalled {
143+
t.Error("FindAll should be called when both query and compare are set")
144+
}
145+
146+
body := rr.Body.String()
147+
if !strings.Contains(body, "pacman") {
148+
t.Error("expected body to contain search result")
149+
}
150+
}
151+
91152
func TestHandlePackages_CompareExceedsSelectLimit(t *testing.T) {
92153
manifest, _ := layout.NewManifest([]byte(`{}`))
93154

@@ -154,11 +215,7 @@ func TestHandlePackages_CompareExceedsChartLimit(t *testing.T) {
154215
names[i] = "pkg" + strings.Repeat("x", i)
155216
}
156217

157-
repo := &mockRepo{
158-
findAllFunc: func(_ context.Context, _ string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
159-
return &packages.PackagePopularityList{Total: 0}, nil
160-
},
161-
}
218+
repo := &mockRepo{}
162219
handler := NewHandler(repo, manifest)
163220

164221
req := httptest.NewRequest(http.MethodGet, "/packages?compare="+strings.Join(names, ","), nil)
@@ -191,9 +248,6 @@ func TestHandlePackages_CompareExceedsChartLimit(t *testing.T) {
191248
func TestHandlePackages_CompareError(t *testing.T) {
192249
manifest, _ := layout.NewManifest([]byte(`{}`))
193250
repo := &mockRepo{
194-
findAllFunc: func(_ context.Context, _ string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
195-
return &packages.PackagePopularityList{Total: 0}, nil
196-
},
197251
findByNameFunc: func(_ context.Context, _ string, _, _ int) (*packages.PackagePopularity, error) {
198252
return nil, errors.New("db error")
199253
},

0 commit comments

Comments
 (0)