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

    air_quality_observed['address']['value'] = {
        "streetAddress": "IJzerlaan",
        "postOfficeBoxNumber": "18",
        "addressLocality": "Antwerpen",
        "addressCountry": "BE",
    }

    from geocoding.geocache import temp_geo_cache
    cache = next(temp_geo_cache(REDIS_HOST, REDIS_PORT))
    assert len(cache.redis.keys('*')) == 0

    try:
        r = geocoding.add_location(air_quality_observed, cache=cache)
        assert r is air_quality_observed
        assert_lon_lat(r, expected_lon=51.23, expected_lat=4.42)
        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_lon_lat(r, expected_lon=51.23, expected_lat=4.42)
        assert len(cache.redis.keys('*')) == 1

    finally:
        cache.redis.flushall()
示例#2
0
def test_caching(air_quality_observed, monkeypatch):
    air_quality_observed.pop('location')

    air_quality_observed['address']['value'] = {
        "streetAddress": "IJzerlaan",
        "postOfficeBoxNumber": "18",
        "addressLocality": "Antwerpen",
        "addressCountry": "BE",
    }

    from geocoding.geocache import temp_geo_cache
    cache = next(temp_geo_cache(REDIS_HOST, REDIS_PORT))
    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'
        lon, lat = r['location']['value'].split(',')
        assert float(lon) == pytest.approx(51.2358357, abs=1e-2)
        assert float(lat) == pytest.approx(4.4201911, abs=1e-2)
        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'
        lon, lat = r['location']['value'].split(',')
        assert float(lon) == pytest.approx(51.2358357, abs=1e-2)
        assert float(lat) == pytest.approx(4.4201911, abs=1e-2)
        assert len(cache.redis.keys('*')) == 1

    finally:
        cache.redis.flushall()