def test_emissions_range_3(self): api_caller = ApiCaller() test_range = {1969: 12112.101, 1970: 21539.958, 1971: 32280.601} api_caller.get_data_range('Nigeria', 'emissions', 1969, 1971) self.assertEqual( test_range, api_caller.get_data_range('Nigeria', 'emissions', 1969, 1971))
def test_year_range_3(self): api_caller = ApiCaller() year_range = [1986, 1987, 1988] self.assertEqual( year_range, api_caller.get_year_list('Aruba', 'emissions_per_capita', 1900, 1988))
def test_year_range_4(self): api_caller = ApiCaller() year_range = [ 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970 ] api_caller.get_year_list('Philippines', 'emissions', 1000, 1970) self.assertEqual( year_range, api_caller.get_year_list('Philippines', 'emissions', 1000, 1970))
def test_population_range_3(self): api_caller = ApiCaller() test_range = { 2005: 16319868, 2006: 16346101, 2007: 16381696, 2008: 16445593 } api_caller.get_data_range('Netherlands', 'population', 2005, 2008) self.assertEqual( test_range, api_caller.get_data_range('Netherlands', 'population', 2005, 2008))
def test_emissions_range_2(self): api_caller = ApiCaller() test_range = { 1986: 102874.018, 1987: 103116.04, 1988: 100354.789, 1989: 107461.435, 1990: 106049.64 } self.assertEqual( test_range, api_caller.get_data_range('Belgium', 'emissions', 1986, 1990))
def test_emissions_range_1(self): api_caller = ApiCaller() test_range = { 2010: 493207.833, 2011: 447828.708, 2012: 468572.927, 2013: 458250.322, 2014: 419820.162 } self.assertEqual( test_range, api_caller.get_data_range('United Kingdom', 'emissions', 2010, 2014))
def test_multi_data_2(self): api_caller = ApiCaller() country_list = ["Puerto Rico", "Sweden"] expected_response = { 'Puerto Rico': { 1960: None }, 'Sweden': { 1960: 6.58 } } self.assertEqual( expected_response, api_caller.get_multiple_data_range(country_list, 'emissions_per_capita', 1940, 1960))
def test_multi_data_1(self): api_caller = ApiCaller() country_list = ["Aruba", "Finland"] expected_response = { 'Aruba': { 1985: None, 1986: 179.683, 1987: 447.374 }, 'Finland': { 1985: 49665.848, 1986: 53329.181, 1987: 57656.241 } } self.assertEqual( expected_response, api_caller.get_multiple_data_range(country_list, 'emissions', 1985, 1987))
def test_country_dict_2(self): api_caller = ApiCaller() country_list = api_caller.get_country_list() country_dict = api_caller.get_country_id_dict() self.assertTrue(len(country_list) == len(country_dict))
def test_year_range_1(self): api_caller = ApiCaller() year_range = [2003, 2004, 2005] self.assertEqual( year_range, api_caller.get_year_list('Zimbabwe', 'emissions', 2005, 2003))
def test_year_range_2(self): api_caller = ApiCaller() year_range = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017] self.assertEqual( year_range, api_caller.get_year_list('Indonesia', 'population', 2010, 3000))
def test_data_8(self): api_caller = ApiCaller() with self.assertRaises(ValueError): api_caller.get_data('Norway', 'notData', 2009)
def test_data_9(self): api_caller = ApiCaller() with self.assertRaises(KeyError): api_caller.get_data('Canada', 'emissions', 1000)
def test_name_1(self): api_caller = ApiCaller() self.assertEqual('dnk', api_caller.get_country_id('Denmark'))
from flask import Flask, render_template, request, jsonify from apiCaller import ApiCaller app = Flask(__name__) api_caller = ApiCaller() # Returns a color based on the hash of the country name def get_color(name: str): # Gets a 9 digit hash from provided string as a string name_hash = str(hash(name) % 10**9) # Converts 3 digit decimal integer to a decimal int between 0 and 255 def to_hex(num): return str(int((num / 999) * 255)) r = to_hex(int(name_hash[0:3])) b = to_hex(int(name_hash[3:6])) g = to_hex(int(name_hash[6:9])) return "rgb(" + r + "," + g + "," + b + ')' @app.route('/', methods=["POST", "GET"]) def index(): generic_country_list = api_caller.get_country_list() generic_year_list = api_caller.get_generic_year_list() current_year = ApiCaller().get_current_year()
def test_country_list_1(self): api_caller = ApiCaller() self.assertEqual('Afghanistan', api_caller.get_country_list()[0])
def test_year_range_6(self): api_caller = ApiCaller() with self.assertRaises(TypeError): api_caller.get_year_list('Portugal', 2, 1967, 1970)
def test_population_range_1(self): api_caller = ApiCaller() test_range = {2009: 71339185, 2010: 72326914, 2011: 73409455} self.assertEqual( test_range, api_caller.get_data_range('Turkey', 'population', 2009, 2011))
def test_country_dict_1(self): api_caller = ApiCaller() self.assertTrue('Africa' not in api_caller.get_country_id_dict())
def test_population_range_2(self): api_caller = ApiCaller() test_range = {2007: 7180100, 2008: 7308800} self.assertEqual( test_range, api_caller.get_data_range('Israel', 'population', 2007, 2008))
def test_year_range_5(self): api_caller = ApiCaller() year_range = [] self.assertEqual( year_range, api_caller.get_year_list('Portugal', 'population', 1900, 1923))
def test_data_range_error_1(self): api_caller = ApiCaller() with self.assertRaises(TypeError): api_caller.get_data_range(True, 'emissions', 1969, 1971)
def test_year_range_7(self): api_caller = ApiCaller() with self.assertRaises(ValueError): api_caller.get_year_list('Portugal', 'asd', 1967, 1970)
def test_data_range_error_2(self): api_caller = ApiCaller() with self.assertRaises(TypeError): api_caller.get_data_range('Nigeria', True, 1969, 1971)
def test_year_range_9(self): api_caller = ApiCaller() with self.assertRaises(TypeError): api_caller.get_year_list('Portugal', 'population', 1967, 'asd')
def test_data_range_error_4(self): api_caller = ApiCaller() with self.assertRaises(TypeError): api_caller.get_data_range('Nigeria', 'emissions', 1969, False)
def test_name_2(self): api_caller = ApiCaller() self.assertEqual('gbr', api_caller.get_country_id('United Kingdom'))
def test_data_range_error_5(self): api_caller = ApiCaller() with self.assertRaises(ValueError): api_caller.get_data_range('Nigeria', 'asd', 1969, 1971)
def index(): generic_country_list = api_caller.get_country_list() generic_year_list = api_caller.get_generic_year_list() current_year = ApiCaller().get_current_year() year_min = int(1960) year_max = ApiCaller().get_current_year() data_type = None labels = generic_year_list values = [] input_country_list = [] country_amount = 0 color_list = [] is_empty = False received = False invalid = False if request.method == "POST": received = True input_country_list = request.form.get('input_country_list').split(";") for country in input_country_list: if country not in generic_country_list: input_country_list.remove(country) country_amount = len(input_country_list) if country_amount == 0: invalid = True else: input_country_list = sorted(input_country_list) # Gets a unique and persistent color for each country for country in input_country_list: color_list.append(get_color(country)) year_min = int(request.form.get('selected_year_min')) year_max = int(request.form.get('selected_year_max')) per_capita_selector = request.form.get('selected_data') if per_capita_selector == "on": data_type = "emissions_per_capita" else: data_type = "emissions" if not invalid: data_dict = api_caller.get_multiple_data_range( input_country_list, data_type, year_min, year_max) # List of years for x axis labels, same for every country labels = list(data_dict[list(data_dict.keys())[0]].keys()) # Assume no data was retrieved is_empty = True for i in range(len(input_country_list)): value_list = list(data_dict[list( data_dict.keys())[i]].values()) values.append(value_list) # Check to see if any data present for this country for item in value_list: if item is not None: is_empty = False return render_template('index.html', generic_country_list=generic_country_list, generic_year_list=generic_year_list, year_min=year_min, year_max=year_max, data_type=data_type, labels=labels, values=values, received=received, is_empty=is_empty, invalid=invalid, input_country_list=input_country_list, country_amount=country_amount, color_list=color_list, current_year=current_year)
def test_data_7(self): api_caller = ApiCaller() with self.assertRaises(KeyError): api_caller.get_data('NotACountry', 'emissions', 2009)