diff --git a/reverse-geocoding.cabal b/reverse-geocoding.cabal index b716c87..2ea8488 100644 --- a/reverse-geocoding.cabal +++ b/reverse-geocoding.cabal @@ -28,6 +28,14 @@ library hs-source-dirs: src default-language: Haskell2010 +test-suite network-test + default-language: Haskell2010 + type: exitcode-stdio-1.0 + hs-source-dirs: tests + main-is: NetworkTest.hs + build-depends: base, + reverse-geocoding + source-repository head type: git location: https://github.com/jcristovao/reverse-geocoding diff --git a/src/Data/Geolocation/Reverse/Providers.hs b/src/Data/Geolocation/Reverse/Providers.hs index d00890d..9390fb8 100644 --- a/src/Data/Geolocation/Reverse/Providers.hs +++ b/src/Data/Geolocation/Reverse/Providers.hs @@ -22,6 +22,7 @@ import Data.Aeson import Data.Aeson.Types import Control.Monad (join) import qualified Data.Text as T +import Text.Printf import Data.Geolocation.Reverse.Types @@ -41,8 +42,8 @@ openStreetMapUrl (Latitude mlat) (Longitude mlon) = do return $ "http://nominatim.openstreetmap.org/reverse" <> "?format=json" <> "&zoom=18" - <> "&lat=" <> show lat - <> "&lon=" <> show lon + <> "&lat=" <> formatFixed lat + <> "&lon=" <> formatFixed lon getPostCodeText :: Suburb -> Maybe Suburb @@ -77,3 +78,5 @@ openStreetMapParser o = <|?> (o .:? "street") ) <*> o .:? "postcode" + +formatFixed n = printf "%.6f" n diff --git a/tests/NetworkTest.hs b/tests/NetworkTest.hs new file mode 100644 index 0000000..0de09c6 --- /dev/null +++ b/tests/NetworkTest.hs @@ -0,0 +1,15 @@ +import Data.Geolocation.Reverse +import Data.Geolocation.Reverse.Types + +main = do + + -- all we're going to do is get a location and + -- check we don't crash. + l <- getLocationInfoDef (Latitude (Just 5)) (Longitude (Just 5)) + print l + + -- regression test for extremely small numbers that + -- were being rendered using invalid exponential + -- notation. + l2 <- getLocationInfoDef (Latitude (Just 0.0005)) (Longitude (Just 0.0005)) + print l2