2015-12-06 20:10:54 +08:00
|
|
|
import getopt
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "h", ["help", "host="])
|
|
|
|
|
|
|
|
if len(args) < 2:
|
2015-12-06 20:10:54 +08:00
|
|
|
usage()
|
|
|
|
sys.exit()
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
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"):
|
2015-12-06 20:10:54 +08:00
|
|
|
host = opts[0][1]
|
2015-12-04 23:58:28 +08:00
|
|
|
else:
|
|
|
|
assert False, "unhandled option"
|
|
|
|
|
|
|
|
try:
|
2015-12-06 20:10:54 +08:00
|
|
|
table_name = import_test_dataset(username, api_key, host)
|
|
|
|
set_environment_variables(username, api_key, table_name, host)
|
|
|
|
execute_tests()
|
2015-12-04 23:58:28 +08:00
|
|
|
finally:
|
2015-12-06 20:10:54 +08:00
|
|
|
clean_environment_variables()
|
|
|
|
clean_test_dataset(username, api_key, table_name, host)
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def usage():
|
2015-12-06 20:10:54 +08:00
|
|
|
print """Usage: run_tests.py [options] username api_key
|
2015-12-04 23:58:28 +08:00
|
|
|
Options:
|
|
|
|
-h: Show this help
|
2015-12-06 20:10:54 +08:00
|
|
|
--host: take that host as base (by default is cartodb.com)"""
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def import_test_dataset(username, api_key, host):
|
2015-12-06 21:52:57 +08:00
|
|
|
url = "https://{0}.{1}/api/v1/imports/"\
|
|
|
|
"?type_guessing=false&api_key={2}".format(
|
|
|
|
username, host, api_key)
|
2015-12-06 20:10:54 +08:00
|
|
|
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)
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def get_imported_table_name(username, host, api_key, import_id):
|
2015-12-06 20:10:54 +08:00
|
|
|
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']
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def execute_tests():
|
2015-12-06 20:10:54 +08:00
|
|
|
print "Start testing..."
|
|
|
|
process = subprocess.Popen(["nosetests", "--where=integration/"])
|
|
|
|
process.wait()
|
|
|
|
print "Testing finished!"
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def set_environment_variables(username, api_key, table_name, host):
|
2015-12-06 20:10:54 +08:00
|
|
|
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
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def clean_environment_variables():
|
2015-12-06 20:10:54 +08:00
|
|
|
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"]
|
|
|
|
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
def clean_test_dataset(username, api_key, table_name, host):
|
2015-12-06 20:10:54 +08:00
|
|
|
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)
|
2015-12-04 23:58:28 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-12-06 20:10:54 +08:00
|
|
|
main()
|