diff --git a/test/helpers/integration_test_helper.py b/test/helpers/integration_test_helper.py index 7fdcd38..df050de 100644 --- a/test/helpers/integration_test_helper.py +++ b/test/helpers/integration_test_helper.py @@ -2,6 +2,29 @@ import os import requests import json +from nose.tools import assert_true + + +# From https://www.python.org/dev/peps/pep-0485/#proposed-implementation +def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): + return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) + + +def assert_close_enough(xy_a, xy_b, rel_tol=0.0001, abs_tol=0.0005): + """ + Asserts that the given points are "close enough", in a square. + :param xy_a: Array of 2 elements, X and Y. + :param xy_b: Array of 2 elements, X and Y. + :param rel_tol: Relative tolerance. Default: 0.001 (0.1%). + :param abs_tol: Absolute tolerance. Default: 0.0005. + """ + + for i in [0, 1]: + assert_true(isclose(xy_a[i], xy_b[i], rel_tol, abs_tol), + "Coord {} error: {} and {} are not closer than {}, {}".format( + i, xy_a[i], xy_b[i], rel_tol, abs_tol + )) + class IntegrationTestHelper: @@ -34,3 +57,5 @@ class IntegrationTestHelper: @classmethod def execute_query(cls, sql_api_url, query): return cls.execute_query_raw(sql_api_url, query)['rows'][0] + + diff --git a/test/integration/test_street_functions.py b/test/integration/test_street_functions.py index 45bda56..420e6a2 100644 --- a/test/integration/test_street_functions.py +++ b/test/integration/test_street_functions.py @@ -5,7 +5,7 @@ from unittest import TestCase from nose.tools import assert_raises from nose.tools import assert_not_equal, assert_equal from ..helpers.integration_test_helper import IntegrationTestHelper - +from ..helpers.integration_test_helper import assert_close_enough class TestStreetFunctionsSetUp(TestCase): @@ -104,7 +104,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): 1: self.fixture_points['Plaza Mayor, Valladolid'], 2: self.fixture_points['Paseo Zorrilla, Valladolid'] } - assert_equal(self._x_y_by_cartodb_id(response), points_by_cartodb_id) + self.assert_close_points(self._x_y_by_cartodb_id(response), points_by_cartodb_id) def test_empty_columns(self): query = "select *, st_x(the_geom), st_y(the_geom) " \ @@ -115,7 +115,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): "'address', '''''', '''''', '''''')" response = self._run_authenticated(query) - assert_equal(self._x_y_by_cartodb_id(response)[1], + assert_close_enough(self._x_y_by_cartodb_id(response)[1], self.fixture_points['1901 amphitheatre parkway']) def test_null_columns(self): @@ -127,7 +127,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): "'address')" response = self._run_authenticated(query) - assert_equal(self._x_y_by_cartodb_id(response)[1], + assert_close_enough(self._x_y_by_cartodb_id(response)[1], self.fixture_points['1901 amphitheatre parkway']) def test_batching(self): @@ -146,7 +146,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): 2: self.fixture_points['1901 amphitheatre parkway'], 3: self.fixture_points['1902 amphitheatre parkway'], } - assert_equal(self._x_y_by_cartodb_id(response), points_by_cartodb_id) + self.assert_close_points(self._x_y_by_cartodb_id(response), points_by_cartodb_id) def test_city_column_geocoding(self): query = "select *, st_x(the_geom), st_y(the_geom) " \ @@ -164,7 +164,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): 1: self.fixture_points['Valladolid'], 2: self.fixture_points['Madrid'] } - assert_equal(self._x_y_by_cartodb_id(response), points_by_cartodb_id) + self.assert_close_points(self._x_y_by_cartodb_id(response), points_by_cartodb_id) def test_free_text_geocoding(self): query = "select *, st_x(the_geom), st_y(the_geom) " \ @@ -176,7 +176,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): "'''Logroño, La Rioja, Spain''')" response = self._run_authenticated(query) - assert_equal(self._x_y_by_cartodb_id(response)[1], + assert_close_enough(self._x_y_by_cartodb_id(response)[1], self.fixture_points['Logroño, Spain']) def test_templating_geocoding(self): @@ -195,7 +195,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): 1: self.fixture_points['Logroño, Spain'], 2: self.fixture_points['Logroño, Argentina'] } - assert_equal(self._x_y_by_cartodb_id(response), points_by_cartodb_id) + self.assert_close_points(self._x_y_by_cartodb_id(response), points_by_cartodb_id) def test_template_with_two_columns_geocoding(self): query = "SELECT cartodb_id, st_x(the_geom), st_y(the_geom) from " \ @@ -212,7 +212,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): 1: self.fixture_points['Valladolid, Mexico'], 2: self.fixture_points['Valladolid, Spain'] } - assert_equal(self._x_y_by_cartodb_id(response), points_by_cartodb_id) + self.assert_close_points(self._x_y_by_cartodb_id(response), points_by_cartodb_id) def test_large_batches(self): """ @@ -245,3 +245,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): return {r['cartodb_id']: [r['st_x'], r['st_y']] for r in response['rows']} + @staticmethod + def assert_close_points(points_a_by_cartodb_id, points_b_by_cartodb_id): + for cartodb_id, point in points_a_by_cartodb_id.iteritems(): + assert_close_enough(point, points_b_by_cartodb_id[cartodb_id])