def test_getters_return_expected_daily_data(self): instance = Forecast("daily", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.interval, "daily") self.assertEqual(instance.reception_time(), self.__test_reception_time) self.assertEqual(instance.location, self.__test_location) self.assertEqual(instance.weathers, self.__test_weathers)
def test_getters_return_expected_daily_data(self): instance = Forecast("daily", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.get_interval(), "daily") self.assertEqual(instance.get_reception_time(), self.__test_reception_time) self.assertEqual(instance.get_location(), self.__test_location) self.assertEqual(instance.get_weathers(), self.__test_weathers)
def test_getters_return_expected_3h_data(self): """ Test either for "3h" forecast and "daily" ones """ instance = Forecast("3h", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.interval, "3h") self.assertEqual(instance.reception_time(), self.__test_reception_time) self.assertEqual(instance.location, self.__test_location) self.assertEqual(instance.weathers, self.__test_weathers)
def test_getters_return_expected_3h_data(self): """ Test either for "3h" forecast and "daily" ones """ instance = Forecast("3h", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.get_interval(), "3h") self.assertEqual(instance.get_reception_time(), self.__test_reception_time) self.assertEqual(instance.get_location(), self.__test_location) self.assertEqual(instance.get_weathers(), self.__test_weathers)
def five_day(coords, code=None): ''' Get each weather forecast for the corrosponding coordinates :param coords: the latitude and longitude for which that that weather is being forecasted :type coords: tuple containing the latitude and logitude for the forecast :return five_day: the five day, every three hours, forecast for the zipcode :type five_day: dict ''' owm = OWM(masta_key) Forecast = get_data_from_weather_api(owm, coords=coords).get_forecast() forecast = json.loads(Forecast.to_JSON()) if code: forecast['zipcode'] = code if coords: forecast['coordinates'] = coords forecast.pop('Location') forecast.pop('interval') reception_time = forecast['reception_time'] # to add to weathers array for cast in forecast['weathers']: cast['zipcode'] = forecast['zipcode'] cast['instant'] = cast.pop('reference_time') cast['time_to_instant'] = cast['instant'] - reception_time return forecast
def five_day(location): ''' Get each weather forecast for the corrosponding coordinates :param coords: the latitude and longitude for which that that weather is being forecasted :type coords: tuple containing the latitude and logitude for the forecast :return casts: the five day, every three hours, forecast for the zip code :type casts: list of Weather objects ''' owm = OWM(masta_key) Forecast = get_data_from_weather_api(owm, location).get_forecast() forecast = json.loads(Forecast.to_JSON()) casts = [ ] # This is for the weather objects created in the for loop below. for data in forecast['weathers']: # Make an _id for the next Weather to be created, create the weather, # append it to the casts list. instant = data['reference_time'] casts.append(Weather(location, 'forecast', data)) return casts # from Extract.make_instants import find_data # # set database and collection for testing # database = 'test' # collection = 'instant_temp' # # create a dict to hold the instants pulled from the database # instants = {} # data = find_data(client, database, collection) # # add each doc to instants and set its key and _id to the same values # for item in data: # instants[f'{item["_id"]}'] = item['_id']
def get_forecast_latest_weathers(forecast: Forecast) -> List[Weather]: weathers = {} for weather in forecast.get_weathers(): weather_date = weather.get_reference_time('date').strftime('%Y-%M-%d') weathers[weather_date] = weather return list(weathers.values())
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 test_actualize(self): weathers = [Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, {"deg": 252.002, "speed": 1.100}, 57, {"press": 1030.119, "sea_level": 1038.589}, {"temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), # will this time ever be reached? Weather(9999999999, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, {"deg": 103.4, "speed": 4.2}, 12, {"press": 1070.119, "sea_level": 1078.589}, {"temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] f = Forecast("daily", self.__test_reception_time, self.__test_location, weathers) self.assertEqual(2, len(f)) f.actualize() self.assertEqual(1, len(f))
def test_actualize(self): weathers = [ Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, { "deg": 252.002, "speed": 1.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), # will this time ever be reached? Weather(9999999999, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, { "deg": 103.4, "speed": 4.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] f = Forecast("daily", self.__test_reception_time, self.__test_location, weathers) self.assertEqual(2, len(f)) f.actualize() self.assertEqual(1, len(f))
def five_day(location): ''' Get each weather forecast for the corrosponding coordinates :param coords: the latitude and longitude for which that that weather is being forecasted :type coords: tuple containing the latitude and logitude for the forecast :return casts: the five day, every three hours, forecast for the zip code :type casts: list of Weather objects ''' owm = OWM(masta_key) Forecast = get_data_from_weather_api(owm, location).get_forecast() forecast = json.loads(Forecast.to_JSON()) casts = [ ] # This is for the weather objects created in the for loop below. for data in forecast['weathers']: # Make an _id for the next Weather to be created, create the weather, # append it to the casts list. instant = data['reference_time'] casts.append(Weather(location, 'forecast', data)) return casts
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_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)
def test_from_dict_when_server_error(self): with self.assertRaises(APIResponseError): Forecast.from_dict(json.loads(INTERNAL_SERVER_ERROR_JSON))
class TestForecast(unittest.TestCase): __test_reception_time = 1234567 __test_iso_reception_time = "1970-01-15 06:56:07+00:00" __test_date_reception_time = datetime.fromisoformat( __test_iso_reception_time) __test_location = Location('test', 12.3, 43.7, 987, 'IT') __test_weathers = [ Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, { "deg": 252.002, "speed": 1.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), Weather(1378459690, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, { "deg": 103.4, "speed": 4.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] __test_n_weathers = len(__test_weathers) __test_instance = Forecast("daily", __test_reception_time, __test_location, __test_weathers) __bad_json = '{"a": "test", "b": 1.234, "c": [ "hello", "world"] }' __bad_json_2 = '{ "city": {"id": 2643743,' \ '"name": "London","coord": {"lon": -0.12574,"lat": 51.50853},"country": ' \ '"GB","population": 1000000} }' __no_items_found_json = '{"count": "0", "city": {"id": 2643743,' \ '"name": "London","coord": {"lon": -0.12574,"lat": 51.50853},"country": ' \ '"GB","population": 1000000} }' FORECAST_JSON_DUMP = '{"reception_time": 1234567, "interval": "daily", ' \ '"location": {"country": "IT", "name": "test", ' \ '"coordinates": {"lat": 43.7, "lon": 12.3}, "ID": 987}, ' \ '"weathers": [{"status": "Clouds", ' \ '"visibility_distance": 1000, "humidity": 57, "clouds": 67,' \ ' "temperature": {"temp_kf": -1.899, "temp_max": 296.098, ' \ '"temp": 294.199, "temp_min": 294.199}, "dewpoint": 300.0,' \ ' "snow": {"all": 0}, "detailed_status": "Overcast clouds",' \ ' "reference_time": 1378459200, "weather_code": 804, ' \ '"humidex": 298.0, "rain": {"all": 20}, ' \ '"sunset_time": 1378496400, "pressure": {"press": 1030.119,' \ ' "sea_level": 1038.589}, "sunrise_time": 1378449600, ' \ '"heat_index": 296.0, "weather_icon_name": "04d", "wind": ' \ '{"speed": 1.1, "deg": 252.002}, "utc_offset": null, "uvi": null}, {"status": "Clear", ' \ '"visibility_distance": 1000, "humidity": 12, ' \ '"clouds": 23, "temperature": {"temp_kf": -1.899, ' \ '"temp_max": 299.0, "temp": 297.199, "temp_min": 295.6}, ' \ '"dewpoint": 300.0, "snow": {"all": 0}, "detailed_status": ' \ '"Sky is clear", "reference_time": 1378459690, ' \ '"weather_code": 804, "humidex": 298.0, "rain": {"all": 10},' \ ' "sunset_time": 1378496480, "pressure": ' \ '{"press": 1070.119, "sea_level": 1078.589}, ' \ '"sunrise_time": 1378449510, "heat_index": 296.0, ' \ '"weather_icon_name": "02d", "wind": {"speed": 4.2, ' \ '"deg": 103.4}, "utc_offset": null, "uvi": null}]}' def test_actualize(self): weathers = [ Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, { "deg": 252.002, "speed": 1.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), # will this time ever be reached? Weather(9999999999, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, { "deg": 103.4, "speed": 4.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] f = Forecast("daily", self.__test_reception_time, self.__test_location, weathers) self.assertEqual(2, len(f)) f.actualize() self.assertEqual(1, len(f)) def test_init_fails_when_reception_time_is_negative(self): self.assertRaises(ValueError, Forecast, "3h", -1234567, self.__test_location, self.__test_weathers) def test_get(self): index = 1 self.assertEqual(self.__test_weathers[index], self.__test_instance.get(index)) def test_getters_return_expected_3h_data(self): """ Test either for "3h" forecast and "daily" ones """ instance = Forecast("3h", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.interval, "3h") self.assertEqual(instance.reception_time(), self.__test_reception_time) self.assertEqual(instance.location, self.__test_location) self.assertEqual(instance.weathers, self.__test_weathers) def test_getters_return_expected_daily_data(self): instance = Forecast("daily", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.interval, "daily") self.assertEqual(instance.reception_time(), self.__test_reception_time) self.assertEqual(instance.location, self.__test_location) self.assertEqual(instance.weathers, self.__test_weathers) def test_returning_different_formats_for_reception_time(self): instance = self.__test_instance self.assertEqual(instance.reception_time(timeformat='iso'), self.__test_iso_reception_time) self.assertEqual(instance.reception_time(timeformat='unix'), self.__test_reception_time) self.assertEqual(instance.reception_time(timeformat='date'), self.__test_date_reception_time) def test__iter__(self): instance = self.__test_instance counter = 0 for weather in instance: self.assertTrue(isinstance(weather, Weather)) counter += 1 self.assertEqual(len(instance.weathers), counter) def test__len__(self): self.assertEqual(len(self.__test_instance), len(self.__test_weathers)) def test_from_dict(self): result = self.__test_instance.from_dict( json.loads(THREE_HOURS_FORECAST_JSON)) self.assertTrue(result is not None) self.assertTrue(result.reception_time() is not None) self.assertFalse(result.interval is not None) loc = result.location self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) self.assertTrue(isinstance(result.weathers, list)) for weather in result: self.assertTrue(weather is not None) def test_from_dict_fails_when_JSON_data_is_None(self): with self.assertRaises(ParseAPIResponseError): Forecast.from_dict(None) 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 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_when_server_error(self): with self.assertRaises(APIResponseError): Forecast.from_dict(json.loads(INTERNAL_SERVER_ERROR_JSON)) def test_to_dict(self): expected = json.loads(self.FORECAST_JSON_DUMP) result = self.__test_instance.to_dict() self.assertEqual(expected, result) def test__repr(self): print(self.__test_instance)
def test_most_windy_returning_None(self): fcstr = Forecaster( Forecast("daily", 1379089800, self.__test_location, [self.__test_none_values])) self.assertFalse(fcstr.most_windy())
class TestForecaster(unittest.TestCase): __test_time_1 = 1379090800 __test_start_coverage_iso = "2013-09-13 16:46:40+00" __test_date_start_coverage = datetime.strptime( __test_start_coverage_iso, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC()) __test_middle_1_coverage = 1379226100 __test_middle_1_coverage_iso = "2013-09-15 06:21:40+00" __test_date_middle_1_coverage = datetime.strptime( __test_middle_1_coverage_iso, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC()) __test_time_2 = 1379361400 __test_middle_2_coverage_iso = "2013-09-16 19:56:40+00" __test_date_middle_2_coverage = datetime.strptime( __test_middle_2_coverage_iso, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC()) __test_end_coverage = 1379902600 __test_end_coverage_iso = "2013-09-23 02:16:40+00" __test_date_end_coverage = datetime.strptime( __test_end_coverage_iso, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC()) __test_location = Location('test', 12.3, 43.7, 987, 'IT') __test_weather_rainsnow = Weather(__test_time_1, 1378496400, 1378449600, 67, {"all": 30}, {"all": 1}, { "deg": 252.002, "speed": 4.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Rain", "Light rain", 500, "10d", 1000, 300.0, 298.0, 296.0) __test_weather_clouds = Weather(__test_middle_1_coverage, 1378496480, 1378449510, 23, {"all": 0}, {}, { "deg": 103.4, "speed": 1.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clouds", "Overcast clouds", 804, "02d", 1000, 300.0, 298.0, 296.0) __test_weather_sun_1 = Weather(__test_time_2, 1378496480, 1378449510, 5, {"all": 0}, {}, { "deg": 103.4, "speed": 1.2 }, 12, { "press": 1090.119, "sea_level": 1078.589 }, { "temp": 299.199, "temp_kf": -1.899, "temp_max": 301.0, "temp_min": 297.6 }, "Clear", "Sky is clear", 800, "01d", 1000, 300.0, 298.0, 296.0) __test_weather_sun_2 = Weather(__test_end_coverage, 1378496480, 1378449510, 5, {"all": 0}, {}, { "deg": 99.4, "speed": 0.8 }, 7, { "press": 1091.119, "sea_level": 1079.589 }, { "temp": 299.599, "temp_kf": -1.899, "temp_max": 301.9, "temp_min": 298.0 }, "Clear", "Sky is clear", 800, "01d", 1000, 300.0, 298.0, 296.0) __test_weather_storm = Weather(__test_end_coverage, 1378496480, 1378449510, 5, {"all": 0}, {}, { "deg": 99.4, "speed": 0.8 }, 7, { "press": 1071.119, "sea_level": 1059.589 }, { "temp": 299.599, "temp_kf": -1.899, "temp_max": 301.9, "temp_min": 298.0 }, "Storm", "Violent storm", 961, "01d", 1000, 300.0, 298.0, 296.0) __test_weather_hurricane = Weather(__test_end_coverage, 1378496480, 1378449510, 5, {"all": 0}, {}, { "deg": 99.4, "speed": 0.8 }, 7, { "press": 1071.119, "sea_level": 1059.589 }, { "temp": 299.599, "temp_kf": -1.899, "temp_max": 301.9, "temp_min": 298.0 }, "Hurricane", "Hurricane", 962, "01d", 1000, 300.0, 298.0, 296.0) __test_none_values = Weather(__test_end_coverage, 1378496480, 1378449510, 5, {}, {}, {}, 7, { "press": 1091.119, "sea_level": 1079.589 }, { "temp": 299.599, "temp_kf": -1.899 }, "Clear", "Sky is clear", 800, "01d", 1000, 300.0, 298.0, 296.0) __test_weathers = [ __test_weather_rainsnow, __test_weather_clouds, __test_weather_sun_1, __test_weather_sun_2, __test_weather_storm, __test_weather_hurricane ] __test_forecast = Forecast("daily", 1379089800, __test_location, __test_weathers) __test_instance = Forecaster(__test_forecast) def test_getter_returns_expected_data(self): self.assertEqual(self.__test_instance.get_forecast(), self.__test_forecast) def test_when_starts_returning_different_timeformats(self): self.assertEqual(self.__test_instance.when_starts(timeformat='iso'), self.__test_start_coverage_iso) self.assertEqual(self.__test_instance.when_starts(timeformat='unix'), self.__test_time_1) self.assertEqual(self.__test_instance.when_starts(timeformat='date'), self.__test_date_start_coverage) def test_when_ends_returning_different_timeformats(self): self.assertEqual(self.__test_instance.when_ends(timeformat='iso'), self.__test_end_coverage_iso) self.assertEqual(self.__test_instance.when_ends(timeformat='unix'), self.__test_end_coverage) self.assertEqual(self.__test_instance.when_ends(timeformat='date'), self.__test_date_end_coverage) def test_will_have_rain(self): self.assertTrue(self.__test_instance.will_have_rain()) def test_will_have_sun(self): self.assertTrue(self.__test_instance.will_have_sun()) def test_will_have_clear(self): self.assertTrue(self.__test_instance.will_have_clear()) def test_will_have_clouds(self): self.assertTrue(self.__test_instance.will_have_clouds()) def test_will_have_fog(self): self.assertFalse(self.__test_instance.will_have_fog()) def test_will_have_snow(self): self.assertFalse(self.__test_instance.will_have_snow()) def test_will_have_storm(self): self.assertTrue(self.__test_instance.will_have_storm()) def test_will_have_tornado(self): self.assertFalse(self.__test_instance.will_have_tornado()) def test_will_have_hurricane(self): self.assertTrue(self.__test_instance.will_have_hurricane()) def test_when_rain(self): self.assertEqual([self.__test_weather_rainsnow], self.__test_instance.when_rain()) def test_when_sun(self): self.assertEqual( [self.__test_weather_sun_1, self.__test_weather_sun_2], self.__test_instance.when_sun()) def test_when_clear(self): self.assertEqual( [self.__test_weather_sun_1, self.__test_weather_sun_2], self.__test_instance.when_clear()) def test_when_clouds(self): self.assertEqual([self.__test_weather_clouds], self.__test_instance.when_clouds()) def test_when_fog(self): self.assertFalse(self.__test_instance.when_fog()) def test_when_snow(self): self.assertFalse(self.__test_instance.when_snow()) def test_when_storm(self): self.assertTrue(self.__test_instance.when_storm()) def test_when_tornado(self): self.assertFalse(self.__test_instance.when_tornado()) def test_when_hurricane(self): self.assertTrue(self.__test_instance.when_hurricane()) def test_will_be_rainy_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertTrue(self.__test_instance.will_be_rainy_at(time_1)) self.assertFalse(self.__test_instance.will_be_rainy_at(time_2)) self.assertFalse(self.__test_instance.will_be_rainy_at(time_3)) def test_will_be_rainy_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_rainy_at, self.__test_instance, 45.7) def test_will_be_sunny_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_sunny_at(time_1)) self.assertFalse(self.__test_instance.will_be_sunny_at(time_2)) self.assertTrue(self.__test_instance.will_be_sunny_at(time_3)) def test_will_be_sunny_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_sunny_at, self.__test_instance, 45.7) def test_will_be_clear_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_clear_at(time_1)) self.assertFalse(self.__test_instance.will_be_clear_at(time_2)) self.assertTrue(self.__test_instance.will_be_clear_at(time_3)) def test_will_be_clear_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_clear_at, self.__test_instance, 45.7) def test_will_be_snowy_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_snowy_at(time_1)) self.assertFalse(self.__test_instance.will_be_snowy_at(time_2)) self.assertFalse(self.__test_instance.will_be_snowy_at(time_3)) def test_will_be_snowy_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_snowy_at, self.__test_instance, 45.7) def test_will_be_cloudy_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_cloudy_at(time_1)) self.assertTrue(self.__test_instance.will_be_cloudy_at(time_2)) self.assertFalse(self.__test_instance.will_be_cloudy_at(time_3)) def test_will_be_cloudy_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_cloudy_at, self.__test_instance, 45.7) def test_will_be_foggy_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_foggy_at(time_1)) self.assertFalse(self.__test_instance.will_be_foggy_at(time_2)) self.assertFalse(self.__test_instance.will_be_foggy_at(time_3)) def test_will_be_foggy_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_foggy_at, self.__test_instance, 45.7) def test_will_be_stormy_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_stormy_at(time_1)) self.assertFalse(self.__test_instance.will_be_stormy_at(time_2)) self.assertFalse(self.__test_instance.will_be_stormy_at(time_3)) def test_will_be_stormy_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_stormy_at, self.__test_instance, 45.7) def test_will_be_tornado_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_tornado_at(time_1)) self.assertFalse(self.__test_instance.will_be_tornado_at(time_2)) self.assertFalse(self.__test_instance.will_be_tornado_at(time_3)) def test_will_be_tornado_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_tornado_at, self.__test_instance, 45.7) def test_will_be_hurricane_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertFalse(self.__test_instance.will_be_hurricane_at(time_1)) self.assertFalse(self.__test_instance.will_be_hurricane_at(time_2)) self.assertFalse(self.__test_instance.will_be_hurricane_at(time_3)) def test_will_be_hurricane_at_fails_with_bad_parameters(self): self.assertRaises(TypeError, Forecaster.will_be_hurricane_at, self.__test_instance, 45.7) def test_get_weather_at(self): time_1 = datetime(2013, 9, 13, 16, 47, 0) time_2 = 1379226110 time_3 = "2013-09-16 19:56:50+00" self.assertEqual(self.__test_instance.get_weather_at(time_1), self.__test_weather_rainsnow) self.assertEqual(self.__test_instance.get_weather_at(time_2), self.__test_weather_clouds) self.assertEqual(self.__test_instance.get_weather_at(time_3), self.__test_weather_sun_1) def test_get_weather_at_fails_with_bad_parameter(self): self.assertRaises(TypeError, Forecaster.get_weather_at, self.__test_instance, 45.7) def test_most_hot(self): self.assertEqual(self.__test_weather_sun_2, self.__test_instance.most_hot()) def test_most_hot_returning_None(self): fcstr = Forecaster( Forecast("daily", 1379089800, self.__test_location, [self.__test_none_values])) self.assertFalse(fcstr.most_hot()) def test_most_cold(self): self.assertEqual(self.__test_weather_rainsnow, self.__test_instance.most_cold()) def test_most_cold_returning_None(self): fcstr = Forecaster( Forecast("daily", 1379089800, self.__test_location, [self.__test_none_values])) self.assertFalse(fcstr.most_hot()) def test_most_humid(self): self.assertEqual(self.__test_weather_rainsnow, self.__test_instance.most_humid()) def test_most_rainy(self): self.assertEqual(self.__test_weather_rainsnow, self.__test_instance.most_rainy()) def test_most_snowy(self): self.assertEqual(self.__test_weather_rainsnow, self.__test_instance.most_snowy()) def test_most_rainy_returning_None(self): fcstr = Forecaster( Forecast("daily", 1379089800, self.__test_location, [self.__test_none_values])) self.assertFalse(fcstr.most_rainy()) def test_most_windy(self): self.assertEqual(self.__test_weather_rainsnow, self.__test_instance.most_windy()) def test_most_windy_returning_None(self): fcstr = Forecaster( Forecast("daily", 1379089800, self.__test_location, [self.__test_none_values])) self.assertFalse(fcstr.most_windy())
class TestForecast(unittest.TestCase): __test_reception_time = 1234567 __test_iso_reception_time = "1970-01-15 06:56:07+00" __test_date_reception_time = datetime.strptime( __test_iso_reception_time, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC()) __test_location = Location('test', 12.3, 43.7, 987, 'IT') __test_weathers = [ Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, { "deg": 252.002, "speed": 1.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), Weather(1378459690, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, { "deg": 103.4, "speed": 4.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] __test_n_weathers = len(__test_weathers) __test_instance = Forecast("daily", __test_reception_time, __test_location, __test_weathers) def test_actualize(self): weathers = [ Weather(1378459200, 1378496400, 1378449600, 67, {"all": 20}, {"all": 0}, { "deg": 252.002, "speed": 1.100 }, 57, { "press": 1030.119, "sea_level": 1038.589 }, { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 }, "Clouds", "Overcast clouds", 804, "04d", 1000, 300.0, 298.0, 296.0), # will this time ever be reached? Weather(9999999999, 1378496480, 1378449510, 23, {"all": 10}, {"all": 0}, { "deg": 103.4, "speed": 4.2 }, 12, { "press": 1070.119, "sea_level": 1078.589 }, { "temp": 297.199, "temp_kf": -1.899, "temp_max": 299.0, "temp_min": 295.6 }, "Clear", "Sky is clear", 804, "02d", 1000, 300.0, 298.0, 296.0) ] f = Forecast("daily", self.__test_reception_time, self.__test_location, weathers) self.assertEqual(2, len(f)) f.actualize() self.assertEqual(1, len(f)) def test_init_fails_when_reception_time_is_negative(self): self.assertRaises(ValueError, Forecast, "3h", -1234567, self.__test_location, self.__test_weathers) def test_get(self): index = 1 self.assertEqual(self.__test_weathers[index], self.__test_instance.get(index)) def test_accessors_interval_property(self): former_interval = self.__test_instance.get_interval() self.__test_instance.set_interval("3h") result = self.__test_instance.get_interval() self.__test_instance.set_interval(former_interval) self.assertEqual("3h", result) def test_getters_return_expected_3h_data(self): """ Test either for "3h" forecast and "daily" ones """ instance = Forecast("3h", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.get_interval(), "3h") self.assertEqual(instance.get_reception_time(), self.__test_reception_time) self.assertEqual(instance.get_location(), self.__test_location) self.assertEqual(instance.get_weathers(), self.__test_weathers) def test_getters_return_expected_daily_data(self): instance = Forecast("daily", self.__test_reception_time, self.__test_location, self.__test_weathers) self.assertEqual(instance.get_interval(), "daily") self.assertEqual(instance.get_reception_time(), self.__test_reception_time) self.assertEqual(instance.get_location(), self.__test_location) self.assertEqual(instance.get_weathers(), self.__test_weathers) def test_returning_different_formats_for_reception_time(self): instance = self.__test_instance self.assertEqual(instance.get_reception_time(timeformat='iso'), self.__test_iso_reception_time) self.assertEqual(instance.get_reception_time(timeformat='unix'), self.__test_reception_time) self.assertEqual(instance.get_reception_time(timeformat='date'), self.__test_date_reception_time) def test_count_weathers(self): instance = self.__test_instance self.assertEqual(instance.count_weathers(), self.__test_n_weathers) def test_forecast_iterator(self): instance = self.__test_instance counter = 0 for weather in instance: self.assertTrue(isinstance(weather, Weather)) counter += 1 self.assertEqual(instance.count_weathers(), counter) def test__len__(self): self.assertEqual(len(self.__test_instance), len(self.__test_weathers)) # Test JSON and XML comparisons by ordering strings (this overcomes # interpeter-dependant serialization of XML/JSON objects) def test_to_JSON(self): ordered_base_json = ''.join(sorted(FORECAST_JSON_DUMP)) ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON())) self.assertEqual(ordered_base_json, ordered_actual_json) def test_to_XML(self): ordered_base_xml = ''.join(sorted(FORECAST_XML_DUMP)) ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML())) self.assertEqual(ordered_base_xml, ordered_actual_xml)