-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I want to test that the geocode function returns what is expected given the response from the geocode api. In order to mock the response of the geocode api using the http.Server test mock, it is necessary to refactor the geocode method so that it accepts the url to GET from as an argument. If it accepts it as an argument then we can mock it
- Loading branch information
CiaraTully
committed
Nov 12, 2023
1 parent
c37ddfc
commit afd6141
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package geocoder | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetCoordinates(t *testing.T) { | ||
type args struct { | ||
url string | ||
httpGetStatus int | ||
} | ||
|
||
// the json keys returned from the geocoder api | ||
type ServerResponse struct { | ||
Lat string `json:"lat"` | ||
Lon string `json:"lon"` | ||
DisplayName string `json:"display_name"` | ||
Class string `json:"class"` | ||
Type string `json:"type"` | ||
Importance float64 `json:"importance"` | ||
PlaceID int `json:"place_id"` | ||
License string `json:"licence"` | ||
PoweredBy string `json:"powered_by"` | ||
OSMType string `json:"osm_type"` | ||
OSMID int `json:"osm_id"` | ||
BoundingBox []string `json:"boundingbox"` | ||
} | ||
|
||
createServerMock := func(args2 args, serverResponseBody []ServerResponse) *httptest.Server { | ||
s := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(args2.httpGetStatus) | ||
jsonResponse, _ := json.Marshal(serverResponseBody) | ||
w.Write(jsonResponse) | ||
}), | ||
) | ||
return s | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
serverJSONResponse []ServerResponse | ||
want []Place | ||
wantErr error | ||
}{ | ||
{ | ||
name: "Returns slice of Places when 200 response", | ||
args: args{httpGetStatus: 200}, | ||
serverJSONResponse: []ServerResponse{{Lat: "100", Lon: "100", DisplayName: "Somewhere", PlaceID: 123}, {Lat: "200", Lon: "100", DisplayName: "Everest", PlaceID: 789}}, | ||
want: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}, {Latitude: "200", Longitude: "100", DisplayName: "Everest"}}, | ||
}, | ||
{ | ||
name: "Returns empty Place slice if the geocode returns no results ", | ||
args: args{httpGetStatus: 200}, | ||
serverJSONResponse: []ServerResponse{}, | ||
want: []Place{}, | ||
}, | ||
{ | ||
name: "Returns empty Place slice if geocode returns a 500", | ||
args: args{httpGetStatus: 500}, | ||
serverJSONResponse: []ServerResponse{}, | ||
want: []Place{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
serverMock := createServerMock(tt.args, tt.serverJSONResponse) | ||
tt.args.url = serverMock.URL | ||
got, err := GetPlace(tt.args.url) | ||
assert.Equal(t, tt.wantErr, err) | ||
assert.Equal(t, tt.want, got) | ||
serverMock.Close() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters