Skip to content

Commit

Permalink
Fix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
taliaga committed Jan 24, 2018
1 parent 83c18f8 commit 21aff7b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions geocoding/geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
a point but don't necessary have a street + postOfficeBoxNumber. For example,
"Eiffel Tower, Paris".
"""
import json
import geocoder
import json
import logging
import requests

Expand Down Expand Up @@ -157,7 +157,7 @@ def add_location(entity, raise_error=False, session=None, cache=None):
return entity

if cache:
cache.put(key, loc)
cache.put(key, json.dumps(loc))

return _do_add_location(entity, loc)

Expand Down
39 changes: 25 additions & 14 deletions geocoding/tests/test_geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,35 @@ def test_multiple_entities(air_quality_observed):
assert len(r) == 2


def test_caching(air_quality_observed):
def test_caching(air_quality_observed, monkeypatch):
air_quality_observed.pop('location')

air_quality_observed['address']['value'] = {
"streetAddress": "Acolman",
"postOfficeBoxNumber": "22",
"addressLocality": "Ciudad de México",
"addressCountry": "MX",
"streetAddress": "IJzerlaan",
"postOfficeBoxNumber": "18",
"addressLocality": "Antwerpen",
"addressCountry": "BE",
}

from geocoding.geocache import GeoCodingCache
cache = GeoCodingCache(REDIS_HOST, REDIS_PORT)
value = {'type': 'Point', 'coordinates': [-98.9109537, 19.6389474]}
cache.put('Acolman 22 , Ciudad de México , MX', json.dumps(value))

r = geocoding.add_location(air_quality_observed, cache=cache)
assert r is air_quality_observed

assert 'location' in r
assert r['location']['type'] == 'geo:json'
assert r['location']['value'] == value
assert len(cache.redis.keys('*')) == 0

try:
r = geocoding.add_location(air_quality_observed, cache=cache)
assert r is air_quality_observed
assert 'location' in r
assert r['location']['type'] == 'geo:point'
assert len(cache.redis.keys('*')) == 1

# Make sure no external calls are made
monkeypatch.delattr("requests.sessions.Session.request")

r.pop('location')
r = geocoding.add_location(air_quality_observed, cache=cache)
assert 'location' in r
assert r['location']['type'] == 'geo:point'
assert len(cache.redis.keys('*')) == 1

finally:
cache.redis.flushall()

0 comments on commit 21aff7b

Please sign in to comment.