Skip to content

Commit

Permalink
Merge pull request #254 from DenisCarriere/fix_tests_with_mocks
Browse files Browse the repository at this point in the history
added mocks for test_geonames
  • Loading branch information
DenisCarriere committed Aug 2, 2017
2 parents 6281796 + 887e4b4 commit a681e8a
Show file tree
Hide file tree
Showing 14 changed files with 775 additions and 42 deletions.
4 changes: 2 additions & 2 deletions geocoder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def _connect(self, **kwargs):
if self.status_code == 200:
try:
self.content = r.json()
except:
except Exception as err:
self.status_code = 400
self.error = 'ERROR - JSON Corrupted'
self.error = 'ERROR - JSON Corrupted: %s' % str(err)
self.content = r.content

def _initialize(self, **kwargs):
Expand Down
7 changes: 4 additions & 3 deletions geocoder/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import six
from geocoder.base import Base
from geocoder.keys import google_key, google_client, google_client_secret
from collections import OrderedDict


class Google(Base):
Expand Down Expand Up @@ -63,11 +64,11 @@ def _location_init(self, location, **kwargs):
def _encode_params(self):
# turn non-empty params into sorted list in order to maintain signature validity.
# Requests will honor the order.
self.params = sorted([(k, v) for (k, v) in self.params.items() if v])
ordered_params = sorted([(k, v) for (k, v) in self.params.items() if v])
self.params = OrderedDict(ordered_params)

# the signature parameter needs to come in the end of the url
signature = ['signature', self._sign_url(self.url, self.params, self.client_secret)]
self.params.append(signature)
self.params['signature'] = self._sign_url(self.url, ordered_params, self.client_secret)

def _sign_url(self, base_url=None, params=None, client_secret=None):
""" Sign a request URL with a Crypto Key.
Expand Down
2 changes: 1 addition & 1 deletion geocoder/osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,5 @@ def place_rank(self):


if __name__ == '__main__':
g = Osm("Ottawa, ON")
g = Osm('Ottawa, Ontario')
g.debug()
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ratelim==0.1.6
requests==2.18.1
six==1.10.0
urllib3==1.21.1
requests-mock==1.3.0
20 changes: 20 additions & 0 deletions tests/results/geonames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"totalResultsCount": 142,
"geonames": [{
"adminCode1": "08",
"lng": "-75.69812",
"geonameId": 6094817,
"toponymName": "Ottawa",
"countryId": "6251999",
"fcl": "P",
"population": 812129,
"countryCode": "CA",
"name": "Ottawa",
"fclName": "city, village,...",
"countryName": "Canada",
"fcodeName": "capital of a political entity",
"adminName1": "Ontario",
"lat": "45.41117",
"fcode": "PPLC"
}]
}
39 changes: 39 additions & 0 deletions tests/results/geonames_children.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"totalResultsCount": 2,
"geonames": [
{
"adminCode1": "10",
"lng": "-75.76583",
"geonameId": 5901584,
"toponymName": "Birch Manor",
"countryId": "6251999",
"fcl": "P",
"population": 0,
"countryCode": "CA",
"name": "Birch Manor",
"fclName": "city, village,...",
"countryName": "Canada",
"fcodeName": "section of populated place",
"adminName1": "Quebec",
"lat": "45.42472",
"fcode": "PPLX"
},
{
"adminCode1": "10",
"lng": "-75.58833",
"geonameId": 6162703,
"toponymName": "Templeton-Est",
"countryId": "6251999",
"fcl": "P",
"population": 0,
"countryCode": "CA",
"name": "Templeton-Est",
"fclName": "city, village,...",
"countryName": "Canada",
"fcodeName": "section of populated place",
"adminName1": "Quebec",
"lat": "45.49389",
"fcode": "PPLX"
}
]
}
81 changes: 81 additions & 0 deletions tests/results/geonames_hierarchy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"geonames": [
{
"lng": "0",
"geonameId": 6295630,
"name": "Earth",
"fclName": "parks,area, ...",
"toponymName": "Earth",
"fcodeName": "area",
"adminName1": "",
"lat": "0",
"fcl": "L",
"fcode": "AREA",
"population": 6814400000
},
{
"lng": "-100.54688",
"geonameId": 6255149,
"name": "North America",
"fclName": "parks,area, ...",
"toponymName": "North America",
"fcodeName": "continent",
"adminName1": "",
"lat": "46.07323",
"fcl": "L",
"fcode": "CONT",
"population": 0
},
{
"adminCode1": "00",
"lng": "-113.64258",
"geonameId": 6251999,
"toponymName": "Canada",
"countryId": "6251999",
"fcl": "A",
"population": 33679000,
"countryCode": "CA",
"name": "Canada",
"fclName": "country, state, region,...",
"countryName": "Canada",
"fcodeName": "independent political entity",
"adminName1": "",
"lat": "60.10867",
"fcode": "PCLI"
},
{
"adminCode1": "08",
"lng": "-84.49983",
"geonameId": 6093943,
"toponymName": "Ontario",
"countryId": "6251999",
"fcl": "A",
"population": 12861940,
"countryCode": "CA",
"name": "Ontario",
"fclName": "country, state, region,...",
"countryName": "Canada",
"fcodeName": "first-order administrative division",
"adminName1": "Ontario",
"lat": "49.25014",
"fcode": "ADM1"
},
{
"adminCode1": "08",
"lng": "-75.69812",
"geonameId": 6094817,
"toponymName": "Ottawa",
"countryId": "6251999",
"fcl": "P",
"population": 812129,
"countryCode": "CA",
"name": "Ottawa",
"fclName": "city, village,...",
"countryName": "Canada",
"fcodeName": "capital of a political entity",
"adminName1": "Ontario",
"lat": "45.41117",
"fcode": "PPLC"
}
]
}
74 changes: 74 additions & 0 deletions tests/results/google.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"results": [
{
"address_components": [
{
"long_name": "Ottawa",
"short_name": "Ottawa",
"types": [
"locality",
"political"
]
},
{
"long_name": "Ottawa Division",
"short_name": "Ottawa Division",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Ontario",
"short_name": "ON",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Canada",
"short_name": "CA",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Ottawa, ON, Canada",
"geometry": {
"bounds": {
"northeast": {
"lat": 45.5375801,
"lng": -75.2465979
},
"southwest": {
"lat": 44.962733,
"lng": -76.3539158
}
},
"location": {
"lat": 45.4215296,
"lng": -75.69719309999999
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 45.5375801,
"lng": -75.2465979
},
"southwest": {
"lat": 44.962733,
"lng": -76.3539158
}
}
},
"place_id": "ChIJrxNRX7IFzkwR7RXdMeFRaoo",
"types": [
"locality",
"political"
]
}
],
"status": "OK"
}

0 comments on commit a681e8a

Please sign in to comment.