In [1]:
import json
import urllib2
test_url = 'http://localhost:81/api'
prod_url = 'http://datasurfer.sandag.org/api'
def getJsonResponse(*args):
url = '/'.join(args).replace(' ', '%20')
return json.load(urllib2.urlopen(url))
In [2]:
'''
Test to ensure the test and production year / series lists match for each type of datasource
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
test_json = sorted(getJsonResponse(test_url, datasource), key=lambda x: x[json_key])
prod_json = sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1])
print "Datasource: %s, Length %s" % (datasource, "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
test = 'matches' if test_json[i][json_key] == prod_json[i][1] else 'DOES NOT MATCH'
print "Test %s: %s %s Prod %s: %s" % (json_key, test_json[i][json_key], test, json_key, prod_json[i][1])
In [3]:
'''
Test to ensure the test and production zone type lists match for each type of datasource / year combination
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1])), key=lambda x: x['zone'])
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1])
print "Datasource: %s, Length %s" % (datasource, "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
test = 'matches' if test_json[i]['zone'] == prod_json[i][1] else 'DOES NOT MATCH'
print "Test %s: %s %s Prod %s: %s" % (json_key, test_json[i]['zone'], test, json_key, prod_json[i][1])
In [4]:
'''
Test to ensure the test and production zone lists match for each type of datasource / year / zonetype combination
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]])
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]])
print "Datasource: %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
if test_json[i][zone_json[1]] != prod_json[i][zone_json[1]]:
print "Test %s: %s DOES NOT MATCH Prod %s: %s" % (json_key, test_json[i][zone_json[1]], json_key, prod_json[i][zone_json[1]])
In [5]:
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
print "Datasource: %s %s %s" % (datasource, datasource_json[1], str(zone_json[1]))
for area_json in sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'age'), key=lambda x: (x['year'], x['sex'], x['group_10yr']))
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'age'), key=lambda x: (x['year'], x['sex'], x['group_10yr']))
if len(test_json) != len(prod_json):
print "Datasource: %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
print "Prod Count: %d" % (len(prod_json))
print "Test Count: %d" % (len(test_json))
for i in xrange(len(prod_json)):
if test_json[i]['population'] <> prod_json[i]['population']:
print "Datasource: %s %s %s %s, Age Population Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
In [6]:
'''
ETHNICITY COMPARISON TEST - PRODUCTION IS CONSIDERED TRUTH
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
print "Datasource: %s %s %s" % (datasource, datasource_json[1], str(zone_json[1]))
for area_json in sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'ethnicity'), key=lambda x: (x['year'], x['ethnicity']))
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'ethnicity'), key=lambda x: (x['year'], x['ethnicity']))
if len(test_json) != len(prod_json):
print "Datasource: %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
if test_json[i]['population'] <> prod_json[i]['population']:
print "Datasource: %s %s %s %s, Ethnicity Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
In [22]:
'''
HOUSING COMPARISON TEST - PRODUCTION IS CONSIDERED TRUTH
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
print "Datasource: %s %s %s" % (datasource, datasource_json[1], str(zone_json[1]))
for area_json in sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'housing'), key=lambda x: (x['year'], x['unit_type']))
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'housing'), key=lambda x: (x['year'], x['unit_type']))
if len(test_json) != len(prod_json):
print "Datasource: %s %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]], "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
if test_json[i]['units'] <> prod_json[i]['units']:
print "Datasource: %s %s %s %s, Units Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
if test_json[i]['occupied'] <> prod_json[i]['occupied']:
print "Datasource: %s %s %s %s, Occupied Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
if test_json[i]['unoccupied'] <> prod_json[i]['unoccupied']:
print "Datasource: %s %s %s %s, Unoccupied Population Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
if prod_json[i]['vacancy_rate'] is None:
if test_json[i]['vacancy_rate'] <> 0:
print "Datasource: %s %s %s %s, Vacancy Population Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
elif round(float(test_json[i]['vacancy_rate']), 5) <> round(float(prod_json[i]['vacancy_rate']),5):
print "Datasource: %s %s %s %s, Vacancy Population Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
print "Test = %f, Prod = %f" % (round(float(test_json[i]['vacancy_rate']), 5), round(float(prod_json[i]['vacancy_rate']), 5))
In [23]:
'''
INCOME COMPARISON TEST - PRODUCTION IS CONSIDERED TRUTH
'''
for datasource in ['estimate', 'census', 'forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
print "Datasource: %s %s %s" % (datasource, datasource_json[1], str(zone_json[1]))
for area_json in sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'income'), key=lambda x: (x['year'], x['ordinal']))
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'income'), key=lambda x: (x['year'], x['ordinal']))
if len(test_json) != len(prod_json):
print "Datasource: %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
if test_json[i]['households'] <> prod_json[i]['households']:
print "Datasource: %s %s %s %s, Income Does Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])
In [ ]:
'''
JOBS COMPARISON TEST - PRODUCTION IS CONSIDERED TRUTH
'''
for datasource in ['forecast']:
json_key = 'series' if datasource == 'forecast' else 'year'
for datasource_json in sorted(getJsonResponse(prod_url, datasource), key=lambda x: x[1]):
for zone_json in sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1])), key=lambda x: x[1]):
print "Datasource: %s %s %s" % (datasource, datasource_json[1], str(zone_json[1]))
for area_json in sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1])), key=lambda x: x[zone_json[1]]):
test_json = sorted(getJsonResponse(test_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'jobs'), key=lambda x: (x['year'], x['category']))
prod_json = sorted(getJsonResponse(prod_url, datasource, str(datasource_json[1]), str(zone_json[1]), area_json[zone_json[1]], 'jobs'), key=lambda x: (x['year'], x['category']))
if len(test_json) != len(prod_json):
print "Datasource: %s %s %s, Length %s" % (datasource, datasource_json[1], str(zone_json[1]), "Matches" if len(test_json) == len(prod_json) else "DOES NOT MATCH")
for i in xrange(len(prod_json)):
if test_json[i]['jobs'] <> prod_json[i]['jobs']:
print "Datasource: %s %s %s %s, Jobs Do Not Match" % (datasource, datasource_json[1], str(zone_json[1]), area_json[zone_json[1]])