@@ -9,24 +9,24 @@ import (
99 "net/http/httptest"
1010 "net/url"
1111 "reflect"
12- "strconv"
1312 "testing"
1413
1514 "github.com/odpf/columbus/api/handlers"
1615 "github.com/odpf/columbus/lib/mock"
1716 "github.com/odpf/columbus/models"
1817
18+ "github.com/stretchr/testify/assert"
1919 testifyMock "github.com/stretchr/testify/mock"
20+ "github.com/stretchr/testify/require"
2021)
2122
2223func TestSearchHandler (t * testing.T ) {
2324 ctx := context .Background ()
2425 // todo: pass testCase to ValidateResponse
2526 type testCase struct {
2627 Title string
27- SearchText string
2828 ExpectStatus int
29- RequestParams map [ string ][] string
29+ Querystring string
3030 InitRepo func (testCase , * mock.TypeRepository )
3131 InitSearcher func (testCase , * mock.RecordSearcher )
3232 ValidateResponse func (testCase , io.Reader ) error
@@ -56,11 +56,11 @@ func TestSearchHandler(t *testing.T) {
5656 Title : "should return HTTP 400 if 'text' parameter is empty or missing" ,
5757 ExpectStatus : http .StatusBadRequest ,
5858 ValidateResponse : func (tc testCase , body io.Reader ) error { return nil },
59- SearchText : "" ,
59+ Querystring : "" ,
6060 },
6161 {
62- Title : "should report HTTP 500 if record searcher fails" ,
63- SearchText : "test" ,
62+ Title : "should report HTTP 500 if record searcher fails" ,
63+ Querystring : "text= test" ,
6464 InitSearcher : func (tc testCase , searcher * mock.RecordSearcher ) {
6565 err := fmt .Errorf ("service unavailable" )
6666 searcher .On ("Search" , ctx , testifyMock .AnythingOfType ("models.SearchConfig" )).
@@ -69,8 +69,8 @@ func TestSearchHandler(t *testing.T) {
6969 ExpectStatus : http .StatusInternalServerError ,
7070 },
7171 {
72- Title : "should return an error if looking up an type detail fails" ,
73- SearchText : "test" ,
72+ Title : "should return an error if looking up an type detail fails" ,
73+ Querystring : "text= test" ,
7474 InitSearcher : func (tc testCase , searcher * mock.RecordSearcher ) {
7575 results := []models.SearchResult {
7676 {
@@ -88,55 +88,31 @@ func TestSearchHandler(t *testing.T) {
8888 ExpectStatus : http .StatusInternalServerError ,
8989 },
9090 {
91- Title : "should pass filter to search config format" ,
92- SearchText : "resource" ,
93- RequestParams : map [string ][]string {
94- // "landscape" is not a valid filter key. All filters
95- // begin with the "filter." prefix. Adding this here is just a little
96- // extra check to make sure that the handler correctly parses the filters.
97- "landscape" : {"id" , "vn" },
98- "filter.landscape" : {"th" },
99- "filter.type" : {"dagger" },
100- },
101- InitRepo : withTypes (testdata .Type ),
91+ Title : "should pass filter to search config format" ,
92+ Querystring : "text=resource&landscape=id,vn&filter.data.landscape=th&filter.type=topic&filter.service=kafka,rabbitmq" ,
10293 InitSearcher : func (tc testCase , searcher * mock.RecordSearcher ) {
10394 cfg := models.SearchConfig {
104- Text : tc . SearchText ,
105- TypeWhiteList : tc . RequestParams [ "filter.type" ] ,
95+ Text : "resource" ,
96+ TypeWhiteList : [] string { "topic" } ,
10697 Filters : map [string ][]string {
107- "landscape" : tc .RequestParams ["filter.landscape" ],
98+ "service" : {"kafka" , "rabbitmq" },
99+ "data.landscape" : {"th" },
108100 },
109101 }
110102
111- results := []models.SearchResult {
112- {
113- TypeName : testdata .Type .Name ,
114- Record : models.Record {
115- Urn : "test-1" ,
116- Name : "test 1" ,
117- Service : "test-service" ,
118- Data : map [string ]interface {}{
119- "id" : "test-1" ,
120- "title" : "test 1" ,
121- "landscape" : "th" ,
122- "entity" : "odpf" ,
123- },
124- },
125- },
126- }
127- searcher .On ("Search" , ctx , cfg ).Return (results , nil )
103+ searcher .On ("Search" , ctx , cfg ).Return ([]models.SearchResult {}, nil )
128104 return
129105 },
130106 ValidateResponse : func (tc testCase , body io.Reader ) error {
131107 return nil
132108 },
133109 },
134110 {
135- Title : "should return the matched documents" ,
136- SearchText : "test" ,
111+ Title : "should return the matched documents" ,
112+ Querystring : "text= test" ,
137113 InitSearcher : func (tc testCase , searcher * mock.RecordSearcher ) {
138114 cfg := models.SearchConfig {
139- Text : tc . SearchText ,
115+ Text : "test" ,
140116 Filters : make (map [string ][]string ),
141117 }
142118 response := []models.SearchResult {
@@ -190,17 +166,13 @@ func TestSearchHandler(t *testing.T) {
190166 },
191167 },
192168 {
193- Title : "should return the requested number of records" ,
194- RequestParams : map [string ][]string {
195- "size" : {"15" },
196- },
197- SearchText : "resource" ,
198- InitRepo : withTypes (testdata .Type ),
169+ Title : "should return the requested number of records" ,
170+ Querystring : "text=resource&size=10" ,
171+ InitRepo : withTypes (testdata .Type ),
199172 InitSearcher : func (tc testCase , searcher * mock.RecordSearcher ) {
200- maxResults , _ := strconv .Atoi (tc .RequestParams ["size" ][0 ])
201173 cfg := models.SearchConfig {
202- Text : tc . SearchText ,
203- MaxResults : maxResults ,
174+ Text : "resource" ,
175+ MaxResults : 10 ,
204176 Filters : make (map [string ][]string ),
205177 }
206178
@@ -239,10 +211,11 @@ func TestSearchHandler(t *testing.T) {
239211 if err != nil {
240212 return fmt .Errorf ("error reading response body: %v" , err )
241213 }
242- expectedResults , _ := strconv .Atoi (tc .RequestParams ["size" ][0 ])
243- actualResults := len (payload )
244- if expectedResults != actualResults {
245- return fmt .Errorf ("expected search request to return %d results, returned %d results instead" , expectedResults , actualResults )
214+
215+ expectedSize := 10
216+ actualSize := len (payload )
217+ if expectedSize != actualSize {
218+ return fmt .Errorf ("expected search request to return %d results, returned %d results instead" , expectedSize , actualSize )
246219 }
247220 return nil
248221 },
@@ -263,15 +236,9 @@ func TestSearchHandler(t *testing.T) {
263236 defer recordSearcher .AssertExpectations (t )
264237 defer typeRepo .AssertExpectations (t )
265238
266- params := url.Values {}
267- params .Add ("text" , testCase .SearchText )
268- if testCase .RequestParams != nil {
269- for key , values := range testCase .RequestParams {
270- for _ , value := range values {
271- params .Add (key , value )
272- }
273- }
274- }
239+ params , err := url .ParseQuery (testCase .Querystring )
240+ require .NoError (t , err )
241+
275242 requestURL := "/?" + params .Encode ()
276243 rr := httptest .NewRequest (http .MethodGet , requestURL , nil )
277244 rw := httptest .NewRecorder ()
@@ -283,10 +250,7 @@ func TestSearchHandler(t *testing.T) {
283250 if expectStatus == 0 {
284251 expectStatus = http .StatusOK
285252 }
286- if rw .Code != expectStatus {
287- t .Errorf ("expected handler to return http status %d, was %d instead" , expectStatus , rw .Code )
288- return
289- }
253+ assert .Equal (t , expectStatus , rw .Code )
290254 if testCase .ValidateResponse != nil {
291255 if err := testCase .ValidateResponse (testCase , rw .Body ); err != nil {
292256 t .Errorf ("error validating handler response: %v" , err )
0 commit comments