@@ -2,6 +2,7 @@ package country
22
33import (
44 "context"
5+ "errors"
56 "net/http"
67 "net/http/httptest"
78 "strings"
@@ -12,7 +13,8 @@ import (
1213)
1314
1415type mockRepo struct {
15- findAllFunc func (ctx context.Context , query string , startMonth , endMonth , limit , offset int ) (* countries.CountryPopularityList , error )
16+ findAllFunc func (ctx context.Context , query string , startMonth , endMonth , limit , offset int ) (* countries.CountryPopularityList , error )
17+ findSeriesByCodeFunc func (ctx context.Context , code string , startMonth , endMonth , limit , offset int ) (* countries.CountryPopularityList , error )
1618}
1719
1820func (m * mockRepo ) FindByCode (ctx context.Context , code string , startMonth , endMonth int ) (* countries.CountryPopularity , error ) {
@@ -24,6 +26,9 @@ func (m *mockRepo) FindAll(ctx context.Context, query string, startMonth, endMon
2426}
2527
2628func (m * mockRepo ) FindSeriesByCode (ctx context.Context , code string , startMonth , endMonth , limit , offset int ) (* countries.CountryPopularityList , error ) {
29+ if m .findSeriesByCodeFunc != nil {
30+ return m .findSeriesByCodeFunc (ctx , code , startMonth , endMonth , limit , offset )
31+ }
2732 return nil , nil
2833}
2934
@@ -58,3 +63,79 @@ func TestHandleCountries(t *testing.T) {
5863 t .Error ("expected body to contain country code" )
5964 }
6065}
66+
67+ func TestHandleCountryDetail (t * testing.T ) {
68+ manifest , _ := layout .NewManifest ([]byte (`{}` ))
69+ var queriedCode string
70+ repo := & mockRepo {
71+ findSeriesByCodeFunc : func (ctx context.Context , code string , _ , _ , _ , _ int ) (* countries.CountryPopularityList , error ) {
72+ queriedCode = code
73+ return & countries.CountryPopularityList {
74+ Total : 1 ,
75+ CountryPopularities : []countries.CountryPopularity {
76+ {Code : code , StartMonth : 202501 , EndMonth : 202501 , Popularity : 10.5 },
77+ },
78+ }, nil
79+ },
80+ }
81+ handler := NewHandler (repo , manifest )
82+
83+ req := httptest .NewRequest (http .MethodGet , "/countries/de" , nil )
84+ req .SetPathValue ("code" , "de" )
85+ rr := httptest .NewRecorder ()
86+
87+ handler .HandleCountryDetail (rr , req )
88+
89+ if rr .Code != http .StatusOK {
90+ t .Errorf ("expected status 200, got %d" , rr .Code )
91+ }
92+
93+ if queriedCode != "DE" {
94+ t .Errorf ("expected uppercase query DE, got %q" , queriedCode )
95+ }
96+
97+ body := rr .Body .String ()
98+ if ! strings .Contains (body , "DE" ) {
99+ t .Error ("expected body to contain country code" )
100+ }
101+ }
102+
103+ func TestHandleCountryDetail_NotFound (t * testing.T ) {
104+ manifest , _ := layout .NewManifest ([]byte (`{}` ))
105+ repo := & mockRepo {
106+ findSeriesByCodeFunc : func (ctx context.Context , code string , _ , _ , _ , _ int ) (* countries.CountryPopularityList , error ) {
107+ return & countries.CountryPopularityList {Total : 0 }, nil
108+ },
109+ }
110+ handler := NewHandler (repo , manifest )
111+
112+ req := httptest .NewRequest (http .MethodGet , "/countries/xx" , nil )
113+ req .SetPathValue ("code" , "xx" )
114+ rr := httptest .NewRecorder ()
115+
116+ handler .HandleCountryDetail (rr , req )
117+
118+ if rr .Code != http .StatusNotFound {
119+ t .Errorf ("expected status 404, got %d" , rr .Code )
120+ }
121+ }
122+
123+ func TestHandleCountryDetail_Error (t * testing.T ) {
124+ manifest , _ := layout .NewManifest ([]byte (`{}` ))
125+ repo := & mockRepo {
126+ findSeriesByCodeFunc : func (_ context.Context , _ string , _ , _ , _ , _ int ) (* countries.CountryPopularityList , error ) {
127+ return nil , errors .New ("db error" )
128+ },
129+ }
130+ handler := NewHandler (repo , manifest )
131+
132+ req := httptest .NewRequest (http .MethodGet , "/countries/de" , nil )
133+ req .SetPathValue ("code" , "de" )
134+ rr := httptest .NewRecorder ()
135+
136+ handler .HandleCountryDetail (rr , req )
137+
138+ if rr .Code != http .StatusInternalServerError {
139+ t .Errorf ("expected status 500, got %d" , rr .Code )
140+ }
141+ }
0 commit comments