Skip to content

Commit

Permalink
Adding in some more tests for google geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kellydunn committed May 10, 2015
1 parent 9a8e2e0 commit af0eeb9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 18 deletions.
54 changes: 36 additions & 18 deletions google_geocoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,13 @@ func (g *GoogleGeocoder) Request(params string) ([]byte, error) {

// Geocodes the passed in query string and returns a pointer to a new Point struct.
// Returns an error if the underlying request cannot complete.
func (g *GoogleGeocoder) Geocode(query string) (*Point, error) {
url_safe_query := url.QueryEscape(query)

var queryStr = bytes.NewBufferString("")
_, err := queryStr.WriteString(fmt.Sprintf("address=%s", url_safe_query))
func (g *GoogleGeocoder) Geocode(address string) (*Point, error) {
queryStr, err := googleGeocodeQueryStr(address)
if err != nil {
return nil, err
}

if GoogleAPIKey != "" {
_, err := queryStr.WriteString(fmt.Sprintf("&key=%s", GoogleAPIKey))
if err != nil {
return nil, err
}
}

data, err := g.Request(queryStr.String())
data, err := g.Request(queryStr)
if err != nil {
return nil, err
}
Expand All @@ -126,11 +116,11 @@ func (g *GoogleGeocoder) Geocode(query string) (*Point, error) {
return point, nil
}

// Reverse geocodes the pointer to a Point struct and returns the first address that matches
// or returns an error if the underlying request cannot complete.
func (g *GoogleGeocoder) ReverseGeocode(p *Point) (string, error) {
func googleGeocodeQueryStr(address string) (string, error) {
url_safe_query := url.QueryEscape(address)

var queryStr = bytes.NewBufferString("")
_, err := queryStr.WriteString(fmt.Sprintf("latlng=%f,%f", p.lat, p.lng))
_, err := queryStr.WriteString(fmt.Sprintf("address=%s", url_safe_query))
if err != nil {
return "", err
}
Expand All @@ -142,7 +132,18 @@ func (g *GoogleGeocoder) ReverseGeocode(p *Point) (string, error) {
}
}

data, err := g.Request(queryStr.String())
return queryStr.String(), err
}

// Reverse geocodes the pointer to a Point struct and returns the first address that matches
// or returns an error if the underlying request cannot complete.
func (g *GoogleGeocoder) ReverseGeocode(p *Point) (string, error) {
queryStr, err := googleReverseGeocodeQueryStr(p)
if err != nil {
return "", err
}

data, err := g.Request(queryStr)
if err != nil {
return "", err
}
Expand All @@ -159,3 +160,20 @@ func (g *GoogleGeocoder) ReverseGeocode(p *Point) (string, error) {

return res.Results[0].FormattedAddress, err
}

func googleReverseGeocodeQueryStr(p *Point) (string, error) {
var queryStr = bytes.NewBufferString("")
_, err := queryStr.WriteString(fmt.Sprintf("latlng=%f,%f", p.lat, p.lng))
if err != nil {
return "", err
}

if GoogleAPIKey != "" {
_, err := queryStr.WriteString(fmt.Sprintf("&key=%s", GoogleAPIKey))
if err != nil {
return "", err
}
}

return queryStr.String(), err
}
55 changes: 55 additions & 0 deletions google_geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"testing"
"fmt"
)

func TestSetGoogleAPIKey(t * testing.T) {
Expand All @@ -21,6 +22,60 @@ func TestSetGoogleGeocodeURL(t *testing.T) {
}
}

func TestGoogleGeocoderQueryStr(t *testing.T) {
// Empty API Key
SetGoogleAPIKey("")
address := "123 fake st"
res, err := googleGeocodeQueryStr(address)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}

expected := "address=123+fake+st"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}

// Set api key to some value
SetGoogleAPIKey("foo")
res, err = googleGeocodeQueryStr(address)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}

expected = "address=123+fake+st&key=foo"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}

func TestGoogleReverseGeocoderQueryStr(t *testing.T) {
// Empty API Key
SetGoogleAPIKey("")
p := &Point{lat:123.45, lng:56.78}
res, err := googleReverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}

expected := "latlng=123.450000,56.780000"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}

// Set api key to some value
SetGoogleAPIKey("foo")
res, err = googleReverseGeocodeQueryStr(p)
if err != nil {
t.Errorf("Error creating query string: %v", err)
}

expected = "latlng=123.450000,56.780000&key=foo"
if res != expected {
t.Errorf(fmt.Sprintf("Mismatched query string. Expected: %s. Actual: %s", expected, res))
}
}

func GetMockResponse(s string) ([]byte, error) {
dataPath := path.Join(s)
_, readErr := os.Stat(dataPath)
Expand Down

0 comments on commit af0eeb9

Please sign in to comment.