-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from manolovl/add-structured-address
Add structured address
- Loading branch information
Showing
31 changed files
with
988 additions
and
440 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
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
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 |
---|---|---|
@@ -1,71 +1,85 @@ | ||
package cached_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/codingsince1985/geo-golang" | ||
"github.com/codingsince1985/geo-golang/cached" | ||
"github.com/codingsince1985/geo-golang/data" | ||
"github.com/patrickmn/go-cache" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var geoCache = cache.New(5*time.Minute, 30*time.Second) | ||
|
||
// geocoder is chained with one data geocoder with address -> location data | ||
// the other has location -> address data | ||
// this will exercise the chained fallback handling | ||
var geocoder = cached.Geocoder( | ||
data.Geocoder( | ||
data.AddressToLocation{ | ||
"Melbourne VIC": geo.Location{Lat: -37.814107, Lng: 144.96328}, | ||
}, | ||
data.LocationToAddress{ | ||
geo.Location{Lat: -37.816742, Lng: 144.964463}: "Melbourne VIC 3000, Australia", | ||
}, | ||
), | ||
geoCache, | ||
var ( | ||
addressFixture = geo.Address{ | ||
FormattedAddress: "64 Elizabeth Street, Melbourne, Victoria 3000, Australia", | ||
} | ||
locationFixture = geo.Location{ | ||
Lat: -37.814107, | ||
Lng: 144.96328, | ||
} | ||
geocoder = cached.Geocoder( | ||
data.Geocoder( | ||
data.AddressToLocation{ | ||
addressFixture: locationFixture, | ||
}, | ||
data.LocationToAddress{ | ||
locationFixture: addressFixture, | ||
}, | ||
), | ||
geoCache, | ||
) | ||
) | ||
|
||
func TestGeocode(t *testing.T) { | ||
location, err := geocoder.Geocode("Melbourne VIC") | ||
location, err := geocoder.Geocode("64 Elizabeth Street, Melbourne, Victoria 3000, Australia") | ||
assert.NoError(t, err) | ||
assert.Equal(t, geo.Location{Lat: -37.814107, Lng: 144.96328}, location) | ||
assert.Equal(t, locationFixture, *location) | ||
} | ||
|
||
func TestReverseGeocode(t *testing.T) { | ||
address, err := geocoder.ReverseGeocode(-37.816742, 144.964463) | ||
address, err := geocoder.ReverseGeocode(locationFixture.Lat, locationFixture.Lng) | ||
assert.NoError(t, err) | ||
assert.True(t, strings.HasSuffix(address, "Melbourne VIC 3000, Australia")) | ||
assert.True(t, strings.HasSuffix(address.FormattedAddress, "Melbourne, Victoria 3000, Australia")) | ||
} | ||
|
||
func TestReverseGeocodeWithNoResult(t *testing.T) { | ||
_, err := geocoder.ReverseGeocode(-37.816742, 164.964463) | ||
assert.Equal(t, err, geo.ErrNoResult) | ||
addr, err := geocoder.ReverseGeocode(1, 2) | ||
assert.Nil(t, err) | ||
assert.Nil(t, addr) | ||
} | ||
|
||
func TestCachedGeocode(t *testing.T) { | ||
mockAddr := geo.Address{ | ||
FormattedAddress: "42, Some Street, Austin, Texas", | ||
} | ||
mock1 := data.Geocoder( | ||
data.AddressToLocation{ | ||
"Austin,TX": geo.Location{Lat: 1, Lng: 2}, | ||
mockAddr: geo.Location{Lat: 1, Lng: 2}, | ||
}, | ||
data.LocationToAddress{}, | ||
) | ||
|
||
c := cached.Geocoder(mock1, geoCache) | ||
|
||
l, err := c.Geocode("Austin,TX") | ||
l, err := c.Geocode("42, Some Street, Austin, Texas") | ||
assert.NoError(t, err) | ||
assert.Equal(t, geo.Location{Lat: 1, Lng: 2}, l) | ||
assert.Equal(t, geo.Location{Lat: 1, Lng: 2}, *l) | ||
|
||
// Should be cached | ||
// TODO: write a mock Cache impl to test cache is being used | ||
l, err = c.Geocode("Austin,TX") | ||
l, err = c.Geocode("42, Some Street, Austin, Texas") | ||
assert.NoError(t, err) | ||
assert.Equal(t, geo.Location{Lat: 1, Lng: 2}, l) | ||
assert.Equal(t, geo.Location{Lat: 1, Lng: 2}, *l) | ||
|
||
_, err = c.Geocode("NOWHERE,TX") | ||
assert.Equal(t, geo.ErrNoResult, err) | ||
addr, err := c.Geocode("NOWHERE,TX") | ||
assert.Nil(t, err) | ||
assert.Nil(t, addr) | ||
} |
Oops, something went wrong.