Skip to content

Commit

Permalink
Restructre geocode package
Browse files Browse the repository at this point in the history
Why?
To make the code base more maintainable and readable.

How?
Split the geocode client, interface and api implememtation into differnt
files. Split the tests into different files too.
  • Loading branch information
CiaraTully committed Dec 31, 2023
1 parent f9f03c1 commit a8cf11d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 139 deletions.
41 changes: 41 additions & 0 deletions geocoder/geocode_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package geocoder

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)

type GeocodeAPI struct {
baseURL string
endpoint string
apiKey string
}

func NewGeoCodeMaps() Geocoder {
return &GeocodeAPI{baseURL: "https://geocode.maps.co", endpoint: "/search", apiKey: os.Getenv("GEOCODE_API_KEY")}
}

func (g *GeocodeAPI) fullURL() string {
return g.baseURL + g.endpoint
}

func (g *GeocodeAPI) getPlace(address string) ([]Place, error) {
queryParams := url.Values{}
queryParams.Add("q", address)
queryParams.Add("api_key", g.apiKey)
fullURL := fmt.Sprintf("%s?%s", g.fullURL(), queryParams.Encode())
resp, err := http.Get(fullURL)
if err != nil {
print("Error retrieving location information")
}
defer resp.Body.Close()

var places []Place
if err := json.NewDecoder(resp.Body).Decode(&places); err != nil {
fmt.Println("Error decoding JSON:", err)
}
return places, nil
}
60 changes: 11 additions & 49 deletions geocoder/geocoder_test.go → geocoder/geocode_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetCoordinates(t *testing.T) {
type args struct {
func TestGetPlace(t *testing.T) {
type server_config struct {
url string
httpGetStatus int
}
Expand All @@ -31,11 +31,11 @@ func TestGetCoordinates(t *testing.T) {
BoundingBox []string `json:"boundingbox"`
}

createServerMock := func(args2 args, serverResponseBody []ServerResponse) *httptest.Server {
createServerMock := func(server_config_args server_config, serverResponseBody []ServerResponse) *httptest.Server {
s := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(args2.httpGetStatus)
w.WriteHeader(server_config_args.httpGetStatus)
jsonResponse, _ := json.Marshal(serverResponseBody)
w.Write(jsonResponse)
}),
Expand All @@ -45,77 +45,39 @@ func TestGetCoordinates(t *testing.T) {

tests := []struct {
name string
args args
serverJSONResponse []ServerResponse // TO DO: refactor this to move it into args
args server_config
serverJSONResponse []ServerResponse
want []Place
wantErr error
}{
{
name: "Returns slice of Places when 200 response",
args: args{httpGetStatus: 200},
args: server_config{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},
args: server_config{httpGetStatus: 200},
serverJSONResponse: []ServerResponse{},
want: []Place{},
},
{
name: "Returns empty Place slice if geocode returns a 500",
args: args{httpGetStatus: 500},
args: server_config{httpGetStatus: 500},
serverJSONResponse: []ServerResponse{},
want: []Place{},
},
}

geo := GeocodeAPI{baseURL: "testBase.com", endpoint: "/search"}
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 := geo.getPlace(tt.args.url)
geo := GeocodeAPI{baseURL: serverMock.URL, endpoint: "/search", apiKey: "testKey"}
got, err := geo.getPlace("test_location")
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.want, got)
serverMock.Close()
})
}
}

type MockGeocodeAPI struct {
places []Place
err error
}

func (m MockGeocodeAPI) getPlace(address string) ([]Place, error) {
return m.places, m.err
}
func (m MockGeocodeAPI) fullURL() string { return "testing" }

func TestFindCoordinates(t *testing.T) {
type args struct {
mockResponse []Place
}
tests := []struct {
name string
args args
want []Place
wantErr error
}{
{
name: "It delegates to the geocoder api and returns the response and error",
args: args{mockResponse: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}}},
want: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockGeocodeAPI := MockGeocodeAPI{places: tt.args.mockResponse}
geocoderClient := &GeocoderClient{geocoder: mockGeocodeAPI}
location, _ := geocoderClient.FindCoordinates("test address")
assert.Equal(t, tt.want, location)

})
}
}
90 changes: 0 additions & 90 deletions geocoder/geocoder.go

This file was deleted.

25 changes: 25 additions & 0 deletions geocoder/geocoder_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package geocoder

import (
"os"
)

const defualtBaseURL = "https://geocode.maps.co"
const searchEndpoint = "/search"

type GeocoderClient struct {
geocoder Geocoder
}

func NewGeocoderClient(geocoder Geocoder) *GeocoderClient {
if geocoder == nil {
geocoder = &GeocodeAPI{baseURL: defualtBaseURL, endpoint: searchEndpoint, apiKey: os.Getenv("GEOCODE_API_KEY")}
}
return &GeocoderClient{geocoder: geocoder}
}

func (g *GeocoderClient) FindCoordinates(address string) ([]Place, error) {
var places []Place
places, err := g.geocoder.getPlace(address)
return places, err
}
44 changes: 44 additions & 0 deletions geocoder/geocoder_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package geocoder

import (
"testing"

"github.com/stretchr/testify/assert"
)

type MockGeocode struct {
places []Place
err error
}

func (m MockGeocode) getPlace(address string) ([]Place, error) {
return m.places, m.err
}
func (m MockGeocode) fullURL() string { return "testing" }

func TestFindCoordinates(t *testing.T) {
type args struct {
mockResponse []Place
}
tests := []struct {
name string
args args
want []Place
wantErr error
}{
{
name: "It delegates to the geocoder api and returns the response and error",
args: args{mockResponse: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}}},
want: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockGeocodeAPI := MockGeocode{places: tt.args.mockResponse}
geocoderClient := &GeocoderClient{geocoder: mockGeocodeAPI}
location, _ := geocoderClient.FindCoordinates("test address")
assert.Equal(t, tt.want, location)

})
}
}
12 changes: 12 additions & 0 deletions geocoder/geocoder_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package geocoder

type Geocoder interface {
getPlace(address string) ([]Place, error)
fullURL() string
}

type Place struct {
Latitude string `json:"lat"`
Longitude string `json:"lon"`
DisplayName string `json:"display_name"`
}

0 comments on commit a8cf11d

Please sign in to comment.