<commit_before># 
# Copyright 2014 Google Inc. All rights reserved.
# 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# 

"""Tests for the geocoding module."""

import unittest
import datetime
import responses

import test as _test
import googlemaps

class GeocodingTest(_test.TestCase):

    def setUp(self):
        self.key = 'AIzaasdf'
        self.ctx = googlemaps.Context(self.key)

    @responses.activate
    def test_simple_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Sydney')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&address=Sydney' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (-33.8674869, 151.2069902))

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=-33.867487%%2C151.206990&key=%s' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocoding_the_googleplex(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, '1600 Amphitheatre Parkway, '
                                  'Mountain View, CA')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&address=1600+Amphitheatre+Parkway%%2C+Mountain'
                            '+View%%2C+CA' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_bounds(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Winnetka',
                                  bounds={'southwest': (34.172684, -118.604794),
                                          'northeast':(34.236144, -118.500938)})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'bounds=34.172684%%2C-118.604794%%7C34.236144%%2C'
                            '-118.500938&key=%s&address=Winnetka' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_region_biasing(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Toledo', region='es')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'region=es&key=%s&address=Toledo' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_component_filter(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'santa cruz', 
            components={'country': 'ES'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=country%%3AES&address=santa+cruz' % 
                            self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_multiple_component_filters(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Torun', 
            components={'administrative_area': 'TX','country': 'US'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=administrative_area%%3ATX%%7C'
                            'country%%3AUS&address=Torun' % self.key,
                            responses.calls[0].request.url)


    @responses.activate
    def test_geocode_with_just_components(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 
            components={'route': 'Annegatan',
                        'administrative_area': 'Helsinki', 
                        'country': 'Findland'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=administrative_area%%3AHelsinki'
                            '%%7Ccountry%%3AFindland%%7Croute%%3AAnnegatan' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_simple_reverse_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452))

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&key=%s' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_restricted_by_type(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452),
                                          location_type='ROOFTOP',
                                          result_type='street_address')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&result_type=street_address&'
                            'key=%s&location_type=ROOFTOP' % self.key,
                             responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_multiple_location_types(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452),
                                          location_type=['ROOFTOP',
                                                         'RANGE_INTERPOLATED'],
                                          result_type='street_address')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&result_type=street_address&'
                            'key=%s&location_type=ROOFTOP%%7CRANGE_INTERPOLATED' %
                            self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_multiple_result_types(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')<commit_msg>fix spacing<commit_after># 
# Copyright 2014 Google Inc. All rights reserved.
# 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# 

"""Tests for the geocoding module."""

import unittest
import datetime
import responses

import test as _test
import googlemaps

class GeocodingTest(_test.TestCase):

    def setUp(self):
        self.key = 'AIzaasdf'
        self.ctx = googlemaps.Context(self.key)

    @responses.activate
    def test_simple_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Sydney')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&address=Sydney' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (-33.8674869, 151.2069902))

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=-33.867487%%2C151.206990&key=%s' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocoding_the_googleplex(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, '1600 Amphitheatre Parkway, '
                                  'Mountain View, CA')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&address=1600+Amphitheatre+Parkway%%2C+Mountain'
                            '+View%%2C+CA' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_bounds(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Winnetka',
                                  bounds={'southwest': (34.172684, -118.604794),
                                          'northeast':(34.236144, -118.500938)})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'bounds=34.172684%%2C-118.604794%%7C34.236144%%2C'
                            '-118.500938&key=%s&address=Winnetka' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_region_biasing(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Toledo', region='es')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'region=es&key=%s&address=Toledo' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_component_filter(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'santa cruz', 
            components={'country': 'ES'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=country%%3AES&address=santa+cruz' % 
                            self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_geocode_with_multiple_component_filters(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 'Torun', 
            components={'administrative_area': 'TX','country': 'US'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=administrative_area%%3ATX%%7C'
                            'country%%3AUS&address=Torun' % self.key,
                            responses.calls[0].request.url)


    @responses.activate
    def test_geocode_with_just_components(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.geocode(self.ctx, 
            components={'route': 'Annegatan',
                        'administrative_area': 'Helsinki', 
                        'country': 'Findland'})

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'key=%s&components=administrative_area%%3AHelsinki'
                            '%%7Ccountry%%3AFindland%%7Croute%%3AAnnegatan' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_simple_reverse_geocode(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452))

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&key=%s' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_restricted_by_type(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452),
                                          location_type='ROOFTOP',
                                          result_type='street_address')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&result_type=street_address&'
                            'key=%s&location_type=ROOFTOP' % self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_multiple_location_types(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')

        results = googlemaps.reverse_geocode(self.ctx, (40.714224, -73.961452),
                                          location_type=['ROOFTOP',
                                                         'RANGE_INTERPOLATED'],
                                          result_type='street_address')

        self.assertEqual(1, len(responses.calls))
        self.assertURLEqual('https://maps.googleapis.com/maps/api/geocode/json?'
                            'latlng=40.714224%%2C-73.961452&result_type=street_address&'
                            'key=%s&location_type=ROOFTOP%%7CRANGE_INTERPOLATED' %
                            self.key,
                            responses.calls[0].request.url)

    @responses.activate
    def test_reverse_geocode_multiple_result_types(self):
        responses.add(responses.GET, 
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      body='{"status":"OK","results":[]}',
                      status=200,
                      content_type='application/json')
{'fraction_non_alphanumeric': 0.12886829122439827, 'fraction_numerical': 0.029202172721383365, 'mean_word_length': 1.7653300261816178, 'pattern_counts': {'":': 48, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 46, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2813162', 'n_tokens_mistral': 6041, 'n_tokens_neox': 5595, 'n_words': 686}