def test_init_stores_negative_sunrise_time_as_none(self): instance = Weather( self.__test_reference_time, self.__test_sunset_time, -9876543210, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertIsNone(instance.get_sunrise_time())
class TestObservation(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, 'UK') __test_weather = 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) __test_instance = Observation(__test_reception_time, __test_location, __test_weather) def test_init_fails_when_reception_time_is_negative(self): self.assertRaises(ValueError, Observation, -1234567, \ self.__test_location, self.__test_weather) def test_getters_return_expected_data(self): self.assertEqual(self.__test_instance.get_reception_time(), self.__test_reception_time) self.assertEqual(self.__test_instance.get_location(), self.__test_location) self.assertEqual(self.__test_instance.get_weather(), self.__test_weather) def test_returning_different_formats_for_reception_time(self): self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \ self.__test_iso_reception_time) self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \ self.__test_reception_time) self.assertEqual(self.__test_instance.get_reception_time(timeformat='date'), \ self.__test_date_reception_time) # 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(OBSERVATION_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(OBSERVATION_XML_DUMP)) ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML())) self.assertEqual(ordered_base_xml, ordered_actual_xml)
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))
class TestObservation(unittest.TestCase): __test_reception_time = 1234567 __test_iso_reception_time = "1970-01-15 06:56:07+00" __test_location = Location('test', 12.3, 43.7, 987, 'UK') __test_weather = 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) __test_instance = Observation(__test_reception_time, __test_location, __test_weather) def test_init_fails_when_reception_time_is_negative(self): self.assertRaises(ValueError, Observation, -1234567, \ self.__test_location, self.__test_weather) def test_getters_return_expected_data(self): self.assertEqual(self.__test_instance.get_reception_time(), self.__test_reception_time) self.assertEqual(self.__test_instance.get_location(), self.__test_location) self.assertEqual(self.__test_instance.get_weather(), self.__test_weather) def test_returning_different_formats_for_reception_time(self): self.assertEqual(self.__test_instance.get_reception_time(timeformat='iso'), \ self.__test_iso_reception_time) self.assertEqual(self.__test_instance.get_reception_time(timeformat='unix'), \ self.__test_reception_time) # Only test to_JSON and to_XML functions when running Python 2.7 from sys import version_info if version_info[0] < 3: def test_to_JSON(self): self.assertEqual(OBSERVATION_JSON_DUMP, self.__test_instance.to_JSON()) def test_to_XML(self): self.assertEqual(OBSERVATION_XML_DUMP, self.__test_instance.to_XML())
class TestWeatherUtils(unittest.TestCase): __test_time_low = 1379090800 __test_time_low_iso = "2013-09-13 16:46:40+00" __test_time_high = 1379361400 __test_time_high_iso = "2013-09-16 19:56:40+00" __test_registry = WeatherCodeRegistry({ "rain": [{ "start": 1, "end": 100 }, { "start": 200, "end": 600 }], "sun": [{ "start": 750, "end": 850 }] }) __test_weather_rain = Weather(__test_time_low, 1378496400, 1378449600, 67, {"all": 30}, {"all": 0}, { "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_sun = Weather(__test_time_high, 1378496480, 1378449510, 5, {"all": 0}, {"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_weathers = [__test_weather_rain, __test_weather_sun] def test_status_is(self): self.assertTrue( weatherutils.status_is(self.__test_weather_rain, "rain", self.__test_registry)) self.assertFalse( weatherutils.status_is(self.__test_weather_sun, "rain", self.__test_registry)) def test_any_status_is(self): self.assertTrue( weatherutils.any_status_is(self.__test_weathers, "sun", self.__test_registry)) self.assertFalse( weatherutils.any_status_is(self.__test_weathers, "storm", self.__test_registry)) def test_filter_by_status(self): result_1 = weatherutils.filter_by_status(self.__test_weathers, "rain", self.__test_registry) self.assertEqual(1, len(result_1)) self.assertTrue( weatherutils.status_is(result_1[0], "rain", self.__test_registry)) result_2 = weatherutils.filter_by_status(self.__test_weathers, "sun", self.__test_registry) self.assertEqual(1, len(result_2)) self.assertTrue( weatherutils.status_is(result_2[0], "sun", self.__test_registry)) def test_find_closest_weather(self): self.assertEqual( self.__test_weather_rain, weatherutils.find_closest_weather(self.__test_weathers, self.__test_time_low + 200)) self.assertEqual( self.__test_weather_sun, weatherutils.find_closest_weather(self.__test_weathers, self.__test_time_high - 200)) def test_find_closest_weather_with_empty_list(self): self.assertFalse( weatherutils.find_closest_weather([], self.__test_time_low + 200)) def test_find_closest_fails_when_unixtime_not_in_coverage(self): self.assertRaises(NotFoundError, weatherutils.find_closest_weather, self.__test_weathers, self.__test_time_high + 200) def test_is_in_coverage(self): self.assertTrue( weatherutils.is_in_coverage(self.__test_time_low + 200, self.__test_weathers)) self.assertTrue( weatherutils.is_in_coverage(self.__test_time_high - 200, self.__test_weathers)) self.assertTrue( weatherutils.is_in_coverage(self.__test_time_low, self.__test_weathers)) self.assertTrue( weatherutils.is_in_coverage(self.__test_time_high, self.__test_weathers)) self.assertFalse( weatherutils.is_in_coverage(self.__test_time_low - 200, self.__test_weathers)) self.assertFalse( weatherutils.is_in_coverage(self.__test_time_high + 200, self.__test_weathers)) def test_is_in_coverage_with_empty_list(self): self.assertFalse(weatherutils.is_in_coverage(1234567, []))
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)
class TestStation(unittest.TestCase): __test_name = 'KNGU' __test_station_type = 1 __test_status = 50 __test_station_ID = 2865 __test_lat = 36.9375 __test_lon = -76.2893 __test_distance = 18.95 __test_last_weather_instance = 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) __test_instance = Station(__test_name, __test_station_ID, __test_station_type, __test_status, __test_lat, __test_lon, __test_distance, __test_last_weather_instance) def test_init_fails_with_invalid_coords(self): self.assertRaises(ValueError, Station, self.__test_name, self.__test_station_ID, self.__test_station_type, self.__test_status, 120.0, self.__test_lon, self.__test_distance, self.__test_last_weather_instance) self.assertRaises(ValueError, Station, self.__test_name, self.__test_station_ID, self.__test_station_type, self.__test_status, self.__test_lat, 220.0, self.__test_distance, self.__test_last_weather_instance) def test_getters_return_expected_data(self): self.assertEqual(self.__test_name, self.__test_instance.get_name()) self.assertEqual(self.__test_station_type, self.__test_instance.get_station_type()) self.assertEqual(self.__test_status, self.__test_instance.get_status()) self.assertEqual(self.__test_station_ID, self.__test_instance.get_station_ID()) self.assertEqual(self.__test_lat, self.__test_instance.get_lat()) self.assertEqual(self.__test_lon, self.__test_instance.get_lon()) self.assertEqual(self.__test_last_weather_instance, self.__test_instance.get_last_weather()) # Only test to_JSON and to_XML functions when running Python 2.7 from sys import version_info if version_info[0] < 3: def test_to_JSON(self): self.assertEqual(STATION_JSON_DUMP, self.__test_instance.to_JSON()) def test_to_XML(self): self.assertEqual(STATION_XML_DUMP, self.__test_instance.to_XML())
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_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_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_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 TestWeather(unittest.TestCase): __test_reference_time = 1378459200 __test_iso_reference_time = "2013-09-06 09:20:00+00" __test_sunset_time = 1378496400 __test_iso_sunset_time = "2013-09-06 19:40:00+00" __test_sunrise_time = 1378449600 __test_iso_sunrise_time = "2013-09-06 06:40:00+00" __test_clouds = 67 __test_rain = {"all": 20} __test_snow = {"all": 0} __test_wind = {"deg": 252.002, "speed": 1.100} __test_humidity = 57 __test_pressure = {"press": 1030.119, "sea_level": 1038.589} __test_temperature = { "temp": 294.199, "temp_kf": -1.899, "temp_max": 296.098, "temp_min": 294.199 } __test_celsius_temperature = { "temp": 21.049, "temp_kf": -1.899, "temp_max": 22.948, "temp_min": 21.049 } __test_fahrenheit_temperature = { "temp": 69.888, "temp_kf": -1.899, "temp_max": 73.306, "temp_min": 69.888 } __test_status = "Clouds" __test_detailed_status = "Overcast clouds" __test_weather_code = 804 __test_weather_icon_name = "04d" __test_visibility_distance = 1000 __test_dewpoint = 300.0 __test_humidex = 298.0 __test_heat_index = 40.0 __test_instance = Weather(__test_reference_time, __test_sunset_time, __test_sunrise_time, __test_clouds, __test_rain, __test_snow, __test_wind, __test_humidity, __test_pressure, __test_temperature, __test_status, __test_detailed_status, __test_weather_code, __test_weather_icon_name, __test_visibility_distance, __test_dewpoint, __test_humidex, __test_heat_index) def test_init_fails_when_negative_data_provided(self): self.assertRaises( ValueError, Weather, -9876543210, self.__test_sunset_time, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, -9876543210, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, -9876543210, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, self.__test_sunrise_time, -45, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, -16, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, -12, self.__test_dewpoint, self.__test_humidex, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, -10.0, self.__test_heat_index) self.assertRaises( ValueError, Weather, self.__test_reference_time, self.__test_sunset_time, self.__test_sunrise_time, self.__test_clouds, self.__test_rain, self.__test_snow, self.__test_wind, self.__test_humidity, self.__test_pressure, self.__test_temperature, self.__test_status, self.__test_detailed_status, self.__test_weather_code, self.__test_weather_icon_name, self.__test_visibility_distance, self.__test_dewpoint, self.__test_humidex, -10.0) def test_from_dictionary(self): dict1 = { 'clouds': { 'all': 92 }, 'name': 'London', 'coord': { 'lat': 51.50853, 'lon': -0.12574 }, 'sys': { 'country': 'GB', 'sunset': 1378923812, 'sunrise': 1378877413 }, 'weather': [{ 'main': 'Clouds', 'id': 804, 'icon': '04d', 'description': 'overcastclouds' }], 'cod': 200, 'base': 'gdpsstations', 'dt': 1378895177, 'main': { 'pressure': 1022, 'humidity': 75, 'temp_max': 289.82, 'temp': 288.44, 'temp_min': 287.59 }, 'id': 2643743, 'wind': { 'gust': 2.57, 'speed': 1.54, 'deg': 31 }, 'visibility': { 'distance': 1000 }, 'calc': { 'dewpoint': 300.0, 'humidex': 298.0, 'heatindex': 296.0 } } dict2 = { "dt": 1378897200, "temp": { "day": 289.37, "min": 284.88, "max": 289.37, "night": 284.88, "eve": 287.53, "morn": 289.37 }, "pressure": 1025.35, "humidity": 71, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "u10d" }], "speed": 3.76, "deg": 338, "clouds": 48, "rain": 3 } dict3 = { "station": { "name": "KPPQ", "type": 1, "status": 50, "id": 1000, "coord": { "lon": -90.47, "lat": 39.38 } }, "last": { "main": { "temp": 276.15, "pressure": 1031 }, "wind": { "speed": 3.1, "deg": 140 }, "visibility": { "distance": 11265, "prefix": 0 }, "calc": { "dewpoint": 273.15 }, "clouds": [{ "distance": 427, "condition": "SCT" }], "dt": 1417977300 }, "params": ["temp", "pressure", "wind", "visibility"] } result1 = weather_from_dictionary(dict1) self.assertTrue(isinstance(result1, Weather)) self.assertTrue(all(v is not None for v in result1.__dict__.values())) result2 = weather_from_dictionary(dict2) self.assertTrue(isinstance(result2, Weather)) self.assertFalse(all(v is not None for v in result2.__dict__.values())) result3 = weather_from_dictionary(dict3) self.assertTrue(isinstance(result2, Weather)) def test_getters_return_expected_data(self): self.assertEqual(self.__test_instance.get_reference_time(), self.__test_reference_time) self.assertEqual(self.__test_instance.get_sunset_time(), self.__test_sunset_time) self.assertEqual(self.__test_instance.get_sunrise_time(), self.__test_sunrise_time) self.assertEqual(self.__test_instance.get_clouds(), self.__test_clouds) self.assertEqual(self.__test_instance.get_rain(), self.__test_rain) self.assertEqual(self.__test_instance.get_snow(), self.__test_snow) self.assertEqual(self.__test_instance.get_wind(), self.__test_wind) self.assertEqual(self.__test_instance.get_humidity(), self.__test_humidity) self.assertEqual(self.__test_instance.get_pressure(), self.__test_pressure) self.assertEqual(self.__test_instance.get_temperature(), self.__test_temperature) self.assertEqual(self.__test_instance.get_status(), self.__test_status) self.assertEqual(self.__test_instance.get_detailed_status(), self.__test_detailed_status) self.assertEqual(self.__test_instance.get_weather_code(), self.__test_weather_code) self.assertEqual(self.__test_instance.get_weather_icon_name(), self.__test_weather_icon_name) self.assertEqual(self.__test_instance.get_visibility_distance(), self.__test_visibility_distance) self.assertEqual(self.__test_instance.get_dewpoint(), self.__test_dewpoint) self.assertEqual(self.__test_instance.get_humidex(), self.__test_humidex) self.assertEqual(self.__test_instance.get_heat_index(), self.__test_heat_index) def test_get_reference_time_returning_different_formats(self): self.assertEqual( self.__test_instance.get_reference_time(timeformat='iso'), self.__test_iso_reference_time) self.assertEqual( self.__test_instance.get_reference_time(timeformat='unix'), self.__test_reference_time) def test_get_sunset_time_returning_different_formats(self): self.assertEqual( self.__test_instance.get_sunset_time(timeformat='iso'), self.__test_iso_sunset_time) self.assertEqual( self.__test_instance.get_sunset_time(timeformat='unix'), self.__test_sunset_time) def test_get_sunrise_time_returning_different_formats(self): self.assertEqual( self.__test_instance.get_sunrise_time(timeformat='iso'), self.__test_iso_sunrise_time) self.assertEqual( self.__test_instance.get_sunrise_time(timeformat='unix'), self.__test_sunrise_time) def test_get_reference_time_fails_with_unknown_timeformat(self): self.assertRaises(ValueError, Weather.get_reference_time, self.__test_instance, 'xyz') def test_get_sunset_time_fails_with_unknown_timeformat(self): self.assertRaises(ValueError, Weather.get_sunset_time, self.__test_instance, 'xyz') def test_get_sunrise_time_fails_with_unknown_timeformat(self): self.assertRaises(ValueError, Weather.get_sunrise_time, self.__test_instance, 'xyz') def test_returning_different_units_for_temperatures(self): result_kelvin = self.__test_instance.get_temperature(unit='kelvin') result_celsius = self.__test_instance.get_temperature(unit='celsius') result_fahrenheit = self.__test_instance.get_temperature( unit='fahrenheit') for item in self.__test_temperature: self.assertAlmostEqual(result_kelvin[item], self.__test_temperature[item], delta=0.1) self.assertAlmostEqual(result_celsius[item], self.__test_celsius_temperature[item], delta=0.1) self.assertAlmostEqual(result_fahrenheit[item], self.__test_fahrenheit_temperature[item], delta=0.1) def test_get_temperature_fails_with_unknown_units(self): self.assertRaises(ValueError, Weather.get_temperature, self.__test_instance, 'xyz') # 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(WEATHER_JSON_DUMP)) ordered_actual_json = ''.join(sorted(self.__test_instance.to_JSON())) self.assertEqual(ordered_base_json, ordered_actual_json) '''
class TestForecast(unittest.TestCase): __test_reception_time = 1234567 __test_iso_reception_time = "1970-01-15 06:56:07+00" __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_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) 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)) # Only test to_JSON and to_XML functions when running Python 2.7 from sys import version_info if version_info[0] < 3: def test_to_JSON(self): self.assertEqual(FORECAST_JSON_DUMP, self.__test_instance.to_JSON()) def test_to_XML(self): self.assertEqual(FORECAST_XML_DUMP, self.__test_instance.to_XML())
class TestStation(unittest.TestCase): __test_name = 'KNGU' __test_station_type = 1 __test_status = 50 __test_station_ID = 2865 __test_lat = 36.9375 __test_lon = -76.2893 __test_distance = 18.95 __test_last_weather_instance = 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) __test_instance = Station(__test_name, __test_station_ID, __test_station_type, __test_status, __test_lat, __test_lon, __test_distance, __test_last_weather_instance) def test_init_fails_with_invalid_coords(self): self.assertRaises(ValueError, Station, self.__test_name, self.__test_station_ID, self.__test_station_type, self.__test_status, 120.0, self.__test_lon, self.__test_distance, self.__test_last_weather_instance) self.assertRaises(ValueError, Station, self.__test_name, self.__test_station_ID, self.__test_station_type, self.__test_status, self.__test_lat, 220.0, self.__test_distance, self.__test_last_weather_instance) def test_getters_return_expected_data(self): self.assertEqual(self.__test_name, self.__test_instance.get_name()) self.assertEqual(self.__test_station_type, self.__test_instance.get_station_type()) self.assertEqual(self.__test_status, self.__test_instance.get_status()) self.assertEqual(self.__test_station_ID, self.__test_instance.get_station_ID()) self.assertEqual(self.__test_lat, self.__test_instance.get_lat()) self.assertEqual(self.__test_lon, self.__test_instance.get_lon()) self.assertEqual(self.__test_last_weather_instance, self.__test_instance.get_last_weather()) # 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(STATION_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(STATION_XML_DUMP)) ordered_actual_xml = ''.join(sorted(self.__test_instance.to_XML())) self.assertEqual(ordered_base_xml, ordered_actual_xml)