def test_geolocate(): # Test that the geolocate method calls the geocode method of the GoogleV3 # class with the expected parameters with patch.object(geopy.geocoders.GoogleV3, "geocode") as mock_geocode: test_Greengraph = Greengraph(start, end) test_Greengraph.geolocate(start) mock_geocode.assert_called_with(start, exactly_one=False)
def test_geolocate(): ''' Test the geolocate function in the Greengraph() class. A MagicMock object is created for the geocoder. ''' myGreengraph = Greengraph("London","Cambdrige") # defaultResp = ["London",(51.5073509, -0.1277583)] tGeocoder = MagicMock() # tGeocoder = mock.Mock() # attrs = {'geocode.return_value': defaultResp, 'other.side_effect': KeyError} # tGeocoder.configure_mock(**attrs) defaultResp = [] defaultResp.append([]) defaultResp[0].append("London") defaultResp[0].append((51.5073509, -0.1277583)) tGeocoder.geocode.return_value = defaultResp myGreengraph.geocoder = tGeocoder tGeocoder.geocode.assert_not_called() startLocation = myGreengraph.geolocate(myGreengraph.start) # print(startLocation) # testLoc = tGeocoder.geocode("London", exactly_one=False) # print(testLoc) # print(tGeocoder.method_calls) assert_almost_equal(startLocation,(51.5073509, -0.1277583)) tGeocoder.geocode.assert_called_with("London", exactly_one=False)
def test_green_between(mock_geolocate,mock_location,mock_count): mock_location.return_value = [[1,0],[2,0]] with patch.object(Map,'__init__') as mock_get: trail_graph = Greengraph(start,end) mock_get.return_value = None trail_graph.green_between(1) print mock_get.mock_calls mock_get.assert_has_calls([call(1, 0), call(2, 0)])
def test_location_sequence(): with open(os.path.join(os.path.dirname(__file__),'fixtures','location_sequence.yaml')) as fixtures_file: fixtures=yaml.load(fixtures_file) trial_graph = Greengraph(start,end) for fixture in fixtures: steps = fixture.pop('steps') expected = np.asarray(fixture.pop('answer')) answer = trial_graph.location_sequence(fixture.pop('start'),fixture.pop('end'),steps) # cannot assert_equal on arrays, instead loop over elements for step in range(steps): for coord in range(2): assert_equal(expected[step,coord],answer[step,coord])
def test_green_between(mock_count_green, mock_geolocate): # Test Map objects created during green_between method are called with the # expected parameters with patch.object(Greengraph, "location_sequence") as mock_location_sequence: mock_location_sequence.return_value = [ [0., 0.], [1., 1.], [2., 2.], [3., 3.]] expected_calls = [call(0.0, 0.0), call( 1.0, 1.0), call(2.0, 2.0), call(3.0, 3.0)] with patch.object(Map, "__init__") as mock_Map_init: mock_Map_init.return_value = None test_Greengraph = Greengraph(start, end) test_Greengraph.green_between(6) mock_Map_init.assert_has_calls(expected_calls)
def test_location_sequence(): # Test that the location_sequence method computes the steps between two # coordinates correctly test_Greengraph = Greengraph(start, end) with open(os.path.join(os.path.dirname(__file__), "fixtures", "location_sequence.yaml")) as fixtures_file: fixtures = yaml.load(fixtures_file) for fixture in fixtures: expected_results = fixture.pop("result") i = 0 test_sequence = test_Greengraph.location_sequence( (fixture.pop("startlat"), fixture.pop("startlon")), (fixture.pop("endlat"), fixture.pop("endlon")), fixture.pop("steps")) for result in expected_results: assert_equal(test_sequence[i][0], result) assert_equal(test_sequence[i][1], result) i += 1
def process(): parser = ArgumentParser( description="generates a graph of the proportion of green pixels in a series of satellite images between two points." ) parser.add_argument("--from", dest="start", default="London") parser.add_argument("--to", default="Oxford") parser.add_argument("--steps", default=20) parser.add_argument("--out", default="greengraph.jpg") arguments = parser.parse_args() mygraph = Greengraph(arguments.start, arguments.to) data = mygraph.green_between(arguments.steps) plt.plot(data) savefig(arguments.out)
def test_geolocate(): with patch.object(geopy.geocoders.GoogleV3, 'geocode') as mock_get: trail_graph = Greengraph(start,end) trail_graph.geolocate(start) mock_get.assert_called_with('London', exactly_one=False)