From 1397b3fcaffa2d99c91c18535884eab6e84feec9 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Fri, 4 Dec 2015 16:58:28 +0100 Subject: [PATCH 01/10] First iteration to automate smoke tests --- test/fixtures/geocoder_api_test_dataset.csv | 265 ++++++++++++++++++++ test/integration/test_admin0_functions.py | 29 +++ test/run_tests.py | 91 +++++++ 3 files changed, 385 insertions(+) create mode 100644 test/fixtures/geocoder_api_test_dataset.csv create mode 100644 test/integration/test_admin0_functions.py create mode 100644 test/run_tests.py diff --git a/test/fixtures/geocoder_api_test_dataset.csv b/test/fixtures/geocoder_api_test_dataset.csv new file mode 100644 index 0000000..3b65446 --- /dev/null +++ b/test/fixtures/geocoder_api_test_dataset.csv @@ -0,0 +1,265 @@ +iso,name +AF,Afghanistan +AL,Albania +DZ,Algeria +AS,"American Samoa" +AD,Andorra +AO,Angola +AI,Anguilla +AQ,Antarctica +AG,"Antigua and Barbuda" +AR,Argentina +AM,Armenia +AW,Aruba +AU,Australia +AT,Austria +AZ,Azerbaijan +BS,Bahamas +BH,Bahrain +BD,Bangladesh +BB,Barbados +BY,Belarus +BE,Belgium +BZ,Belize +BJ,Benin +BM,Bermuda +BT,Bhutan +BO,Bolivia +BA,"Bosnia and Herzegovina" +BW,Botswana +BV,"Bouvet Island" +BR,Brazil +BQ,"British Antarctic Territory" +IO,"British Indian Ocean Territory" +VG,"British Virgin Islands" +BN,Brunei +BG,Bulgaria +BF,"Burkina Faso" +BI,Burundi +KH,Cambodia +CM,Cameroon +CA,Canada +CT,"Canton and Enderbury Islands" +CV,"Cape Verde" +KY,"Cayman Islands" +CF,"Central African Republic" +TD,Chad +CL,Chile +CN,China +CX,"Christmas Island" +CC,"Cocos [Keeling] Islands" +CO,Colombia +KM,Comoros +CG,"Congo - Brazzaville" +CD,"Congo - Kinshasa" +CK,"Cook Islands" +CR,"Costa Rica" +HR,Croatia +CU,Cuba +CY,Cyprus +CZ,"Czech Republic" +CI,"Côte d’Ivoire" +DK,Denmark +DJ,Djibouti +DM,Dominica +DO,"Dominican Republic" +NQ,"Dronning Maud Land" +DD,"East Germany" +EC,Ecuador +EG,Egypt +SV,"El Salvador" +GQ,"Equatorial Guinea" +ER,Eritrea +EE,Estonia +ET,Ethiopia +FK,"Falkland Islands" +FO,"Faroe Islands" +FJ,Fiji +FI,Finland +FR,France +GF,"French Guiana" +PF,"French Polynesia" +TF,"French Southern Territories" +FQ,"French Southern and Antarctic Territories" +GA,Gabon +GM,Gambia +GE,Georgia +DE,Germany +GH,Ghana +GI,Gibraltar +GR,Greece +GL,Greenland +GD,Grenada +GP,Guadeloupe +GU,Guam +GT,Guatemala +GG,Guernsey +GN,Guinea +GW,Guinea-Bissau +GY,Guyana +HT,Haiti +HM,"Heard Island and McDonald Islands" +HN,Honduras +HK,"Hong Kong SAR China" +HU,Hungary +IS,Iceland +IN,India +ID,Indonesia +IR,Iran +IQ,Iraq +IE,Ireland +IM,"Isle of Man" +IL,Israel +IT,Italy +JM,Jamaica +JP,Japan +JE,Jersey +JT,"Johnston Island" +JO,Jordan +KZ,Kazakhstan +KE,Kenya +KI,Kiribati +KW,Kuwait +KG,Kyrgyzstan +LA,Laos +LV,Latvia +LB,Lebanon +LS,Lesotho +LR,Liberia +LY,Libya +LI,Liechtenstein +LT,Lithuania +LU,Luxembourg +MO,"Macau SAR China" +MK,Macedonia +MG,Madagascar +MW,Malawi +MY,Malaysia +MV,Maldives +ML,Mali +MT,Malta +MH,"Marshall Islands" +MQ,Martinique +MR,Mauritania +MU,Mauritius +YT,Mayotte +FX,"Metropolitan France" +MX,Mexico +FM,Micronesia +MI,"Midway Islands" +MD,Moldova +MC,Monaco +MN,Mongolia +ME,Montenegro +MS,Montserrat +MA,Morocco +MZ,Mozambique +MM,"Myanmar [Burma]" +NA,Namibia +NR,Nauru +NP,Nepal +NL,Netherlands +AN,"Netherlands Antilles" +NT,"Neutral Zone" +NC,"New Caledonia" +NZ,"New Zealand" +NI,Nicaragua +NE,Niger +NG,Nigeria +NU,Niue +NF,"Norfolk Island" +KP,"North Korea" +VD,"North Vietnam" +MP,"Northern Mariana Islands" +NO,Norway +OM,Oman +PC,"Pacific Islands Trust Territory" +PK,Pakistan +PW,Palau +PS,"Palestinian Territories" +PA,Panama +PZ,"Panama Canal Zone" +PG,"Papua New Guinea" +PY,Paraguay +YD,"People's Democratic Republic of Yemen" +PE,Peru +PH,Philippines +PN,"Pitcairn Islands" +PL,Poland +PT,Portugal +PR,"Puerto Rico" +QA,Qatar +RO,Romania +RU,Russia +RW,Rwanda +RE,Réunion +BL,"Saint Barthélemy" +SH,"Saint Helena" +KN,"Saint Kitts and Nevis" +LC,"Saint Lucia" +MF,"Saint Martin" +PM,"Saint Pierre and Miquelon" +VC,"Saint Vincent and the Grenadines" +WS,Samoa +SM,"San Marino" +SA,"Saudi Arabia" +SN,Senegal +RS,Serbia +CS,"Serbia and Montenegro" +SC,Seychelles +SL,"Sierra Leone" +SG,Singapore +SK,Slovakia +SI,Slovenia +SB,"Solomon Islands" +SO,Somalia +ZA,"South Africa" +GS,"South Georgia and the South Sandwich Islands" +KR,"South Korea" +ES,Spain +LK,"Sri Lanka" +SD,Sudan +SR,Suriname +SJ,"Svalbard and Jan Mayen" +SZ,Swaziland +SE,Sweden +CH,Switzerland +SY,Syria +ST,"São Tomé and Príncipe" +TW,Taiwan +TJ,Tajikistan +TZ,Tanzania +TH,Thailand +TL,Timor-Leste +TG,Togo +TK,Tokelau +TO,Tonga +TT,"Trinidad and Tobago" +TN,Tunisia +TR,Turkey +TM,Turkmenistan +TC,"Turks and Caicos Islands" +TV,Tuvalu +UM,"U.S. Minor Outlying Islands" +PU,"U.S. Miscellaneous Pacific Islands" +VI,"U.S. Virgin Islands" +UG,Uganda +UA,Ukraine +SU,"Union of Soviet Socialist Republics" +AE,"United Arab Emirates" +GB,"United Kingdom" +US,"United States" +ZZ,"Unknown or Invalid Region" +UY,Uruguay +UZ,Uzbekistan +VU,Vanuatu +VA,"Vatican City" +VE,Venezuela +VN,Vietnam +WK,"Wake Island" +WF,"Wallis and Futuna" +EH,"Western Sahara" +YE,Yemen +ZM,Zambia +ZW,Zimbabwe +AX,"Åland Islands" diff --git a/test/integration/test_admin0_functions.py b/test/integration/test_admin0_functions.py new file mode 100644 index 0000000..3b460f7 --- /dev/null +++ b/test/integration/test_admin0_functions.py @@ -0,0 +1,29 @@ +import os, time, requests, json +from unittest import TestCase +from nose.tools import assert_raises + + +class TestConfigHelper(TestCase): + + def setUp(self): + username = os.environ["GEOCODER_API_TEST_USERNAME"] + api_key = os.environ["GEOCODER_API_TEST_API_KEY"] + host = os.environ["GEOCODER_API_TEST_HOST"] + self.table_name = os.environ["GEOCODER_API_TEST_TABLE_NAME"] + self.sql_api_url = "https://{0}.{1}/api/v2/sql?api_key={2}".format(username, host, api_key) + + def test_if_select_with_admin0_is_ok(self): + query = "SELECT cdb_geocode_admin0_polygon(name) as geometry FROM {0} LIMIT 1".format(self.table_name) + geometry = self.execute_query(query) + assert geometry != None + + def build_sql_api_query_url(self, query): + return "{0}&q={1}".format(self.sql_api_url,query) + + def execute_query(self, query): + query_url = self.build_sql_api_query_url(query) + query_response = requests.get(query_url) + if query_response.status_code != 200: + raise Exception("Error executing SQL API query") + query_response_data = json.loads(query_response.text) + return query_response_data['rows'][0]['geometry'] diff --git a/test/run_tests.py b/test/run_tests.py new file mode 100644 index 0000000..259d88d --- /dev/null +++ b/test/run_tests.py @@ -0,0 +1,91 @@ +import getopt, sys, requests, time, json, subprocess, os + +def main(): + opts, args = getopt.getopt(sys.argv[1:], "h", ["help", "host="]) + + if len(args) < 2: + usage() + sys.exit() + + host = "cartodb.com" + username = args[0] + api_key = args[1] + table_name = "geocoder_api_test_dataset_".format(int(time.time())) + for o, a in opts: + if o in ("-h", "--help"): + usage() + sys.exit() + elif o in ("--host"): + host = opts[0][1] + else: + assert False, "unhandled option" + + try: + table_name = import_test_dataset(username, api_key, host) + set_environment_variables(username, api_key, table_name, host) + execute_tests() + finally: + clean_environment_variables() + clean_test_dataset(username, api_key, table_name, host) + +def usage(): + print """Usage: run_tests.py [options] username api_key + Options: + -h: Show this help + --host: take that host as base (by default is cartodb.com). Eg. cartodb.com""" + +def import_test_dataset(username, api_key, host): + url = "https://{0}.{1}/api/v1/imports/?api_key={2}".format(username, host, api_key) + dataset = {'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} + response = requests.post(url, files=dataset) + response_json = json.loads(response.text) + if not response_json['success']: + print "Error importing the test dataset: {0}".format(response.text) + sys.exit(1) + while(True): + table_name = get_imported_table_name(username, host, api_key, response_json['item_queue_id']) + if table_name: + return table_name + else: + time.sleep(5) + +def get_imported_table_name(username, host, api_key, import_id): + import_data_url = "https://{0}.{1}/api/v1/imports/{2}?api_key={3}".format( + username, host, import_id, api_key + ) + import_data_response = requests.get(import_data_url) + if import_data_response.status_code != 200: + print "Error getting the table name from the import data: {0}".format(import_data_response.text) + sys.exit(1) + import_data_json = json.loads(import_data_response.text) + return import_data_json['table_name'] + +def execute_tests(): + print "Start testing..." + process = subprocess.Popen(["nosetests", "--where=integration/"]) + process.wait() + print "Testing finished!" + +def set_environment_variables(username, api_key, table_name, host): + os.environ["GEOCODER_API_TEST_USERNAME"] = username + os.environ["GEOCODER_API_TEST_API_KEY"] = api_key + os.environ["GEOCODER_API_TEST_TABLE_NAME"] = table_name + os.environ["GEOCODER_API_TEST_HOST"] = host + +def clean_environment_variables(): + print "Cleaning test dataset environment variables..." + del os.environ["GEOCODER_API_TEST_USERNAME"] + del os.environ["GEOCODER_API_TEST_API_KEY"] + del os.environ["GEOCODER_API_TEST_TABLE_NAME"] + del os.environ["GEOCODER_API_TEST_HOST"] + +def clean_test_dataset(username, api_key, table_name, host): + print "Cleaning test dataset {0}...".format(table_name) + url = "https://{0}.{1}/api/v2/sql?q=drop table {2}&api_key={3}".format(username, host, table_name, api_key) + response = requests.get(url) + if response.status_code != 200: + print "Error cleaning the test dataset: {0}".format(response.text) + sys.exit(1) + +if __name__ == "__main__": + main() \ No newline at end of file From f552f2db12aeb9324d3787866eb74e33b7ecade5 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 13:10:54 +0100 Subject: [PATCH 02/10] Created helper to be used in all the tests --- test/__init__.py | 0 test/helpers/__init__.py | 0 test/helpers/integration_test_helper.py | 30 +++++ test/integration/__init__.py | 0 test/integration/test_admin0_functions.py | 39 +++---- test/run_tests.py | 132 +++++++++++++--------- 6 files changed, 125 insertions(+), 76 deletions(-) create mode 100644 test/__init__.py create mode 100644 test/helpers/__init__.py create mode 100644 test/helpers/integration_test_helper.py create mode 100644 test/integration/__init__.py diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/helpers/__init__.py b/test/helpers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/helpers/integration_test_helper.py b/test/helpers/integration_test_helper.py new file mode 100644 index 0000000..63c7b9a --- /dev/null +++ b/test/helpers/integration_test_helper.py @@ -0,0 +1,30 @@ +import os +import requests +import json + +class IntegrationTestHelper: + + @classmethod + def get_environment_variables(cls): + username = os.environ["GEOCODER_API_TEST_USERNAME"] + api_key = os.environ["GEOCODER_API_TEST_API_KEY"] + host = os.environ["GEOCODER_API_TEST_HOST"] + table_name = os.environ["GEOCODER_API_TEST_TABLE_NAME"] + + return { + "username": username, + "api_key": api_key, + "host": host, + "table_name": table_name + } + + @classmethod + def execute_query(cls, sql_api_url, query): + query_url = "{0}&q={1}".format(sql_api_url, query) + print "Executing query: {0}".format(query_url) + query_response = requests.get(query_url) + if query_response.status_code != 200: + raise Exception("Error executing SQL API query") + query_response_data = json.loads(query_response.text) + + return query_response_data['rows'][0]['geometry'] diff --git a/test/integration/__init__.py b/test/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/integration/test_admin0_functions.py b/test/integration/test_admin0_functions.py index 3b460f7..eac3c76 100644 --- a/test/integration/test_admin0_functions.py +++ b/test/integration/test_admin0_functions.py @@ -1,29 +1,22 @@ -import os, time, requests, json from unittest import TestCase from nose.tools import assert_raises +from nose.tools import assert_not_equal +from ..helpers.integration_test_helper import IntegrationTestHelper -class TestConfigHelper(TestCase): +class TestAdmin0Functions(TestCase): - def setUp(self): - username = os.environ["GEOCODER_API_TEST_USERNAME"] - api_key = os.environ["GEOCODER_API_TEST_API_KEY"] - host = os.environ["GEOCODER_API_TEST_HOST"] - self.table_name = os.environ["GEOCODER_API_TEST_TABLE_NAME"] - self.sql_api_url = "https://{0}.{1}/api/v2/sql?api_key={2}".format(username, host, api_key) + def setUp(self): + self.env_variables = IntegrationTestHelper.get_environment_variables() + self.sql_api_url = "https://{0}.{1}/api/v2/sql?api_key={2}".format( + self.env_variables['username'], + self.env_variables['host'], + self.env_variables['api_key'] + ) - def test_if_select_with_admin0_is_ok(self): - query = "SELECT cdb_geocode_admin0_polygon(name) as geometry FROM {0} LIMIT 1".format(self.table_name) - geometry = self.execute_query(query) - assert geometry != None - - def build_sql_api_query_url(self, query): - return "{0}&q={1}".format(self.sql_api_url,query) - - def execute_query(self, query): - query_url = self.build_sql_api_query_url(query) - query_response = requests.get(query_url) - if query_response.status_code != 200: - raise Exception("Error executing SQL API query") - query_response_data = json.loads(query_response.text) - return query_response_data['rows'][0]['geometry'] + def test_if_select_with_admin0_is_ok(self): + query = "SELECT cdb_geocode_admin0_polygon(name) as geometry " \ + "FROM {0} LIMIT 1".format( + self.env_variables['table_name']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) diff --git a/test/run_tests.py b/test/run_tests.py index 259d88d..443d3cd 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -1,11 +1,18 @@ -import getopt, sys, requests, time, json, subprocess, os +import getopt +import sys +import requests +import time +import json +import subprocess +import os + def main(): opts, args = getopt.getopt(sys.argv[1:], "h", ["help", "host="]) if len(args) < 2: - usage() - sys.exit() + usage() + sys.exit() host = "cartodb.com" username = args[0] @@ -16,76 +23,95 @@ def main(): usage() sys.exit() elif o in ("--host"): - host = opts[0][1] + host = opts[0][1] else: assert False, "unhandled option" try: - table_name = import_test_dataset(username, api_key, host) - set_environment_variables(username, api_key, table_name, host) - execute_tests() + table_name = import_test_dataset(username, api_key, host) + set_environment_variables(username, api_key, table_name, host) + execute_tests() finally: - clean_environment_variables() - clean_test_dataset(username, api_key, table_name, host) + clean_environment_variables() + clean_test_dataset(username, api_key, table_name, host) + def usage(): - print """Usage: run_tests.py [options] username api_key + print """Usage: run_tests.py [options] username api_key Options: -h: Show this help - --host: take that host as base (by default is cartodb.com). Eg. cartodb.com""" + --host: take that host as base (by default is cartodb.com)""" + def import_test_dataset(username, api_key, host): - url = "https://{0}.{1}/api/v1/imports/?api_key={2}".format(username, host, api_key) - dataset = {'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} - response = requests.post(url, files=dataset) - response_json = json.loads(response.text) - if not response_json['success']: - print "Error importing the test dataset: {0}".format(response.text) - sys.exit(1) - while(True): - table_name = get_imported_table_name(username, host, api_key, response_json['item_queue_id']) - if table_name: - return table_name - else: - time.sleep(5) + url = "https://{0}.{1}/api/v1/imports/?api_key={2}".format( + username, host, api_key + ) + dataset = {'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} + response = requests.post(url, files=dataset) + response_json = json.loads(response.text) + if not response_json['success']: + print "Error importing the test dataset: {0}".format(response.text) + sys.exit(1) + while(True): + table_name = get_imported_table_name( + username, + host, + api_key, + response_json['item_queue_id'] + ) + if table_name: + return table_name + else: + time.sleep(5) + def get_imported_table_name(username, host, api_key, import_id): - import_data_url = "https://{0}.{1}/api/v1/imports/{2}?api_key={3}".format( - username, host, import_id, api_key - ) - import_data_response = requests.get(import_data_url) - if import_data_response.status_code != 200: - print "Error getting the table name from the import data: {0}".format(import_data_response.text) - sys.exit(1) - import_data_json = json.loads(import_data_response.text) - return import_data_json['table_name'] + import_data_url = "https://{0}.{1}/api/v1/imports/{2}?api_key={3}".format( + username, host, import_id, api_key + ) + import_data_response = requests.get(import_data_url) + if import_data_response.status_code != 200: + print "Error getting the table name from the import data: {0}".format( + import_data_response.text + ) + sys.exit(1) + import_data_json = json.loads(import_data_response.text) + + return import_data_json['table_name'] + def execute_tests(): - print "Start testing..." - process = subprocess.Popen(["nosetests", "--where=integration/"]) - process.wait() - print "Testing finished!" + print "Start testing..." + process = subprocess.Popen(["nosetests", "--where=integration/"]) + process.wait() + print "Testing finished!" + def set_environment_variables(username, api_key, table_name, host): - os.environ["GEOCODER_API_TEST_USERNAME"] = username - os.environ["GEOCODER_API_TEST_API_KEY"] = api_key - os.environ["GEOCODER_API_TEST_TABLE_NAME"] = table_name - os.environ["GEOCODER_API_TEST_HOST"] = host + os.environ["GEOCODER_API_TEST_USERNAME"] = username + os.environ["GEOCODER_API_TEST_API_KEY"] = api_key + os.environ["GEOCODER_API_TEST_TABLE_NAME"] = table_name + os.environ["GEOCODER_API_TEST_HOST"] = host + def clean_environment_variables(): - print "Cleaning test dataset environment variables..." - del os.environ["GEOCODER_API_TEST_USERNAME"] - del os.environ["GEOCODER_API_TEST_API_KEY"] - del os.environ["GEOCODER_API_TEST_TABLE_NAME"] - del os.environ["GEOCODER_API_TEST_HOST"] + print "Cleaning test dataset environment variables..." + del os.environ["GEOCODER_API_TEST_USERNAME"] + del os.environ["GEOCODER_API_TEST_API_KEY"] + del os.environ["GEOCODER_API_TEST_TABLE_NAME"] + del os.environ["GEOCODER_API_TEST_HOST"] + def clean_test_dataset(username, api_key, table_name, host): - print "Cleaning test dataset {0}...".format(table_name) - url = "https://{0}.{1}/api/v2/sql?q=drop table {2}&api_key={3}".format(username, host, table_name, api_key) - response = requests.get(url) - if response.status_code != 200: - print "Error cleaning the test dataset: {0}".format(response.text) - sys.exit(1) + print "Cleaning test dataset {0}...".format(table_name) + url = "https://{0}.{1}/api/v2/sql?q=drop table {2}&api_key={3}".format( + username, host, table_name, api_key + ) + response = requests.get(url) + if response.status_code != 200: + print "Error cleaning the test dataset: {0}".format(response.text) + sys.exit(1) if __name__ == "__main__": - main() \ No newline at end of file + main() From 916b353ffeec14dc55f9ce664325e290b26ded62 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 13:30:15 +0100 Subject: [PATCH 03/10] Finished admin0 tests --- test/helpers/integration_test_helper.py | 5 +++-- test/integration/test_admin0_functions.py | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/test/helpers/integration_test_helper.py b/test/helpers/integration_test_helper.py index 63c7b9a..7b3d24f 100644 --- a/test/helpers/integration_test_helper.py +++ b/test/helpers/integration_test_helper.py @@ -2,6 +2,7 @@ import os import requests import json + class IntegrationTestHelper: @classmethod @@ -20,11 +21,11 @@ class IntegrationTestHelper: @classmethod def execute_query(cls, sql_api_url, query): - query_url = "{0}&q={1}".format(sql_api_url, query) + query_url = "{0}?q={1}".format(sql_api_url, query) print "Executing query: {0}".format(query_url) query_response = requests.get(query_url) if query_response.status_code != 200: - raise Exception("Error executing SQL API query") + raise Exception(json.loads(query_response.text)['error']) query_response_data = json.loads(query_response.text) return query_response_data['rows'][0]['geometry'] diff --git a/test/integration/test_admin0_functions.py b/test/integration/test_admin0_functions.py index eac3c76..cb34171 100644 --- a/test/integration/test_admin0_functions.py +++ b/test/integration/test_admin0_functions.py @@ -1,6 +1,6 @@ from unittest import TestCase from nose.tools import assert_raises -from nose.tools import assert_not_equal +from nose.tools import assert_not_equal, assert_equal from ..helpers.integration_test_helper import IntegrationTestHelper @@ -8,7 +8,7 @@ class TestAdmin0Functions(TestCase): def setUp(self): self.env_variables = IntegrationTestHelper.get_environment_variables() - self.sql_api_url = "https://{0}.{1}/api/v2/sql?api_key={2}".format( + self.sql_api_url = "https://{0}.{1}/api/v2/sql".format( self.env_variables['username'], self.env_variables['host'], self.env_variables['api_key'] @@ -16,7 +16,17 @@ class TestAdmin0Functions(TestCase): def test_if_select_with_admin0_is_ok(self): query = "SELECT cdb_geocode_admin0_polygon(name) as geometry " \ - "FROM {0} LIMIT 1".format( - self.env_variables['table_name']) + "FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) assert_not_equal(geometry, None) + + def test_if_select_with_admin0_without_api_key_raise_error(self): + query = "SELECT cdb_geocode_admin0_polygon(name) as geometry " \ + "FROM {0} LIMIT 1".format( + self.env_variables['table_name']) + try: + IntegrationTestHelper.execute_query(self.sql_api_url, query) + except Exception as e: + assert_equal(e.message[0], "The api_key must be provided") From 4ca82f01a881bd585991c59c9706061c78a92b7b Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:52:57 +0100 Subject: [PATCH 04/10] Deactivated field guessing to avoid convert postal code to integer --- test/run_tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/run_tests.py b/test/run_tests.py index 443d3cd..d106dac 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -44,9 +44,9 @@ def usage(): def import_test_dataset(username, api_key, host): - url = "https://{0}.{1}/api/v1/imports/?api_key={2}".format( - username, host, api_key - ) + url = "https://{0}.{1}/api/v1/imports/"\ + "?type_guessing=false&api_key={2}".format( + username, host, api_key) dataset = {'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} response = requests.post(url, files=dataset) response_json = json.loads(response.text) From 26174dda221dfb3b62294a2dc73df61b9d922622 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:53:17 +0100 Subject: [PATCH 05/10] Fixture to fulfill the tests needs --- test/fixtures/geocoder_api_test_dataset.csv | 268 +------------------- test/integration/test_admin0_functions.py | 4 +- 2 files changed, 5 insertions(+), 267 deletions(-) diff --git a/test/fixtures/geocoder_api_test_dataset.csv b/test/fixtures/geocoder_api_test_dataset.csv index 3b65446..b27327b 100644 --- a/test/fixtures/geocoder_api_test_dataset.csv +++ b/test/fixtures/geocoder_api_test_dataset.csv @@ -1,265 +1,3 @@ -iso,name -AF,Afghanistan -AL,Albania -DZ,Algeria -AS,"American Samoa" -AD,Andorra -AO,Angola -AI,Anguilla -AQ,Antarctica -AG,"Antigua and Barbuda" -AR,Argentina -AM,Armenia -AW,Aruba -AU,Australia -AT,Austria -AZ,Azerbaijan -BS,Bahamas -BH,Bahrain -BD,Bangladesh -BB,Barbados -BY,Belarus -BE,Belgium -BZ,Belize -BJ,Benin -BM,Bermuda -BT,Bhutan -BO,Bolivia -BA,"Bosnia and Herzegovina" -BW,Botswana -BV,"Bouvet Island" -BR,Brazil -BQ,"British Antarctic Territory" -IO,"British Indian Ocean Territory" -VG,"British Virgin Islands" -BN,Brunei -BG,Bulgaria -BF,"Burkina Faso" -BI,Burundi -KH,Cambodia -CM,Cameroon -CA,Canada -CT,"Canton and Enderbury Islands" -CV,"Cape Verde" -KY,"Cayman Islands" -CF,"Central African Republic" -TD,Chad -CL,Chile -CN,China -CX,"Christmas Island" -CC,"Cocos [Keeling] Islands" -CO,Colombia -KM,Comoros -CG,"Congo - Brazzaville" -CD,"Congo - Kinshasa" -CK,"Cook Islands" -CR,"Costa Rica" -HR,Croatia -CU,Cuba -CY,Cyprus -CZ,"Czech Republic" -CI,"Côte d’Ivoire" -DK,Denmark -DJ,Djibouti -DM,Dominica -DO,"Dominican Republic" -NQ,"Dronning Maud Land" -DD,"East Germany" -EC,Ecuador -EG,Egypt -SV,"El Salvador" -GQ,"Equatorial Guinea" -ER,Eritrea -EE,Estonia -ET,Ethiopia -FK,"Falkland Islands" -FO,"Faroe Islands" -FJ,Fiji -FI,Finland -FR,France -GF,"French Guiana" -PF,"French Polynesia" -TF,"French Southern Territories" -FQ,"French Southern and Antarctic Territories" -GA,Gabon -GM,Gambia -GE,Georgia -DE,Germany -GH,Ghana -GI,Gibraltar -GR,Greece -GL,Greenland -GD,Grenada -GP,Guadeloupe -GU,Guam -GT,Guatemala -GG,Guernsey -GN,Guinea -GW,Guinea-Bissau -GY,Guyana -HT,Haiti -HM,"Heard Island and McDonald Islands" -HN,Honduras -HK,"Hong Kong SAR China" -HU,Hungary -IS,Iceland -IN,India -ID,Indonesia -IR,Iran -IQ,Iraq -IE,Ireland -IM,"Isle of Man" -IL,Israel -IT,Italy -JM,Jamaica -JP,Japan -JE,Jersey -JT,"Johnston Island" -JO,Jordan -KZ,Kazakhstan -KE,Kenya -KI,Kiribati -KW,Kuwait -KG,Kyrgyzstan -LA,Laos -LV,Latvia -LB,Lebanon -LS,Lesotho -LR,Liberia -LY,Libya -LI,Liechtenstein -LT,Lithuania -LU,Luxembourg -MO,"Macau SAR China" -MK,Macedonia -MG,Madagascar -MW,Malawi -MY,Malaysia -MV,Maldives -ML,Mali -MT,Malta -MH,"Marshall Islands" -MQ,Martinique -MR,Mauritania -MU,Mauritius -YT,Mayotte -FX,"Metropolitan France" -MX,Mexico -FM,Micronesia -MI,"Midway Islands" -MD,Moldova -MC,Monaco -MN,Mongolia -ME,Montenegro -MS,Montserrat -MA,Morocco -MZ,Mozambique -MM,"Myanmar [Burma]" -NA,Namibia -NR,Nauru -NP,Nepal -NL,Netherlands -AN,"Netherlands Antilles" -NT,"Neutral Zone" -NC,"New Caledonia" -NZ,"New Zealand" -NI,Nicaragua -NE,Niger -NG,Nigeria -NU,Niue -NF,"Norfolk Island" -KP,"North Korea" -VD,"North Vietnam" -MP,"Northern Mariana Islands" -NO,Norway -OM,Oman -PC,"Pacific Islands Trust Territory" -PK,Pakistan -PW,Palau -PS,"Palestinian Territories" -PA,Panama -PZ,"Panama Canal Zone" -PG,"Papua New Guinea" -PY,Paraguay -YD,"People's Democratic Republic of Yemen" -PE,Peru -PH,Philippines -PN,"Pitcairn Islands" -PL,Poland -PT,Portugal -PR,"Puerto Rico" -QA,Qatar -RO,Romania -RU,Russia -RW,Rwanda -RE,Réunion -BL,"Saint Barthélemy" -SH,"Saint Helena" -KN,"Saint Kitts and Nevis" -LC,"Saint Lucia" -MF,"Saint Martin" -PM,"Saint Pierre and Miquelon" -VC,"Saint Vincent and the Grenadines" -WS,Samoa -SM,"San Marino" -SA,"Saudi Arabia" -SN,Senegal -RS,Serbia -CS,"Serbia and Montenegro" -SC,Seychelles -SL,"Sierra Leone" -SG,Singapore -SK,Slovakia -SI,Slovenia -SB,"Solomon Islands" -SO,Somalia -ZA,"South Africa" -GS,"South Georgia and the South Sandwich Islands" -KR,"South Korea" -ES,Spain -LK,"Sri Lanka" -SD,Sudan -SR,Suriname -SJ,"Svalbard and Jan Mayen" -SZ,Swaziland -SE,Sweden -CH,Switzerland -SY,Syria -ST,"São Tomé and Príncipe" -TW,Taiwan -TJ,Tajikistan -TZ,Tanzania -TH,Thailand -TL,Timor-Leste -TG,Togo -TK,Tokelau -TO,Tonga -TT,"Trinidad and Tobago" -TN,Tunisia -TR,Turkey -TM,Turkmenistan -TC,"Turks and Caicos Islands" -TV,Tuvalu -UM,"U.S. Minor Outlying Islands" -PU,"U.S. Miscellaneous Pacific Islands" -VI,"U.S. Virgin Islands" -UG,Uganda -UA,Ukraine -SU,"Union of Soviet Socialist Republics" -AE,"United Arab Emirates" -GB,"United Kingdom" -US,"United States" -ZZ,"Unknown or Invalid Region" -UY,Uruguay -UZ,Uzbekistan -VU,Vanuatu -VA,"Vatican City" -VE,Venezuela -VN,Vietnam -WK,"Wake Island" -WF,"Wallis and Futuna" -EH,"Western Sahara" -YE,Yemen -ZM,Zambia -ZW,Zimbabwe -AX,"Åland Islands" +id,country,province,city,postalcode,ip +1,Spain,Castilla y León,Valladolid,47010,8.8.8.8 +2,USA,New York,Manhattn,10001,8.8.8.8 \ No newline at end of file diff --git a/test/integration/test_admin0_functions.py b/test/integration/test_admin0_functions.py index cb34171..ff8912a 100644 --- a/test/integration/test_admin0_functions.py +++ b/test/integration/test_admin0_functions.py @@ -15,7 +15,7 @@ class TestAdmin0Functions(TestCase): ) def test_if_select_with_admin0_is_ok(self): - query = "SELECT cdb_geocode_admin0_polygon(name) as geometry " \ + query = "SELECT cdb_geocode_admin0_polygon(country) as geometry " \ "FROM {0} LIMIT 1&api_key={1}".format( self.env_variables['table_name'], self.env_variables['api_key']) @@ -23,7 +23,7 @@ class TestAdmin0Functions(TestCase): assert_not_equal(geometry, None) def test_if_select_with_admin0_without_api_key_raise_error(self): - query = "SELECT cdb_geocode_admin0_polygon(name) as geometry " \ + query = "SELECT cdb_geocode_admin0_polygon(country) as geometry " \ "FROM {0} LIMIT 1".format( self.env_variables['table_name']) try: From db10d5b29a772cc763ae0e80d0b68224b33dcd7c Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:53:54 +0100 Subject: [PATCH 06/10] Admin1 integration tests --- test/integration/test_admin1_functions.py | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/integration/test_admin1_functions.py diff --git a/test/integration/test_admin1_functions.py b/test/integration/test_admin1_functions.py new file mode 100644 index 0000000..082cc43 --- /dev/null +++ b/test/integration/test_admin1_functions.py @@ -0,0 +1,40 @@ +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 + + +class TestAdmin1Functions(TestCase): + + def setUp(self): + self.env_variables = IntegrationTestHelper.get_environment_variables() + self.sql_api_url = "https://{0}.{1}/api/v2/sql".format( + self.env_variables['username'], + self.env_variables['host'], + self.env_variables['api_key'] + ) + + def test_if_select_with_admin1_without_country_is_ok(self): + query = "SELECT cdb_geocode_admin1_polygon(province) as geometry " \ + "FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_admin1_with_country_is_ok(self): + query = "SELECT cdb_geocode_admin1_polygon(province,country)" \ + "as geometry FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_admin1_without_api_key_raise_error(self): + query = "SELECT cdb_geocode_admin1_polygon(province) as geometry " \ + "FROM {0} LIMIT 1".format( + self.env_variables['table_name']) + try: + IntegrationTestHelper.execute_query(self.sql_api_url, query) + except Exception as e: + assert_equal(e.message[0], "The api_key must be provided") From 07041922a445bcc89bd3ef67c61b306e338eed7a Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:54:12 +0100 Subject: [PATCH 07/10] Namedplaces functions integration tests --- test/integration/test_namedplace_functions.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/integration/test_namedplace_functions.py diff --git a/test/integration/test_namedplace_functions.py b/test/integration/test_namedplace_functions.py new file mode 100644 index 0000000..e3c7c07 --- /dev/null +++ b/test/integration/test_namedplace_functions.py @@ -0,0 +1,48 @@ +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 + + +class TestNameplaceFunctions(TestCase): + + def setUp(self): + self.env_variables = IntegrationTestHelper.get_environment_variables() + self.sql_api_url = "https://{0}.{1}/api/v2/sql".format( + self.env_variables['username'], + self.env_variables['host'], + self.env_variables['api_key'] + ) + + def test_if_select_with_namedplace_city_is_ok(self): + query = "SELECT cdb_geocode_namedplace_point(city) as geometry " \ + "FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_namedplace_city_country_is_ok(self): + query = "SELECT cdb_geocode_namedplace_point(city,country) " \ + "as geometry FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_namedplace_city_province_country_is_ok(self): + query = "SELECT cdb_geocode_namedplace_point(city,province,country) " \ + "as geometry FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_namedplace_without_api_key_raise_error(self): + query = "SELECT cdb_geocode_namedplace_point(city) as geometry " \ + "FROM {0} LIMIT 1".format( + self.env_variables['table_name']) + try: + IntegrationTestHelper.execute_query(self.sql_api_url, query) + except Exception as e: + assert_equal(e.message[0], "The api_key must be provided") From 3fd603d5cb2662aff25cf10095dcc20a00f39015 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:54:24 +0100 Subject: [PATCH 08/10] Postal codes functions integration tests --- test/integration/test_postalcode_functions.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/integration/test_postalcode_functions.py diff --git a/test/integration/test_postalcode_functions.py b/test/integration/test_postalcode_functions.py new file mode 100644 index 0000000..7845a32 --- /dev/null +++ b/test/integration/test_postalcode_functions.py @@ -0,0 +1,40 @@ +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 + + +class TestPostalcodeFunctions(TestCase): + + def setUp(self): + self.env_variables = IntegrationTestHelper.get_environment_variables() + self.sql_api_url = "https://{0}.{1}/api/v2/sql".format( + self.env_variables['username'], + self.env_variables['host'], + self.env_variables['api_key'] + ) + + def test_if_select_with_postalcode_polygon_is_ok(self): + query = "SELECT cdb_geocode_postalcode_polygon(postalcode, country) " \ + "as geometry FROM {0} WHERE country='USA'&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_postalcode_point_is_ok(self): + query = "SELECT cdb_geocode_postalcode_point(postalcode, country) " \ + "as geometry FROM {0} WHERE country='Spain'&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_postalcode_without_api_key_raise_error(self): + query = "SELECT cdb_geocode_postalcode_polygon(postalcode, country) " \ + "as geometry FROM {0} WHERE country = 'USA'".format( + self.env_variables['table_name']) + try: + IntegrationTestHelper.execute_query(self.sql_api_url, query) + except Exception as e: + assert_equal(e.message[0], "The api_key must be provided") From 342066f22aa410e4f5ed17dab0f73915025d4bde Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 14:54:41 +0100 Subject: [PATCH 09/10] Ip address functions integration tests --- test/integration/test_ipaddress_functions.py | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/integration/test_ipaddress_functions.py diff --git a/test/integration/test_ipaddress_functions.py b/test/integration/test_ipaddress_functions.py new file mode 100644 index 0000000..42c30a0 --- /dev/null +++ b/test/integration/test_ipaddress_functions.py @@ -0,0 +1,32 @@ +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 + + +class TestPostalcodeFunctions(TestCase): + + def setUp(self): + self.env_variables = IntegrationTestHelper.get_environment_variables() + self.sql_api_url = "https://{0}.{1}/api/v2/sql".format( + self.env_variables['username'], + self.env_variables['host'], + self.env_variables['api_key'] + ) + + def test_if_select_with_ipaddress_point_is_ok(self): + query = "SELECT cdb_geocode_ipaddress_point(ip) " \ + "as geometry FROM {0} LIMIT 1&api_key={1}".format( + self.env_variables['table_name'], + self.env_variables['api_key']) + geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query) + assert_not_equal(geometry, None) + + def test_if_select_with_ipaddress_without_api_key_raise_error(self): + query = "SELECT cdb_geocode_ipaddress_point(ip) " \ + "as geometry FROM {0} LIMIT 1".format( + self.env_variables['table_name']) + try: + IntegrationTestHelper.execute_query(self.sql_api_url, query) + except Exception as e: + assert_equal(e.message[0], "The api_key must be provided") From ae65aff707fd65e79e0c821ed6c724a003fb5e00 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Sun, 6 Dec 2015 15:01:46 +0100 Subject: [PATCH 10/10] Refactor and extract import logic to a helper --- test/helpers/import_helper.py | 45 +++++++++++++++++++++++++++++++++++ test/run_tests.py | 44 ++++------------------------------ 2 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 test/helpers/import_helper.py diff --git a/test/helpers/import_helper.py b/test/helpers/import_helper.py new file mode 100644 index 0000000..cb8f6e1 --- /dev/null +++ b/test/helpers/import_helper.py @@ -0,0 +1,45 @@ +import os +import requests +import json + +import time + +class ImportHelper: + + @classmethod + def import_test_dataset(cls, username, api_key, host): + url = "https://{0}.{1}/api/v1/imports/"\ + "?type_guessing=false&api_key={2}".format( + username, host, api_key) + dataset = { + 'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} + response = requests.post(url, files=dataset) + response_json = json.loads(response.text) + if not response_json['success']: + print "Error importing the test dataset: {0}".format(response.text) + sys.exit(1) + while(True): + table_name = ImportHelper.get_imported_table_name( + username, + host, + api_key, + response_json['item_queue_id'] + ) + if table_name: + return table_name + else: + time.sleep(5) + + @classmethod + def get_imported_table_name(cls, username, host, api_key, import_id): + import_url = "https://{0}.{1}/api/v1/imports/{2}?api_key={3}".format( + username, host, import_id, api_key) + import_data_response = requests.get(import_url) + if import_data_response.status_code != 200: + print "Error getting the table name from " \ + "the import data: {0}".format( + import_data_response.text) + sys.exit(1) + import_data_json = json.loads(import_data_response.text) + + return import_data_json['table_name'] diff --git a/test/run_tests.py b/test/run_tests.py index d106dac..c93b5e3 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -5,6 +5,7 @@ import time import json import subprocess import os +from helpers.import_helper import ImportHelper def main(): @@ -28,9 +29,12 @@ def main(): assert False, "unhandled option" try: - table_name = import_test_dataset(username, api_key, host) + table_name = ImportHelper.import_test_dataset(username, api_key, host) set_environment_variables(username, api_key, table_name, host) execute_tests() + except Exception as e: + print e.message + sys.exit(1) finally: clean_environment_variables() clean_test_dataset(username, api_key, table_name, host) @@ -43,44 +47,6 @@ def usage(): --host: take that host as base (by default is cartodb.com)""" -def import_test_dataset(username, api_key, host): - url = "https://{0}.{1}/api/v1/imports/"\ - "?type_guessing=false&api_key={2}".format( - username, host, api_key) - dataset = {'file': open('fixtures/geocoder_api_test_dataset.csv', 'rb')} - response = requests.post(url, files=dataset) - response_json = json.loads(response.text) - if not response_json['success']: - print "Error importing the test dataset: {0}".format(response.text) - sys.exit(1) - while(True): - table_name = get_imported_table_name( - username, - host, - api_key, - response_json['item_queue_id'] - ) - if table_name: - return table_name - else: - time.sleep(5) - - -def get_imported_table_name(username, host, api_key, import_id): - import_data_url = "https://{0}.{1}/api/v1/imports/{2}?api_key={3}".format( - username, host, import_id, api_key - ) - import_data_response = requests.get(import_data_url) - if import_data_response.status_code != 200: - print "Error getting the table name from the import data: {0}".format( - import_data_response.text - ) - sys.exit(1) - import_data_json = json.loads(import_data_response.text) - - return import_data_json['table_name'] - - def execute_tests(): print "Start testing..." process = subprocess.Popen(["nosetests", "--where=integration/"])