<commit_before>import os
import yaml
import unittest
from .. import greengraph
from mock import Mock, patch

YAMLFILE = 'samplesGreengraph.yaml'

'''
Class for testing Greengraph
'''
class TestGreengraph(unittest.TestCase):
    
    def setUp(self):
        '''
        Setting up some attributes
        '''
        self.YAMLFILE = 'samplesGreengraph.yaml'
        self.ALLOWED_ERROR = 0.00001
        '''
        Loading fixtures from samplesGreengraph.yaml file
        '''
        with open(os.path.join(os.path.dirname(__file__), 'fixtures', self.YAMLFILE)) as fixture_file:
            self.fixtures = yaml.load(fixture_file)

    '''
    Mock side effects for geocode based on values in self.fixtures
    '''
    def geocode_side_effect(self, place, exactly_one=False):
        from geopy.point import Point
        from geopy.location import Location

        if place in self.fixtures['geocode']:
            '''
            If it's not one of the locations defined in fixtures
            '''
            ans = self.fixtures['geocode'][place]
            return [Location(place, Point(ans['lat'], ans['long']))]
        else:
            '''
            Otherwise return some place in Antarctica
            '''
            return [Location((37.3890924, -5.9844589, 0.0))]

    '''
    Testing geolocate
    '''
    def test_geolocate(self):
        '''
        Mock function for geocode method in GoogleV3
        '''
        print "test_geolocate"
        with patch.object(greengraph.geopy.geocoders.GoogleV3, 'geocode',
                side_effect = self.geocode_side_effect) as mock_geocode:
            '''
            Context with mocked geocode
            '''
            gg = greengraph.Greengraph('Bogota', 'Montevideo')

            '''
            Test if geocode function is called with the expected arguments 
            and that geolocate returns the correct answer provided the
            assumed answer from geocode.
            '''
            for location, answer in self.fixtures['geocode'].iteritems():
                ans = gg.geolocate(location)
                mock_geocode.assert_called_with(location, exactly_one=False)
                assert(abs(ans[0] - answer["lat"]) < self.ALLOWED_ERROR)
                assert(abs(ans[1] - answer["long"]) < self.ALLOWED_ERROR)
    
    '''
    Testing location_sequence
    '''
    def test_location_sequence(self):
        from numpy import array, array_equal
        gg = greengraph.Greengraph('Lima', 'Caracas')

        print "test_location_sequence"
        for test_case in self.fixtures['location_sequence']:
            print test_case
            start_coords = self.fixtures['geocode'][test_case['start']]
            end_coords = self.fixtures['geocode'][test_case['end']]
            print start_coords
            ans = gg.location_sequence(
                    (start_coords['lat'], start_coords['long']),
                    (end_coords['lat'], end_coords['long']),
                    test_case['steps'])<commit_msg>tidying up some imports<commit_after>import yaml
import unittest
from .. import greengraph
from mock import patch

'''
Class for testing Greengraph
'''
class TestGreengraph(unittest.TestCase):
    
    '''
    Setup method
    '''
    def setUp(self):
        from os.path import join, dirname
        '''
        Setting up some attributes
        '''
        self.yamlfile = 'samplesGreengraph.yaml'
        self.eps = 0.00001
        '''
        Loading fixtures from samplesGreengraph.yaml file
        '''
        with open(join(dirname(__file__), 'fixtures', self.yamlfile)) as fixture_file:
            self.fixtures = yaml.load(fixture_file)

    '''
    Mock side effects for geocode based on values in self.fixtures
    '''
    def geocode_side_effect(self, place, exactly_one=False):
        from geopy.point import Point
        from geopy.location import Location

        if place in self.fixtures['geocode']:
            '''
            If it's not one of the locations defined in fixtures
            '''
            ans = self.fixtures['geocode'][place]
            return [Location(place, Point(ans['lat'], ans['long']))]
        else:
            '''
            Otherwise return some place in Antarctica
            '''
            return [Location((37.3890924, -5.9844589, 0.0))]

    '''
    Testing geolocate
    '''
    def test_geolocate(self):
        '''
        Mock function for geocode method in GoogleV3
        '''
        print "test_geolocate"
        with patch.object(greengraph.geopy.geocoders.GoogleV3, 'geocode',
                side_effect = self.geocode_side_effect) as mock_geocode:
            '''
            Context with mocked geocode
            '''
            gg = greengraph.Greengraph('Bogota', 'Montevideo')

            '''
            Test if geocode function is called with the expected arguments 
            and that geolocate returns the correct answer provided the
            assumed answer from geocode.
            '''
            for location, answer in self.fixtures['geocode'].iteritems():
                ans = gg.geolocate(location)
                mock_geocode.assert_called_with(location, exactly_one=False)
                assert(abs(ans[0] - answer["lat"]) < self.eps)
                assert(abs(ans[1] - answer["long"]) < self.eps)
    
    '''
    Testing location_sequence
    '''
    def test_location_sequence(self):
        from numpy import array, array_equal

        gg = greengraph.Greengraph('Lima', 'Caracas')
        print "test_location_sequence"
        for test_case in self.fixtures['location_sequence']:
            print test_case
            start_coords = self.fixtures['geocode'][test_case['start']]
            end_coords = self.fixtures['geocode'][test_case['end']]
            print start_coords
            ans = gg.location_sequence(
                    (start_coords['lat'], start_coords['long']),
                    (end_coords['lat'], end_coords['long']),
                    test_case['steps'])
{'fraction_non_alphanumeric': 0.1050266311584554, 'fraction_numerical': 0.009653794940079893, 'mean_word_length': 2.0783811475409837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '106289', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1676, 'n_words': 396}