def test_from_dict_with_malformed_JSON_data(self): with self.assertRaises(ParseAPIResponseError): Forecast.from_dict(json.loads(self.__bad_json)) with self.assertRaises(ParseAPIResponseError): Forecast.from_dict(json.loads(self.__bad_json_2)) with self.assertRaises(ParseAPIResponseError): Forecast.from_dict(json.loads(FORECAST_MALFORMED_JSON))
def main(): ''' ''' park_name = 'Prospect Park' park_lat = park_loc.loc[park_loc.name == park_name, 'lat'].iloc[0] park_long = park_loc.loc[park_loc.name == park_name, 'long'].iloc[0] api_url = 'http://api.openweathermap.org/data/2.5/forecast' payload = {'lat': park_lat, 'lon': park_long, 'appid': OWM_api_key} owm_req = requests.get(api_url, params=payload) owm_json = json.loads(owm_req.text) sunrise_time = owm_json['city']['sunrise'] sunset_time = owm_json['city']['sunset'] tz_correct = owm_json['city']['timezone'] park_forecast = Forecast.from_dict(json.loads(owm_req.text)) weather_df = forcast_to_df(park_name, park_forecast, sunrise_time, sunset_time, tz_correct) # log_path = data_file_path('weather_data', 'weather_log.csv') log_path = os.path.join(root_dir, 'data', 'weather_log.csv') if os.path.exists(log_path): weather_log = pd.read_csv(log_path, index_col=0) else: weather_log = pd.read_csv(os.path.join(root_dir, 'data', 'example_weather_df.csv'), index_col=0) weather_log = weather_log.append(weather_df, ignore_index=True) weather_log.to_csv(log_path, index_label='id')
def test_from_dict_when_server_error(self): with self.assertRaises(APIResponseError): Forecast.from_dict(json.loads(INTERNAL_SERVER_ERROR_JSON))
def test_from_dict_when_no_results(self): result = Forecast.from_dict(json.loads(FORECAST_NOT_FOUND_JSON)) self.assertFalse(result is None) self.assertEqual(0, len(result)) result = Forecast.from_dict(json.loads(self.__no_items_found_json)) self.assertEqual(0, len(result))
def test_from_dict_fails_when_JSON_data_is_None(self): with self.assertRaises(ParseAPIResponseError): Forecast.from_dict(None)