Skip to content

Commit

Permalink
Add a test for geocode.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions geocoder/geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func NewGeocoderClient() *GeocoderClient {
return &GeocoderClient{}
}

func GetPlace(geocodeURL string) ([]Place, error) {
resp, _ := http.Get(geocodeURL)

var places []Place

if err := json.NewDecoder(resp.Body).Decode(&places); err != nil {
fmt.Println("Error decoding JSON:", err)
}
defer resp.Body.Close()
return places, nil
}

func (g *GeocoderClient) FindCoordinates(address string) ([]Place, error) {
queryParams := url.Values{}
queryParams.Add("q", address)
Expand Down
82 changes: 82 additions & 0 deletions geocoder/geocoder_test.go
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()
})
}
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ require (
github.com/zapling/yr.no-golang-client v0.0.0-20210309083036-f048e27db764
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -24,6 +30,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.11.3 // indirect
golang.org/x/sys v0.13.0 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:W
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
Expand Down Expand Up @@ -41,6 +42,7 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -54,6 +56,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
Expand Down Expand Up @@ -82,4 +86,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit afd6141

Please sign in to comment.