Skip to content

Commit

Permalink
BANFrance: a bit of nitpicking + loosen assertions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaEsmukov committed Dec 1, 2018
1 parent f317e26 commit 7729293
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
24 changes: 14 additions & 10 deletions geopy/geocoders/banfrance.py
Expand Up @@ -12,6 +12,7 @@ class BANFrance(Geocoder):
Documentation at:
https://adresse.data.gouv.fr/api
.. versionadded:: 1.18.0
"""

geocode_path = '/search'
Expand All @@ -20,18 +21,21 @@ class BANFrance(Geocoder):
def __init__(
self,
domain='api-adresse.data.gouv.fr',
format_string=None,
scheme=None,
timeout=DEFAULT_SENTINEL,
proxies=DEFAULT_SENTINEL,
user_agent=None,
format_string=None,
ssl_context=DEFAULT_SENTINEL,
):
"""
:param str domain: Currently it is ``'api-adresse.data.gouv.fr'``, can
be changed for testing purposes.
:param str format_string:
See :attr:`geopy.geocoders.options.default_format_string`.
:param str scheme:
See :attr:`geopy.geocoders.options.default_scheme`.
Expand All @@ -44,9 +48,6 @@ def __init__(
:param str user_agent:
See :attr:`geopy.geocoders.options.default_user_agent`.
:param str format_string:
See :attr:`geopy.geocoders.options.default_format_string`.
:type ssl_context: :class:`ssl.SSLContext`
:param ssl_context:
See :attr:`geopy.geocoders.options.default_ssl_context`.
Expand All @@ -72,7 +73,7 @@ def __init__(
def geocode(
self,
query,
limit=5,
limit=None,
exactly_one=True,
timeout=DEFAULT_SENTINEL,
):
Expand Down Expand Up @@ -101,9 +102,11 @@ def geocode(

params = {
'q': self.format_string % query,
'limit': limit
}

if limit is not None:
params['limit'] = limit

url = "?".join((self.geocode_api, urlencode(params)))

logger.debug("%s.geocode: %s", self.__class__.__name__, url)
Expand Down Expand Up @@ -142,9 +145,10 @@ def reverse(
lat, lng = self._coerce_point_to_string(query).split(',')
except ValueError:
raise ValueError("Must be a coordinate pair or Point")

params = {
'lat': lat,
'lng': lng
'lng': lng,
}

url = "?".join((self.reverse_api, urlencode(params)))
Expand All @@ -154,7 +158,7 @@ def reverse(
)

@staticmethod
def _parse_code(feature):
def _parse_feature(feature):
# Parse each resource.
latitude = feature.get('geometry', {}).get('coordinates', [])[1]
longitude = feature.get('geometry', {}).get('coordinates', [])[0]
Expand All @@ -170,6 +174,6 @@ def _parse_json(self, response, exactly_one):
if not len(features):
return None
if exactly_one:
return self._parse_code(features[0])
return self._parse_feature(features[0])
else:
return [self._parse_code(feature) for feature in features]
return [self._parse_feature(feature) for feature in features]
24 changes: 6 additions & 18 deletions test/geocoders/banfrance.py
Expand Up @@ -6,7 +6,6 @@ class BANFranceTestCase(GeocoderTestBase):

@classmethod
def setUpClass(cls):
cls.delta = 0.04
cls.geocoder = BANFrance(timeout=10)

def test_user_agent_custom(self):
Expand All @@ -16,31 +15,20 @@ def test_user_agent_custom(self):
self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')

def test_geocode_with_address(self):
"""
banfrance.geocode Address
"""

self.geocode_run(
location = self.geocode_run(
{"query": "Camp des Landes, 41200 VILLEFRANCHE-SUR-CHER"},
{"latitude": 47.293048, "longitude": 1.718985,
"address": "Le Camp des Landes 41200 Villefranche-sur-Cher"},
{"latitude": 47.293048, "longitude": 1.718985},
)
self.assertIn("Camp des Landes", location.address)

def test_reverse(self):
"""
banfrance.reverse
"""

self.reverse_run(
location = self.reverse_run(
{"query": "48.154587,3.221237", "exactly_one": True},
{"latitude": 48.154587, "longitude": 3.221237},
{"address": "15 Rue des Fontaines 89100 Collemiers"}
)
self.assertIn("Rue des Fontaines", location.address)

def test_limit(self):
"""
banfrance.geocode limit
"""
def test_geocode_limit(self):
result = self.geocode_run(
{"query": "8 bd du port", "limit": 2, "exactly_one": False},
{}
Expand Down

0 comments on commit 7729293

Please sign in to comment.