def test_get_subscription_type(self): owm_free = OWM25({}) self.assertEqual(owm_free.get_subscription_type(), 'free') owm_paid = OWM25(self.__test_parsers, API_key='xyz', subscription_type='pro') self.assertEqual(owm_paid.get_subscription_type(), 'pro')
def test_obfuscate_API_key(self): API_key = '22e28da2669c4283acdbd9cfa7dc0903' expected = '************************a7dc0903' instance = OWM25(self.__test_parsers, API_key) self.assertEqual(expected, instance._obfuscate_API_key()) instance = OWM25(self.__test_parsers, None) self.assertIsNone(instance._obfuscate_API_key())
def test_encode_string(self): name = 'testname' if sys.version_info > (3, 0): result = OWM25._encode_string(name) self.assertEquals(result, name) else: # Python 2 result = OWM25._encode_string(name) try: result.decode('ascii') except: self.fail()
def test_assert_is_string_or_unicode(self): a_string = 'test' a_non_string = 123 OWM25._assert_is_string_or_unicode(a_string) self.assertRaises(AssertionError, OWM25._assert_is_string_or_unicode, a_non_string) try: # only for Python 2 unicode_value = unicode('test') OWM25._assert_is_string_or_unicode(unicode_value) except: pass
def OWM(API_key=None, version=constants.LATEST_OWM_API_VERSION, config_module=None, language=None): """ A parametrized factory method returning a global OWM instance that represents the desired OWM web API version (or the currently supported one if no version number is specified) :param API_key: the OWM web API key (``None`` by default) :type API_key: str :param version: the OWM web API version. Defaults to ``None``, which means use the latest web API version :type version: str :param config_module: the Python path of the configuration module you want to provide for instantiating the library. Defaults to ``None``, which means use the default configuration values for the web API version support you are currently requesting. Please be aware that malformed user-defined configuration modules can lead to unwanted behaviour! :type config_module: str (eg: 'mypackage.mysubpackage.myconfigmodule') :param language: the language in which you want text results to be returned. It's a two-characters string, eg: "en", "ru", "it". Defaults to: ``None``, which means use the default language. :type language: str :returns: an instance of a proper *OWM* subclass :raises: *ValueError* when unsupported OWM API versions are provided """ if version == "2.5": if config_module is None: config_module = "pyowm.webapi25.configuration25" cfg_module = __import__(config_module, fromlist=['']) from pyowm.webapi25.owm25 import OWM25 if language is None: language = cfg_module.language return OWM25(cfg_module.parsers, API_key, cfg_module.cache, language) raise ValueError("Unsupported OWM web API version")
class IntegrationTestsREADMESnippets(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) def test_snippets(self): # Will it be clear tomorrow at this time in Milan (Italy) ? f_milan = self.__owm.daily_forecast("Milan,it") self.assertIsNotNone(f_milan) self.assertTrue(isinstance(f_milan, Forecaster)) tomorrow = timeutils.tomorrow() will_be_clear = f_milan.will_be_clear_at(tomorrow) self.assertTrue(isinstance(will_be_clear, bool)) # Search for current weather in London (UK) o_london = self.__owm.weather_at_place('London,uk') self.assertTrue(isinstance(o_london, Observation)) w_london = o_london.get_weather() self.assertTrue(isinstance(w_london, Weather)) # Weather details self.assertIsNotNone(w_london.get_wind()) self.assertIsNotNone(w_london.get_humidity()) self.assertIsNotNone(w_london.get_temperature('celsius')) # Search current weather observations in the surroundings of # lat=22.57W, lon=43.12S (Rio de Janeiro, BR) os_rio = self.__owm.weather_around_coords(-22.57, -43.12) self.assertIsNotNone(os_rio) self.assertTrue(len(os_rio) > 0) for o in os_rio: self.assertTrue(isinstance(o, Observation))
class IntegrationTestsPollutionAPI30(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) def test_coindex_around_coords(self): """ Test feature: get CO index around geo-coordinates. """ u = self.__owm.coindex_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_co_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_ozone_around_coords(self): """ Test feature: get ozone around geo-coordinates. """ u = self.__owm.ozone_around_coords(0.0, 10.0, start='2016-12-31 12:55:55+00') self.assertIsNotNone(u) self.assertIsNotNone(u.get_du_value()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_no2index_around_coords(self): """ Test feature: get NO2 index around geo-coordinates. """ u = self.__owm.no2index_around_coords(0.0, 10.0, start='2016-12-31 12:55:55+00') self.assertIsNotNone(u) self.assertIsNotNone(u.get_no2_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_so2index_around_coords(self): """ Test feature: get SO2 index around geo-coordinates. """ u = self.__owm.so2index_around_coords(0.0, 10.0, start='2016-12-31 12:55:55+00') self.assertIsNotNone(u) self.assertIsNotNone(u.get_so2_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location())
def OWM(API_key=constants.DEFAULT_API_KEY, version=constants.LATEST_OWM_API_VERSION, config_module=None, language=None, subscription_type=None, use_ssl=None): """ A parametrized factory method returning a global OWM instance that represents the desired OWM web API version (or the currently supported one if no version number is specified) :param API_key: the OWM web API key (defaults to a test value) :type API_key: str :param version: the OWM web API version. Defaults to ``None``, which means use the latest web API version :type version: str :param config_module: the Python path of the configuration module you want to provide for instantiating the library. Defaults to ``None``, which means use the default configuration values for the web API version support you are currently requesting. Please be aware that malformed user-defined configuration modules can lead to unwanted behaviour! :type config_module: str (eg: 'mypackage.mysubpackage.myconfigmodule') :param language: the language in which you want text results to be returned. It's a two-characters string, eg: "en", "ru", "it". Defaults to: ``None``, which means use the default language. :type language: str :param subscription_type: the type of OWM web API subscription to be wrapped. Can be 'free' (free subscription) or 'pro' (paid subscription), Defaults to: 'free' :type subscription_type: str :param use_ssl: whether API calls should be made via SSL or not. Defaults to: False :type use_ssl: bool :returns: an instance of a proper *OWM* subclass :raises: *ValueError* when unsupported OWM API versions are provided """ if version == '2.5': if config_module is None: config_module = "pyowm.webapi25.configuration25" cfg_module = __import__(config_module, fromlist=['']) from pyowm.webapi25.owm25 import OWM25 if language is None: language = cfg_module.language if subscription_type is None: subscription_type = cfg_module.API_SUBSCRIPTION_TYPE if subscription_type not in ['free', 'pro']: subscription_type = 'free' if use_ssl is None: use_ssl = cfg_module.USE_SSL return OWM25(cfg_module.parsers, API_key, cfg_module.cache, language, subscription_type, use_ssl) raise ValueError("Unsupported OWM web API version")
def test_ssl(self): # Try to call the OWM API using SSL and certificate validation owm = OWM25(parsers, API_key=self.__API_key, use_ssl=True) try: # weather API owm.weather_at_place('London,GB') # pollution API owm.uvindex_around_coords(-22.57, -43.12) # stations API mgr = owm.stations_manager() mgr.get_stations() except Exception as e: self.fail(str(e))
def test_cache_limits(self): """ Test that when cache is full, cached elements undergo a turnover and the real OWM web API is invoked """ cache = LRUCache(3, 1000 * 60 * 60) # Only three cacheable elements! wrapped_cache = CacheWrapper(cache) owm = OWM25(parsers, '5746e1a976021a0', wrapped_cache) owm.weather_at_place('London,uk') # Comes from OWM web API owm.weather_at_place('Kiev') # Comes from OWM web API owm.weather_at_place('Madrid') # Comes from OWM web API self.assertEqual(3, wrapped_cache.api_calls()) owm.weather_at_place('London,uk') # Comes from cache owm.weather_at_place('Kiev') # Comes from cache self.assertEqual(3, wrapped_cache.api_calls()) owm.weather_at_place('Tokyo') self.assertEqual(4, wrapped_cache.api_calls()) owm.weather_at_place('Madrid') # Now Madrid should have been pulled out of cache self.assertEqual(5, wrapped_cache.api_calls())
def test_caching_times(self): """ Test that subsequent calls to the same endpoint and with the same query parameters are cached if OWM instance is configured with a non-null cache. """ cache = LRUCache(20, 1000 * 60 * 60) owm = OWM25(parsers, API_key=self.API_KEY, cache=cache) before_request = time() o1 = owm.weather_at_place('London,GB') # Comes from OWM web API after_request = time() o2 = owm.weather_at_place('London,GB') # Comes from cache after_cache_hit_1 = time() owm.weather_at_place('Kiev') # Comes from OWM web API owm.weather_at_coords(-33.936524, 18.503723) # Comes from OWM web API owm.weather_at_coords(-33.936524, 18.503723) # Cached, we don't care owm.weather_at_coords(-33.936524, 18.503723) # Cached, we don't care before_cache_hit_2 = time() o3 = owm.weather_at_place('London,GB') # Comes from cache after_cache_hit_2 = time() #Check results: difference in reception time should not be less than 20 sec self.assertAlmostEqual(o1.get_reception_time(), o2.get_reception_time(), places=None, msg=None, delta=20) self.assertAlmostEqual(o1.get_reception_time(), o3.get_reception_time(), places=None, msg=None, delta=20) #Check times: all cache hit times must be less than the former OWM web #API request time and ratio between cache hit times and request time #should be far less than 1 req_delay = after_request - before_request cache_hit_1_delay = after_cache_hit_1 - after_request cache_hit_2_delay = after_cache_hit_2 - before_cache_hit_2 self.assertTrue(cache_hit_1_delay < req_delay) self.assertTrue(cache_hit_2_delay < req_delay) self.assertTrue(cache_hit_1_delay / req_delay < 1) self.assertTrue(cache_hit_2_delay / req_delay < 1)
class IntegrationTestsUVIndexAPI30(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) def test_uvindex_around_coords(self): """ Test feature: get UV index around geo-coordinates. """ u = self.__owm.uvindex_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_value()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_location()) def test_uvindex_forecast_around_coords(self): """ Test feature: get UV index forecast around geo-coordinates. """ uv_list = self.__owm.uvindex_forecast_around_coords(45, 9) self.assertIsInstance(uv_list, list) for item in uv_list: self.assertIsNotNone(item.get_value()) self.assertIsNotNone(item.get_reception_time()) self.assertIsNotNone(item.get_location()) def test_uvindex_history_around_coords(self): """ Test feature: get UV index history around geo-coordinates. """ start = datetime(2017, 6, 21) end = datetime(2017, 6, 27) uv_list = self.__owm.uvindex_history_around_coords(37.7, -122.37, start, end=end) self.assertIsInstance(uv_list, list) for item in uv_list: self.assertIsNotNone(item.get_value()) self.assertIsNotNone(item.get_reception_time()) self.assertIsNotNone(item.get_location())
def test_caching_prevents_API_calls(self): cache = LRUCache(20, 1000 * 60 * 60) wrapped_cache = CacheWrapper(cache) owm = OWM25(parsers, '5746e1a976021a0', wrapped_cache) self.assertFalse(wrapped_cache.last_request_was_hit()) self.assertEqual(0, wrapped_cache.api_calls()) owm.weather_at_place('London,uk') # Comes from OWM web API self.assertFalse(wrapped_cache.last_request_was_hit()) self.assertEqual(1, wrapped_cache.api_calls()) owm.weather_at_place('London,uk') # Comes from cache self.assertTrue(wrapped_cache.last_request_was_hit()) self.assertEqual(1, wrapped_cache.api_calls()) owm.weather_at_place('London,uk') # Comes from cache again self.assertTrue(wrapped_cache.last_request_was_hit()) self.assertEqual(1, wrapped_cache.api_calls()) owm.weather_at_place('Kiev') # Comes from OWM web API self.assertFalse(wrapped_cache.last_request_was_hit()) self.assertEqual(2, wrapped_cache.api_calls()) owm.weather_at_place('Kiev') # Comes from cache self.assertTrue(wrapped_cache.last_request_was_hit()) self.assertEqual(2, wrapped_cache.api_calls()) owm.weather_at_place('London,uk') # Comes from cache self.assertTrue(wrapped_cache.last_request_was_hit()) self.assertEqual(2, wrapped_cache.api_calls())
class IntegrationTestsAlertAPI30(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) cond1 = Condition(WeatherParametersEnum.HUMIDITY, OperatorsEnum.LESS_THAN, 10) cond2 = Condition(WeatherParametersEnum.CLOUDS, OperatorsEnum.LESS_THAN, 90) start = '2019-07-01 14:17:00+00' end = '2019-07-02 14:17:00+00' # a rectangle around the city of Moscow area1 = geo.Polygon.from_dict({ "type": "Polygon", "coordinates": [[[36.826171875, 55.17259379606185], [39.012451171875, 55.17259379606185], [39.012451171875, 56.15778819063682], [36.826171875, 56.15778819063682], [36.826171875, 55.17259379606185]]] }) # somewhere in Dubai area2 = geo.Point.from_dict({ "type": "Point", "coordinates": [55.29693603515625, 25.186301620540558] }) def test_triggers_CRUD(self): mgr = self.__owm.alert_manager() # check if any previous triggers exist on this account n_old_triggers = len(mgr.get_triggers()) # create trigger1 trigger1 = mgr.create_trigger(self.start, self.end, conditions=[self.cond1], area=[self.area1]) # create trigger2 trigger2 = mgr.create_trigger(self.start, self.end, conditions=[self.cond2], area=[self.area2]) # Read all created triggers triggers = mgr.get_triggers() self.assertEqual(n_old_triggers + 2, len(triggers)) # Read one by one result = mgr.get_trigger(trigger1.id) self.assertEqual(trigger1.id, result.id) self.assertEqual(trigger1.start_after_millis, result.start_after_millis) self.assertEqual(trigger1.end_after_millis, result.end_after_millis) self.assertEqual(len(trigger1.conditions), len(result.conditions)) self.assertEqual(len(trigger1.area), len(result.area)) result = mgr.get_trigger(trigger2.id) self.assertEqual(trigger2.id, result.id) self.assertEqual(trigger2.start_after_millis, result.start_after_millis) self.assertEqual(trigger2.end_after_millis, result.end_after_millis) self.assertEqual(len(trigger2.conditions), len(result.conditions)) self.assertEqual(len(trigger2.area), len(result.area)) # Update a trigger modified_trigger2 = copy.deepcopy(trigger2) modified_trigger2.conditions = [self.cond1, self.cond2] mgr.update_trigger(modified_trigger2) result = mgr.get_trigger(modified_trigger2.id) self.assertEqual(modified_trigger2.id, result.id) self.assertEqual(modified_trigger2.start_after_millis, result.start_after_millis) self.assertEqual(modified_trigger2.end_after_millis, result.end_after_millis) self.assertEqual(len(modified_trigger2.area), len(result.area)) # of course, conditions have been modified with respect to former trigger 2 self.assertNotEqual(len(trigger2.conditions), len(result.conditions)) self.assertEqual(len(modified_trigger2.conditions), len(result.conditions)) # Delete triggers one by one mgr.delete_trigger(trigger1) triggers = mgr.get_triggers() self.assertEqual(n_old_triggers + 1, len(triggers)) mgr.delete_trigger(modified_trigger2) triggers = mgr.get_triggers() self.assertEqual(n_old_triggers, len(triggers))
class IntegrationTestsWebAPI25(unittest.TestCase): __owm = OWM25(parsers, '�b02f5370d�76021a0') def test_is_API_online(self): self.assertTrue(self.__owm.is_API_online()) def test_weather_at_place(self): """ Test feature: get currently observed weather at specific location """ o1 = self.__owm.weather_at_place('London,uk') o2 = self.__owm.weather_at_place('Kiev') o3 = self.__owm.weather_at_place('QmFoPIlbf') # Shall be None self.assertTrue(o1 is not None) self.assertTrue(o1.get_reception_time() is not None) loc = o1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat is not None) self.assertTrue(o2 is not None) self.assertTrue(o2.get_reception_time() is not None) loc = o2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat is not None) self.assertFalse(o3 is not None) def test_weather_at_coords(self): """ Test feature: get currently observed weather at specific coordinates """ o1 = self.__owm.weather_at_coords(41.896144, 12.484589) # Rome o2 = self.__owm.weather_at_coords(-33.936524, 18.503723) # Cape Town self.assertTrue(o1) self.assertTrue(o1.get_reception_time()) loc = o1.get_location() self.assertTrue(loc) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat) self.assertTrue(o2) self.assertTrue(o2.get_reception_time()) loc = o2.get_location() self.assertTrue(loc) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat) def test_weather_at_id(self): o1 = self.__owm.weather_at_id(5128581) # New York o2 = self.__owm.weather_at_id(703448) # Kiev' o3 = self.__owm.weather_at_id(99999999) # Shall be None self.assertTrue(o1 is not None) self.assertTrue(o1.get_reception_time() is not None) loc = o1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat is not None) self.assertTrue(o2 is not None) self.assertTrue(o2.get_reception_time() is not None) loc = o2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat is not None) self.assertFalse(o3 is not None) def test_weather_at_places(self): """ Test feature: find currently observed weather for locations matching the specified text search pattern """ # Test using searchtype=accurate o1 = self.__owm.weather_at_places("London", "accurate") o2 = self.__owm.weather_at_places("Paris", "accurate", 2) self.assertTrue(isinstance(o1, list)) for item in o1: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) self.assertTrue(isinstance(o2, list)) self.assertFalse(len(o2) > 2) for item in o2: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) # Test using searchtype=like o3 = self.__owm.weather_at_places("London", "like") o4 = self.__owm.weather_at_places("Paris", "like", 2) self.assertTrue(isinstance(o3, list)) for item in o3: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) self.assertTrue(isinstance(o4, list)) self.assertFalse(len(o4) > 2) for item in o4: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_around_coords(self): """ Test feature: find currently observed weather for locations that are nearby the specified coordinates """ o2 = self.__owm.weather_around_coords(57.0, -2.15) # Scotland self.assertTrue(isinstance(o2, list)) for item in o2: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) o1 = self.__owm.weather_around_coords(57.0, -2.15, 2) # Scotland self.assertTrue(isinstance(o1, list)) for item in o1: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_three_hours_forecast(self): """ Test feature: get 3 hours forecast for a specific location """ fc1 = self.__owm.three_hours_forecast("London,uk") fc2 = self.__owm.three_hours_forecast('Kiev') fc3 = self.__owm.three_hours_forecast('QmFoPIlbf') # Shall be None self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) self.assertFalse(fc3 is not None) def test_three_hours_forecast_at_coords(self): """ Test feature: get 3 hours forecast at a specific geographic coordinate """ # London,uk fc1 = self.__owm.three_hours_forecast_at_coords(51.5073509, -0.1277583) # Kiev fc2 = self.__owm.three_hours_forecast_at_coords(50.4501, 30.5234) self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) with self.assertRaises(ValueError): self.__owm.three_hours_forecast_at_coords(199, 199) def test_three_hours_forecast_at_id(self): """ Test feature: get 3 hours forecast for city ID """ # London,uk fc1 = self.__owm.three_hours_forecast_at_id(2643743) # Kiev fc2 = self.__owm.three_hours_forecast_at_id(703448) # Shall be None fc3 = self.__owm.three_hours_forecast_at_id(99999999) self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) self.assertEqual(fc3, None) def test_daily_forecast(self): """ Test feature: get daily forecast for a specific location """ fc1 = self.__owm.daily_forecast("London,uk") fc2 = self.__owm.daily_forecast('Kiev') fc3 = self.__owm.daily_forecast('QmFoPIlbf') # Shall be None self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) self.assertFalse(fc3 is not None) def test_daily_forecast_at_coords(self): """ Test feature: get daily forecast at a specific geographic coordinate """ fc1 = self.__owm.daily_forecast_at_coords(51.5073509, -0.1277583) # London,uk self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) with self.assertRaises(ValueError): self.__owm.daily_forecast_at_coords(199, 199) def test_daily_forecast_at_id(self): """ Test feature: get daily forecast for a specific city ID """ # London,uk fc1 = self.__owm.daily_forecast_at_id(2643743) # Kiev fc2 = self.__owm.daily_forecast_at_id(703448) # Shall be None fc3 = self.__owm.daily_forecast_at_id(99999999) self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) self.assertFalse(fc3 is not None) def test_weather_history_at_place(self): """ Test feature: get weather history for a specific location """ start_iso = "2013-09-06 09:20:00+00" start_unix = 1378459200 start_date = datetime(2013, 9, 6, 9, 20, 0) end_iso = "2013-09-06 20:26:40+00" end_unix = 1378499200 end_date = datetime(2013, 9, 6, 20, 26, 40) l1 = self.__owm.weather_history_at_place("London") if l1 is not None: for weather in l1: self.assertTrue(weather is not None) l2 = self.__owm.weather_history_at_place('Kiev', start_unix, end_unix) if l2 is not None: for weather in l2: self.assertTrue(weather is not None) l3 = self.__owm.weather_history_at_place('Rome', start_iso, end_iso) if l3 is not None: for weather in l3: self.assertTrue(weather is not None) l4 = self.__owm.weather_history_at_place('Berlin', start_date, end_date) if l4 is not None: for weather in l4: self.assertTrue(weather is not None) l5 = self.__owm.weather_history_at_place('QmFoPIlbf') # Shall be None self.assertTrue(l5 is None) def test_weather_history_at_id(self): """ Test feature: get weather history for a specific city ID """ start_iso = "2013-09-06 09:20:00+00" start_unix = 1378459200 start_date = datetime(2013, 9, 6, 9, 20, 0) end_iso = "2013-09-06 20:26:40+00" end_unix = 1378499200 end_date = datetime(2013, 9, 6, 20, 26, 40) l1 = self.__owm.weather_history_at_id(5128581) # New York if l1 is not None: for weather in l1: self.assertTrue(weather is not None) l2 = self.__owm.weather_history_at_id(703448, start_unix, end_unix) # Kiev if l2 is not None: for weather in l2: self.assertTrue(weather is not None) l3 = self.__owm.weather_history_at_id(703448, start_iso, end_iso) if l3 is not None: for weather in l3: self.assertTrue(weather is not None) l4 = self.__owm.weather_history_at_id(703448, start_date, end_date) if l4 is not None: for weather in l4: self.assertTrue(weather is not None) def test_station_at_coords(self): """ Test feature: get a list of meteostations nearest to a geographical point """ s1 = self.__owm.station_at_coords(51.5073509, -0.1277583, 2) self.assertEqual(2, len(s1)) for station in s1: self.assertTrue(station is not None) self.assertTrue( all(v is not None for v in station.__dict__.values())) with self.assertRaises(ValueError): self.__owm.station_at_coords(51.5073509, 220) with self.assertRaises(ValueError): self.__owm.station_at_coords(220, -0.1277583) with self.assertRaises(ValueError): self.__owm.station_at_coords(51.5073509, -0.1277583, -3) with self.assertRaises(AssertionError): self.__owm.station_at_coords(51.5073509, -0.1277582, 'foo') def test_station_tick_history(self): """ Test feature: get station tick weather history for a specific meteostation """ h1 = self.__owm.station_tick_history(39276) self.assertTrue(h1 is not None) sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_tick_history(39276, limit=2) self.assertTrue(h2 is not None) sh2 = h2.get_station_history() self.assertTrue(sh2 is not None) data2 = sh2.get_measurements() self.assertTrue(data2 is not None) self.assertFalse(len(data2) > 2) h3 = self.__owm.station_tick_history(987654) # Shall be None self.assertFalse(h3 is not None) def test_station_hour_history(self): """ Test feature: get station hour weather history for a specific meteostation """ h1 = self.__owm.station_hour_history(123) self.assertTrue(h1 is not None) sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_hour_history(987654) # Shall be None self.assertFalse(h2 is not None) def test_station_day_history(self): """ Test feature: get station hour weather history for a specific meteostation """ h1 = self.__owm.station_day_history(123) self.assertTrue(h1 is not None) sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_day_history(123, limit=3) self.assertTrue(h2 is not None) sh2 = h2.get_station_history() self.assertTrue(sh2 is not None) data2 = sh2.get_measurements() self.assertTrue(data2 is not None) h3 = self.__owm.station_day_history(987654) # Shall be None self.assertFalse(h3 is not None) def test_weather_at_station(self): """ Test feature: get current weather measurement for a specific meteostation """ o = self.__owm.weather_at_station(1000) self.assertTrue(o is not None) self.assertTrue(o.get_reception_time() is not None) weat = o.get_weather() self.assertTrue(weat is not None) def test_weather_at_stations_in_bbox(self): """ Test feature: get current weather observations from meteostations inside of a bounding box determined by geo-coordinates. """ o = self.__owm.weather_at_stations_in_bbox(49.07,8.87,61.26,65.21) self.assertTrue(isinstance(o, list)) for item in o: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) weat = item.get_weather() self.assertTrue(weat is not None)
class TestOWM25(unittest.TestCase): __test_parsers = { 'observation': ObservationParser(), 'observation_list': ObservationListParser(), 'forecast': ForecastParser(), 'weather_history': WeatherHistoryParser(), 'station_history': StationHistoryParser(), 'station': StationParser(), 'station_list': StationListParser(), } __test_instance = OWM25(__test_parsers, 'test_API_key') # Mock functions def mock_httputils_call_API_returning_single_obs(self, API_subset_URL, params_dict): return OBSERVATION_JSON def mock_httputils_call_API_returning_single_station_obs( self, API_subset_URL, params_dict): return STATION_OBSERVATION_JSON def mock_httputils_call_API_ping(self, API_subset_URL, params_dict, API_timeout): return OBSERVATION_JSON def mock_httputils_call_API_failing_ping(self, API_subset_URL, params_dict, API_timeout): return None def mock_httputils_call_API_returning_multiple_obs(self, API_subset_URL, params_dict): return SEARCH_RESULTS_JSON def mock_httputils_call_API_returning_3h_forecast(self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_JSON def mock_httputils_call_API_returning_3h_forecast_with_no_items( self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_NOT_FOUND_JSON def mock_httputils_call_API_returning_daily_forecast_with_no_items( self, API_subset_URL, params_dict): return DAILY_FORECAST_NOT_FOUND_JSON def mock_httputils_call_API_returning_3h_forecast_at_coords( self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_AT_COORDS_JSON def mock_httputils_call_API_returning_3h_forecast_at_id( self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_AT_ID_JSON def mock_httputils_call_API_returning_daily_forecast( self, API_subset_URL, params_dict): return DAILY_FORECAST_JSON def mock_httputils_call_API_returning_daily_forecast_at_coords( self, API_subset_URL, params_dict): return DAILY_FORECAST_AT_COORDS_JSON def mock_httputils_call_API_returning_daily_forecast_at_id( self, API_subset_URL, params_dict): return DAILY_FORECAST_AT_ID_JSON def mock_httputils_call_API_returning_city_weather_history( self, API_subset_URL, params_dict): return CITY_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_tick_weather_history( self, API_subset_URL, params_dict): return STATION_TICK_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_hour_weather_history( self, API_subset_URL, params_dict): return STATION_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_day_weather_history( self, API_subset_URL, params_dict): return STATION_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_history_with_no_items( self, API_subset_URL, params_dict): return STATION_HISTORY_NO_ITEMS_JSON def mock_httputils_call_API_returning_weather_at_stations_in_bbox( self, API_subset_URL, params_dict): return WEATHER_AT_STATION_IN_BBOX_JSON def mock_httputils_call_API_returning_station_at_coords( self, API_subset_URL, params_dict): return STATION_AT_COORDS_JSON # Tests def test_wrong_API_key(self): try: OWM25(self.__test_parsers, 1234) self.fail("Didn't raise AssertionError") except AssertionError: pass def test_API_key_accessors(self): test_API_key = 'G097IueS-9xN712E' owm = OWM25({}) self.assertFalse(owm.get_API_key()) owm.set_API_key(test_API_key) self.assertEqual(owm.get_API_key(), test_API_key) def test_is_API_online(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_ping result = self.__test_instance.is_API_online() OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(result) def test_is_API_online_failure(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_failing_ping result = self.__test_instance.is_API_online() OWMHTTPClient.call_API = ref_to_original_call_API self.assertFalse(result) def test_city_id_registry(self): result = self.__test_instance.city_id_registry() self.assertTrue(result is not None) def test_get_API_version(self): self.assertEqual("2.5", self.__test_instance.get_API_version()) def test_get_version(self): self.assertEqual(PYOWM_VERSION, self.__test_instance.get_version()) def test_language_accessors(self): self.assertEqual("en", self.__test_instance.get_language()) self.__test_instance.set_language("ru") self.assertEqual("ru", self.__test_instance.get_language()) def test_weather_at_place(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_place("London,uk") OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_place_fails_with_wrong_parameters(self): self.assertRaises(AssertionError, OWM25.weather_at_place, \ self.__test_instance, 3) def test_weather_at_coords(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_coords(57.0, -2.15) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_coords_fails_when_coordinates_out_of_bounds(self): """ Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90 """ self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 200, 2.5) def test_weather_at_id(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_id( 5128581) # New York city, US OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_id_fails_when_id_negative(self): self.assertRaises(ValueError, OWM25.weather_at_id, \ self.__test_instance, -156667) def test_weather_at_station(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_single_station_obs result = self.__test_instance.weather_at_station(1000) # station: PAKP OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_station_fails_when_id_negative(self): self.assertRaises(ValueError, OWM25.weather_at_station, \ self.__test_instance, -156667) def test_weather_at_places(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_multiple_obs result = \ self.__test_instance.weather_at_places("London", "accurate") OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) self.assertEqual(2, len(result)) for item in result: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_at_places_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.weather_at_places, \ self.__test_instance, "London", "x") self.assertRaises(ValueError, OWM25.weather_at_places, \ self.__test_instance, "London", "accurate", -5) def test_weather_around_coords(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_multiple_obs result = self.__test_instance.weather_around_coords(57.0, -2.15) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for item in result: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_around_coords_fails_when_coordinates_out_of_bounds(self): """ Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90 """ self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 200, 2.5) def test_weather_around_coords_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, 20.0, -3) def test_three_hours_forecast(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast result = self.__test_instance.three_hours_forecast("London,uk") OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast("London,uk") OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_coords(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_at_coords result = \ self.__test_instance\ .three_hours_forecast_at_coords(51.50853, -0.12574) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_at_coords_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast_at_coords( 51.50853, -0.12574) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_coords_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, -100.0, 0.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 100.0, 0.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 0.0, -200.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 0.0, 200.0) def test_three_hours_forecast_at_id(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_at_id result = self.__test_instance.three_hours_forecast_at_id(2643743) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_at_id_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast_at_id(2643743) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_id_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.three_hours_forecast_at_id, self.__test_instance, -1234) def test_daily_forecast(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast result = self.__test_instance.daily_forecast("London,uk", 2) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_fails_with_wrong_params(self): self.assertRaises(AssertionError, OWM25.daily_forecast, self.__test_instance, 2, 3) self.assertRaises(ValueError, OWM25.daily_forecast, self.__test_instance, "London,uk", -3) def test_daily_forecast_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast('London,uk') OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_daily_forecast_at_coords(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_at_coords result = \ self.__test_instance.daily_forecast_at_coords(51.50853, -0.12574, 2) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_at_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 51.50853, -0.12574, -3) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, -100.0, 0.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 100.0, 0.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 0.0, -200.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 0.0, 200.0) def test_daily_forecast_at_coords_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast_at_coords( 51.50853, -0.12574) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_daily_forecast_at_id(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_at_id result = \ self.__test_instance.daily_forecast_at_id(2643743, 2) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_at_id_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.daily_forecast_at_id, self.__test_instance, -123456, 3) self.assertRaises(ValueError, OWM25.daily_forecast_at_id, self.__test_instance, 123456, -3) def test_daily_forecast_at_id_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast_at_id(123456) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_weather_history_at_place(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_city_weather_history result = self.__test_instance.weather_history_at_place("London,uk") OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for weather in result: self.assertTrue(isinstance(weather, Weather)) def test_weather_history_at_place_fails_with_unordered_time_boundaries( self): self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_place_fails_with_time_boundaries_in_the_future( self): current_time = int(time.time()) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", current_time + 1000, current_time + 2000) def test_weather_history_at_place_fails_with_wrong_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", None, 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", -1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", None, -1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", -999, -888) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", "test", 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, "test") def test_weather_history_at_place_fails_with_wrong_name(self): self.assertRaises(AssertionError, OWM25.weather_history_at_place, self.__test_instance, 1, "test", 1234567) def test_weather_history_at_id(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_city_weather_history result = self.__test_instance.weather_history_at_id(12345) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for weather in result: self.assertTrue(isinstance(weather, Weather)) def test_weather_history_at_id_fails_with_negative_id(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, -12345, "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_id_fails_with_unordered_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_id_fails_with_time_boundaries_in_the_future( self): current_time = int(time.time()) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, current_time + 1000, current_time + 2000) def test_weather_history_at_id_fails_with_wrong_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, None, 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, -1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, None, -1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, -999, -888) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, "test", 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, "test") def test_weather_at_station_in_bbox(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_weather_at_stations_in_bbox results = self.__test_instance\ .weather_at_stations_in_bbox(49.07,8.87,61.26,65.21) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(results, list)) for result in results: self.assertTrue(isinstance(result, Observation)) self.assertTrue(isinstance(result.get_weather(), Weather)) self.assertTrue(isinstance(result.get_location(), Location)) self.assertTrue(result.get_reception_time() is not None) def test_station_tick_history(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_tick_weather_history result = self.__test_instance.station_tick_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_tick_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_tick_history, self.__test_instance, 1234, -3) def test_station_tick_history_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_history_with_no_items result = self.__test_instance.station_tick_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_station_hour_history(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_hour_weather_history result = self.__test_instance.station_hour_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_hour_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_hour_history, self.__test_instance, 1234, -3) def test_station_hour_history_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_history_with_no_items result = self.__test_instance.station_hour_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_station_day_history(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_day_weather_history result = self.__test_instance.station_day_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_day_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_day_history, self.__test_instance, 1234, -3) def test_station_hour_history_when_forecast_not_found(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_history_with_no_items result = self.__test_instance.station_hour_history(1234, limit=4) OWMHTTPClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_station_at_coords(self): ref_to_original_call_API = OWMHTTPClient.call_API OWMHTTPClient.call_API = \ self.mock_httputils_call_API_returning_station_at_coords results = self.__test_instance.station_at_coords( 51.5073509, -0.1277583, 2) OWMHTTPClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(results, list)) for result in results: self.assertTrue(isinstance(result, Station)) self.assertTrue(isinstance(result.get_lon(), float)) self.assertTrue(isinstance(result.get_lat(), float)) self.assertTrue(isinstance(result.get_distance(), float)) self.assertTrue(result.get_name()) self.assertTrue(isinstance(result.get_last_weather(), Weather)) self.assertTrue(isinstance(result.get_station_ID(), int)) self.assertTrue(isinstance(result.get_station_type(), int)) self.assertTrue(isinstance(result.get_status(), int))
class TestOWM25(unittest.TestCase): __test_parsers = { 'observation': ObservationParser(), 'observation_list': ObservationListParser(), 'forecast': ForecastParser(), 'weather_history': WeatherHistoryParser(), 'station_history': StationHistoryParser(), 'station': StationParser(), 'station_list': StationListParser(), 'uvindex': UVIndexParser(), 'coindex': COIndexParser(), 'ozone': OzoneParser() } __test_instance = OWM25(__test_parsers, 'test_API_key') # Mock functions def mock_httputils_call_API_returning_single_obs(self, API_subset_URL, params_dict): return OBSERVATION_JSON def mock_httputils_call_API_returning_single_station_obs(self, API_subset_URL, params_dict): return STATION_OBSERVATION_JSON def mock_httputils_call_API_ping(self, API_subset_URL, params_dict, API_timeout): return OBSERVATION_JSON def mock_httputils_call_API_failing_ping(self, API_subset_URL, params_dict, API_timeout): return None def mock_httputils_call_API_returning_multiple_obs(self, API_subset_URL, params_dict): return SEARCH_RESULTS_JSON def mock_httputils_call_API_returning_3h_forecast(self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_JSON def mock_httputils_call_API_returning_3h_forecast_with_no_items(self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_NOT_FOUND_JSON def mock_httputils_call_API_returning_daily_forecast_with_no_items(self, API_subset_URL, params_dict): return DAILY_FORECAST_NOT_FOUND_JSON def mock_httputils_call_API_returning_3h_forecast_at_coords(self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_AT_COORDS_JSON def mock_httputils_call_API_returning_3h_forecast_at_id(self, API_subset_URL, params_dict): return THREE_HOURS_FORECAST_AT_ID_JSON def mock_httputils_call_API_returning_daily_forecast(self, API_subset_URL, params_dict): return DAILY_FORECAST_JSON def mock_httputils_call_API_returning_daily_forecast_at_coords(self, API_subset_URL, params_dict): return DAILY_FORECAST_AT_COORDS_JSON def mock_httputils_call_API_returning_daily_forecast_at_id(self, API_subset_URL, params_dict): return DAILY_FORECAST_AT_ID_JSON def mock_httputils_call_API_returning_city_weather_history(self, API_subset_URL, params_dict): return CITY_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_tick_weather_history(self, API_subset_URL, params_dict): return STATION_TICK_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_hour_weather_history(self, API_subset_URL, params_dict): return STATION_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_day_weather_history(self, API_subset_URL, params_dict): return STATION_WEATHER_HISTORY_JSON def mock_httputils_call_API_returning_station_history_with_no_items(self, API_subset_URL, params_dict): return STATION_HISTORY_NO_ITEMS_JSON def mock_httputils_call_API_returning_weather_at_stations_in_bbox(self, API_subset_URL, params_dict): return WEATHER_AT_STATION_IN_BBOX_JSON def mock_httputils_call_API_returning_station_at_coords(self, API_subset_URL, params_dict): return STATION_AT_COORDS_JSON def mock_httputils_call_API_returning_weather_history_at_coords(self, API_subset_URL, params_dict): return CITY_WEATHER_HISTORY_JSON def mock_get_uvi_returning_uvindex_around_coords(self, params_dict): return UVINDEX_JSON def mock_get_coi_returning_coindex_around_coords(self, params_dict): return COINDEX_JSON def mock_get_o3_returning_coindex_around_coords(self, params_dict): return OZONE_JSON # Tests def test_encode_string(self): name = 'testname' if sys.version_info > (3, 0): result = OWM25._encode_string(name) self.assertEquals(result, name) else: # Python 2 result = OWM25._encode_string(name) try: result.decode('ascii') except: self.fail() def test_assert_is_string(self): a_string = 'test' a_non_string = 123 OWM25._assert_is_string(a_string) self.assertRaises(AssertionError, OWM25._assert_is_string, a_non_string) def test_assert_is_string_or_unicode(self): a_string = 'test' a_non_string = 123 OWM25._assert_is_string_or_unicode(a_string) self.assertRaises(AssertionError, OWM25._assert_is_string_or_unicode, a_non_string) try: # only for Python 2 unicode_value = unicode('test') OWM25._assert_is_string_or_unicode(unicode_value) except: pass def test_wrong_API_key(self): try: OWM25(self.__test_parsers, 1234) self.fail("Didn't raise AssertionError") except AssertionError: pass def test_API_key_is_mandatory_with_paid_subscription(self): try: owm_paid = OWM25(self.__test_parsers, subscription_type='pro') self.fail("Didn't raise AssertionError") except AssertionError: pass def test_API_key_accessors(self): test_API_key = 'G097IueS-9xN712E' owm = OWM25({}) self.assertFalse(owm.get_API_key()) owm.set_API_key(test_API_key) self.assertEqual(owm.get_API_key(), test_API_key) def test_get_subscription_type(self): owm_free = OWM25({}) self.assertEqual(owm_free.get_subscription_type(), 'free') owm_paid = OWM25(self.__test_parsers, API_key='xyz', subscription_type='pro') self.assertEqual(owm_paid.get_subscription_type(), 'pro') def test_is_API_online(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_ping result = self.__test_instance.is_API_online() WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(result) def test_is_API_online_failure(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_failing_ping result = self.__test_instance.is_API_online() WeatherHttpClient.call_API = ref_to_original_call_API self.assertFalse(result) def test_city_id_registry(self): result = self.__test_instance.city_id_registry() self.assertTrue(result is not None) def test_get_API_version(self): self.assertEqual("2.5", self.__test_instance.get_API_version()) def test_get_version(self): self.assertEqual(PYOWM_VERSION, self.__test_instance.get_version()) def test_language_accessors(self): self.assertEqual("en", self.__test_instance.get_language()) self.__test_instance.set_language("ru") self.assertEqual("ru", self.__test_instance.get_language()) def test_weather_at_place(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_place("London,uk") WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_place_fails_with_wrong_parameters(self): self.assertRaises(AssertionError, OWM25.weather_at_place, \ self.__test_instance, 3) def test_weather_at_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_coords(57.0, -2.15) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_zip_code(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_zip_code("2000", "AU") WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_coords_fails_when_coordinates_out_of_bounds(self): """ Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90 """ self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.weather_at_coords, \ self.__test_instance, 200, 2.5) def test_weather_at_id(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_single_obs result = self.__test_instance.weather_at_id(5128581) # New York city, US WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_id_fails_when_id_negative(self): self.assertRaises(ValueError, OWM25.weather_at_id, \ self.__test_instance, -156667) def test_weather_at_ids(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_multiple_obs result = self.__test_instance.weather_at_ids([5128581, 15647, 78654]) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for obs in result: self.assertTrue(obs is not None) self.assertTrue(isinstance(obs, Observation)) weat = obs.get_weather() self.assertTrue(weat is not None) def test_weather_at_ids_fails_when_wrong_parameters(self): self.assertRaises(AssertionError, OWM25.weather_at_ids, \ self.__test_instance, "test") self.assertRaises(ValueError, OWM25.weather_at_ids, \ self.__test_instance, [-1, 2, 3]) def test_weather_at_station(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_single_station_obs result = self.__test_instance.weather_at_station(1000) # station: PAKP WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Observation)) self.assertTrue(result.get_reception_time() is not None) loc = result.get_location() self.assertTrue(loc is not None) weat = result.get_weather() self.assertTrue(weat is not None) def test_weather_at_station_fails_when_id_negative(self): self.assertRaises(ValueError, OWM25.weather_at_station, \ self.__test_instance, -156667) def test_weather_at_places(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_multiple_obs result = \ self.__test_instance.weather_at_places("London", "accurate") WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) self.assertEqual(2, len(result)) for item in result: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_at_places_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.weather_at_places, \ self.__test_instance, "London", "x") self.assertRaises(ValueError, OWM25.weather_at_places, \ self.__test_instance, "London", "accurate", -5) def test_weather_around_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_multiple_obs result = self.__test_instance.weather_around_coords(57.0, -2.15) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for item in result: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_around_coords_fails_when_coordinates_out_of_bounds(self): """ Test failure when providing: lon < -180, lon > 180, lat < -90, lat > 90 """ self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 200, 2.5) def test_weather_around_coords_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.weather_around_coords, \ self.__test_instance, 43.7, 20.0, -3) def test_three_hours_forecast(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast result = self.__test_instance.three_hours_forecast("London,uk") WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast("London,uk") WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_at_coords result = \ self.__test_instance\ .three_hours_forecast_at_coords(51.50853, -0.12574) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_at_coords_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast_at_coords(51.50853, -0.12574) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_coords_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, -100.0, 0.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 100.0, 0.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 0.0, -200.0) self.assertRaises(ValueError, OWM25.three_hours_forecast_at_coords, self.__test_instance, 0.0, 200.0) def test_three_hours_forecast_at_id(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_at_id result = self.__test_instance.three_hours_forecast_at_id(2643743) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_three_hours_forecast_at_id_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_3h_forecast_with_no_items result = self.__test_instance.three_hours_forecast_at_id(2643743) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_three_hours_forecast_at_id_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.three_hours_forecast_at_id, self.__test_instance, -1234) def test_daily_forecast(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast result = self.__test_instance.daily_forecast("London,uk", 2) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_fails_with_wrong_params(self): self.assertRaises(AssertionError, OWM25.daily_forecast, self.__test_instance, 2, 3) self.assertRaises(ValueError, OWM25.daily_forecast, self.__test_instance, "London,uk", -3) def test_daily_forecast_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast('London,uk') WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_daily_forecast_at_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_at_coords result = \ self.__test_instance.daily_forecast_at_coords(51.50853, -0.12574, 2) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_at_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 51.50853, -0.12574, -3) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, -100.0, 0.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 100.0, 0.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 0.0, -200.0) self.assertRaises(ValueError, OWM25.daily_forecast_at_coords, self.__test_instance, 0.0, 200.0) def test_daily_forecast_at_coords_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast_at_coords(51.50853, -0.12574) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_daily_forecast_at_id(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_at_id result = \ self.__test_instance.daily_forecast_at_id(2643743, 2) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Forecaster)) forecast = result.get_forecast() self.assertTrue(isinstance(forecast, Forecast)) self.assertTrue(forecast.get_interval() is not None) self.assertTrue(forecast.get_reception_time() is not None) self.assertTrue(isinstance(forecast.get_location(), Location)) self.assertEqual(1, len(forecast)) for weather in forecast: self.assertTrue(isinstance(weather, Weather)) def test_daily_forecast_at_id_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.daily_forecast_at_id, self.__test_instance, -123456, 3) self.assertRaises(ValueError, OWM25.daily_forecast_at_id, self.__test_instance, 123456, -3) def test_daily_forecast_at_id_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_daily_forecast_with_no_items result = self.__test_instance.daily_forecast_at_id(123456) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_weather_history_at_place(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_city_weather_history result = self.__test_instance.weather_history_at_place("London,uk") WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for weather in result: self.assertTrue(isinstance(weather, Weather)) def test_weather_history_at_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_weather_history_at_coords result = self.__test_instance.weather_history_at_coords(51.503614, -0.107331) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for weather in result: self.assertTrue(isinstance(weather, Weather)) def test_weather_history_at_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, -3) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, -100.0, 0.0) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 100.0, 0.0) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 0.0, -200.0) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 0.0, 200.0) def test_weather_history_at_coords_fails_with_unordered_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_coords_fails_with_time_boundaries_in_the_future(self): current_time = int(time.time()) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, current_time + 1000, current_time + 2000) def test_weather_history_at_place_fails_with_wrong_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, None, 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, -1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, None, -1234567) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, -999, -888) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, "test", 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_coords, self.__test_instance, 51.50853, -0.12574, 1234567, "test") def test_weather_history_at_place_fails_with_unordered_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_place_fails_with_time_boundaries_in_the_future(self): current_time = int(time.time()) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", current_time + 1000, current_time + 2000) def test_weather_history_at_place_fails_with_wrong_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", None, 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", -1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", None, -1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", -999, -888) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", "test", 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_place, self.__test_instance, "London,uk", 1234567, "test") def test_weather_history_at_place_fails_with_wrong_name(self): self.assertRaises(AssertionError, OWM25.weather_history_at_place, self.__test_instance, 1, "test", 1234567) def test_weather_history_at_id(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_city_weather_history result = self.__test_instance.weather_history_at_id(12345) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, list)) for weather in result: self.assertTrue(isinstance(weather, Weather)) def test_weather_history_at_id_fails_with_negative_id(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, -12345, "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_id_fails_with_unordered_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, "2013-09-06 20:26:40+00", "2013-09-06 09:20:00+00") def test_weather_history_at_id_fails_with_time_boundaries_in_the_future(self): current_time = int(time.time()) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, current_time + 1000, current_time + 2000) def test_weather_history_at_id_fails_with_wrong_time_boundaries(self): self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, None, 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, -1234567, None) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, None, -1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, -999, -888) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, "test", 1234567) self.assertRaises(ValueError, OWM25.weather_history_at_id, self.__test_instance, 12345, 1234567, "test") def test_weather_at_station_in_bbox(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_weather_at_stations_in_bbox results = self.__test_instance\ .weather_at_stations_in_bbox(49.07,8.87,61.26,65.21) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(results, list)) for result in results: self.assertTrue(isinstance(result, Observation)) self.assertTrue(isinstance(result.get_weather(), Weather)) self.assertTrue(isinstance(result.get_location(), Location)) self.assertTrue(result.get_reception_time() is not None) def test_station_tick_history(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_tick_weather_history result = self.__test_instance.station_tick_history(1234, limit=4) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_tick_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_tick_history, self.__test_instance, 1234, -3) def test_station_tick_history_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_history_with_no_items result = self.__test_instance.station_tick_history(1234, limit=4) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_station_hour_history(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_hour_weather_history result = self.__test_instance.station_hour_history(1234, limit=4) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_hour_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_hour_history, self.__test_instance, 1234, -3) def test_station_hour_history_when_forecast_not_found(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_history_with_no_items result = self.__test_instance.station_hour_history(1234, limit=4) WeatherHttpClient.call_API = ref_to_original_call_API self.assertIsNone(result) def test_station_day_history(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_day_weather_history result = self.__test_instance.station_day_history(1234, limit=4) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(result, Historian)) station_history = result.get_station_history() self.assertTrue(isinstance(station_history, StationHistory)) self.assertTrue(isinstance(station_history.get_measurements(), dict)) def test_station_day_history_fails_with_wrong_params(self): self.assertRaises(ValueError, OWM25.station_day_history, self.__test_instance, 1234, -3) def test_station_at_coords(self): ref_to_original_call_API = WeatherHttpClient.call_API WeatherHttpClient.call_API = \ self.mock_httputils_call_API_returning_station_at_coords results = self.__test_instance.station_at_coords(51.5073509, -0.1277583, 2) WeatherHttpClient.call_API = ref_to_original_call_API self.assertTrue(isinstance(results, list)) for result in results: self.assertTrue(isinstance(result, Station)) self.assertTrue(isinstance(result.get_lon(), float)) self.assertTrue(isinstance(result.get_lat(), float)) self.assertTrue(isinstance(result.get_distance(), float)) self.assertTrue(result.get_name()) self.assertTrue(isinstance(result.get_last_weather(), Weather)) self.assertTrue(isinstance(result.get_station_ID(), int)) self.assertTrue(isinstance(result.get_station_type(), int)) self.assertTrue(isinstance(result.get_status(), int)) def test_uvindex_around_coords(self): ref_to_original = UltraVioletHttpClient.get_uvi UltraVioletHttpClient.get_uvi = \ self.mock_get_uvi_returning_uvindex_around_coords result = self.__test_instance.uvindex_around_coords(45, 9) UltraVioletHttpClient.get_uvi = ref_to_original self.assertTrue(isinstance(result, UVIndex)) self.assertIsNotNone(result.get_reference_time()) self.assertIsNotNone(result.get_reception_time()) loc = result.get_location() self.assertIsNotNone(loc) self.assertIsNotNone(loc.get_lat()) self.assertIsNotNone(loc.get_lon()) self.assertIsNotNone(result.get_value()) self.assertIsNotNone(result.get_interval()) def test_uvindex_around_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.uvindex_around_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.uvindex_around_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.uvindex_around_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.uvindex_around_coords, \ self.__test_instance, 200, 2.5) def test_coindex_around_coords(self): ref_to_original = AirPollutionHttpClient.get_coi AirPollutionHttpClient.get_coi = \ self.mock_get_coi_returning_coindex_around_coords result = self.__test_instance.coindex_around_coords(45, 9) AirPollutionHttpClient.get_coi = ref_to_original self.assertTrue(isinstance(result, COIndex)) self.assertIsNotNone(result.get_reference_time()) self.assertIsNotNone(result.get_reception_time()) loc = result.get_location() self.assertIsNotNone(loc) self.assertIsNotNone(loc.get_lat()) self.assertIsNotNone(loc.get_lon()) self.assertIsNotNone(result.get_co_samples()) self.assertIsNotNone(result.get_interval()) def test_coindex_around_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.coindex_around_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.coindex_around_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.coindex_around_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.coindex_around_coords, \ self.__test_instance, 200, 2.5) def test_ozone_around_coords(self): ref_to_original = AirPollutionHttpClient.get_o3 AirPollutionHttpClient.get_o3 = \ self.mock_get_o3_returning_coindex_around_coords result = self.__test_instance.ozone_around_coords(45, 9) AirPollutionHttpClient.get_o3 = ref_to_original self.assertTrue(isinstance(result, Ozone)) self.assertIsNotNone(result.get_reference_time()) self.assertIsNotNone(result.get_reception_time()) loc = result.get_location() self.assertIsNotNone(loc) self.assertIsNotNone(loc.get_lat()) self.assertIsNotNone(loc.get_lon()) self.assertIsNotNone(result.get_du_value()) self.assertIsNotNone(result.get_interval()) def test_ozone_around_coords_fails_with_wrong_parameters(self): self.assertRaises(ValueError, OWM25.ozone_around_coords, \ self.__test_instance, 43.7, -200.0) self.assertRaises(ValueError, OWM25.ozone_around_coords, \ self.__test_instance, 43.7, 200.0) self.assertRaises(ValueError, OWM25.ozone_around_coords, \ self.__test_instance, -200, 2.5) self.assertRaises(ValueError, OWM25.ozone_around_coords, \ self.__test_instance, 200, 2.5)
def test_API_key_accessors(self): test_API_key = 'G097IueS-9xN712E' owm = OWM25({}) self.assertFalse(owm.get_API_key()) owm.set_API_key(test_API_key) self.assertEqual(owm.get_API_key(), test_API_key)
def test_API_key_is_mandatory_with_paid_subscription(self): try: owm_paid = OWM25(self.__test_parsers, subscription_type='pro') self.fail("Didn't raise AssertionError") except AssertionError: pass
def test_wrong_API_key(self): try: OWM25(self.__test_parsers, 1234) self.fail("Didn't raise AssertionError") except AssertionError: pass
def test_assert_is_string(self): a_string = 'test' a_non_string = 123 OWM25._assert_is_string(a_string) self.assertRaises(AssertionError, OWM25._assert_is_string, a_non_string)
class IntegrationTestsWebAPI25(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) def test_is_API_online(self): self.assertTrue(self.__owm.is_API_online()) def test_weather_at_place(self): """ Test feature: get currently observed weather at specific location """ o1 = self.__owm.weather_at_place('London,uk') o2 = self.__owm.weather_at_place('Kiev') self.assertTrue(o1 is not None) self.assertTrue(o1.get_reception_time() is not None) loc = o1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat is not None) self.assertTrue(o2 is not None) self.assertTrue(o2.get_reception_time() is not None) loc = o2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat is not None) def test_weather_at_coords(self): """ Test feature: get currently observed weather at specific coordinates """ o1 = self.__owm.weather_at_coords(41.896144, 12.484589) # Rome o2 = self.__owm.weather_at_coords(-33.936524, 18.503723) # Cape Town self.assertTrue(o1) self.assertTrue(o1.get_reception_time()) loc = o1.get_location() self.assertTrue(loc) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat) self.assertTrue(o2) self.assertTrue(o2.get_reception_time()) loc = o2.get_location() self.assertTrue(loc) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat) def test_weather_at_zipcode(self): """ Test feature: get currently observed weather at specific postcode """ o1 = self.__owm.weather_at_zip_code("94040", "US") self.assertTrue(o1) self.assertTrue(o1.get_reception_time()) loc = o1.get_location() self.assertTrue(loc) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat) def test_weather_at_id(self): o1 = self.__owm.weather_at_id(5128581) # New York o2 = self.__owm.weather_at_id(703448) # Kiev' o3 = self.__owm.weather_at_id(99999999) # Shall be None self.assertTrue(o1 is not None) self.assertTrue(o1.get_reception_time() is not None) loc = o1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat is not None) self.assertTrue(o2 is not None) self.assertTrue(o2.get_reception_time() is not None) loc = o2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat is not None) self.assertFalse(o3 is not None) def test_weather_at_ids(self): # New York, Kiev observations = self.__owm.weather_at_ids([5128581, 703448]) o1 = observations[0] o2 = observations[1] self.assertTrue(o1 is not None) self.assertTrue(o1.get_reception_time() is not None) loc = o1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o1.get_weather() self.assertTrue(weat is not None) self.assertTrue(o2 is not None) self.assertTrue(o2.get_reception_time() is not None) loc = o2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = o2.get_weather() self.assertTrue(weat is not None) def test_weather_at_places(self): """ Test feature: find currently observed weather for locations matching the specified text search pattern """ # Test using searchtype=accurate o1 = self.__owm.weather_at_places("London", "accurate") o2 = self.__owm.weather_at_places("Paris", "accurate", 2) self.assertTrue(isinstance(o1, list)) for item in o1: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) self.assertTrue(isinstance(o2, list)) self.assertFalse(len(o2) > 2) for item in o2: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) # Test using searchtype=like o3 = self.__owm.weather_at_places("London", "like") o4 = self.__owm.weather_at_places("Paris", "like", 2) self.assertTrue(isinstance(o3, list)) for item in o3: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) self.assertTrue(isinstance(o4, list)) self.assertFalse(len(o4) > 2) for item in o4: self.assertTrue(item) self.assertTrue(item.get_reception_time()) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_weather_around_coords(self): """ Test feature: find currently observed weather for locations that are nearby the specified coordinates """ o2 = self.__owm.weather_around_coords(57.0, -2.15) # Scotland self.assertTrue(isinstance(o2, list)) for item in o2: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) o1 = self.__owm.weather_around_coords(57.0, -2.15, 2) # Scotland self.assertTrue(isinstance(o1, list)) for item in o1: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) weat = item.get_weather() self.assertTrue(weat is not None) def test_three_hours_forecast(self): """ Test feature: get 3 hours forecast for a specific location """ fc1 = self.__owm.three_hours_forecast("London,uk") fc2 = self.__owm.three_hours_forecast('Kiev') self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) def test_three_hours_forecast_at_coords(self): """ Test feature: get 3 hours forecast at a specific geographic coordinate """ # London,uk fc1 = self.__owm.three_hours_forecast_at_coords(51.5073509, -0.1277583) # Kiev fc2 = self.__owm.three_hours_forecast_at_coords(50.4501, 30.5234) self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) with self.assertRaises(ValueError): self.__owm.three_hours_forecast_at_coords(199, 199) def test_three_hours_forecast_at_id(self): """ Test feature: get 3 hours forecast for city ID """ # London,uk fc1 = self.__owm.three_hours_forecast_at_id(2643743) # Kiev fc2 = self.__owm.three_hours_forecast_at_id(703448) # Shall be None fc3 = self.__owm.three_hours_forecast_at_id(99999999) self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) self.assertEqual(fc3, None) def test_daily_forecast(self): """ Test feature: get daily forecast for a specific location """ fc1 = self.__owm.daily_forecast("London,uk") fc2 = self.__owm.daily_forecast('Kiev') self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) def test_daily_forecast_at_coords(self): """ Test feature: get daily forecast at a specific geographic coordinate """ fc1 = self.__owm.daily_forecast_at_coords(51.5073509, -0.1277583) # London,uk self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) with self.assertRaises(ValueError): self.__owm.daily_forecast_at_coords(199, 199) def test_daily_forecast_at_id(self): """ Test feature: get daily forecast for a specific city ID """ # London,uk fc1 = self.__owm.daily_forecast_at_id(2643743) # Kiev fc2 = self.__owm.daily_forecast_at_id(703448) try: fc3 = self.__owm.daily_forecast_at_id(99999999) raise AssertionError("APICallError was expected here") except api_call_error.APICallError: pass # Ok! self.assertTrue(fc1) f1 = fc1.get_forecast() self.assertTrue(f1 is not None) self.assertTrue(f1.get_reception_time() is not None) loc = f1.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f1: self.assertTrue(weather is not None) self.assertTrue(fc2 is not None) f2 = fc2.get_forecast() self.assertTrue(f2 is not None) self.assertTrue(f2.get_reception_time() is not None) loc = f2.get_location() self.assertTrue(loc is not None) self.assertTrue(all(v is not None for v in loc.__dict__.values())) for weather in f2: self.assertTrue(weather is not None) def test_weather_history_at_place(self): """ Test feature: get weather history for a specific location """ start_iso = "2013-09-06 09:20:00+00" start_unix = 1378459200 start_date = datetime(2013, 9, 6, 9, 20, 0) end_iso = "2013-09-06 20:26:40+00" end_unix = 1378499200 end_date = datetime(2013, 9, 6, 20, 26, 40) try: l1 = self.__owm.weather_history_at_place("London,UK") if l1 is not None: for weather in l1: self.assertTrue(weather is not None) l2 = self.__owm.weather_history_at_place('Kiev', start_unix, end_unix) if l2 is not None: for weather in l2: self.assertTrue(weather is not None) l3 = self.__owm.weather_history_at_place('Rome', start_iso, end_iso) if l3 is not None: for weather in l3: self.assertTrue(weather is not None) l4 = self.__owm.weather_history_at_place('Berlin', start_date, end_date) if l4 is not None: for weather in l4: self.assertTrue(weather is not None) l5 = self.__owm.weather_history_at_place( 'QmFoPIlbf') # Shall be None self.assertTrue(l5 is None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_weather_history_at_coords(self): try: l1 = self.__owm.weather_history_at_coords(51.5073509, -0.1277583) if l1 is not None: for weather in l1: self.assertTrue(weather is not None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_weather_history_at_id(self): """ Test feature: get weather history for a specific city ID """ try: start_iso = "2013-09-06 09:20:00+00" start_unix = 1378459200 start_date = datetime(2013, 9, 6, 9, 20, 0) end_iso = "2013-09-06 20:26:40+00" end_unix = 1378499200 end_date = datetime(2013, 9, 6, 20, 26, 40) l1 = self.__owm.weather_history_at_id(2756723) # Dongen if l1 is not None: for weather in l1: self.assertTrue(weather is not None) l2 = self.__owm.weather_history_at_id(2756723, start_unix, end_unix) if l2 is not None: for weather in l2: self.assertTrue(weather is not None) l3 = self.__owm.weather_history_at_id(2756723, start_iso, end_iso) if l3 is not None: for weather in l3: self.assertTrue(weather is not None) l4 = self.__owm.weather_history_at_id(2756723, start_date, end_date) if l4 is not None: for weather in l4: self.assertTrue(weather is not None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_station_at_coords(self): """ Test feature: get a list of meteostations nearest to a geographical point """ s1 = self.__owm.station_at_coords(51.5073509, -0.1277583, 2) self.assertEqual(2, len(s1)) for station in s1: self.assertTrue(station is not None) self.assertTrue( all(v is not None for v in station.__dict__.values())) with self.assertRaises(ValueError): self.__owm.station_at_coords(51.5073509, 220) with self.assertRaises(ValueError): self.__owm.station_at_coords(220, -0.1277583) with self.assertRaises(ValueError): self.__owm.station_at_coords(51.5073509, -0.1277583, -3) with self.assertRaises(AssertionError): self.__owm.station_at_coords(51.5073509, -0.1277582, 'foo') def test_station_tick_history(self): """ Test feature: get station tick weather history for a specific meteostation """ try: h1 = self.__owm.station_tick_history(39276) if h1 is not None: sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_tick_history(39276, limit=2) self.assertTrue(h2 is not None) sh2 = h2.get_station_history() self.assertTrue(sh2 is not None) data2 = sh2.get_measurements() self.assertTrue(data2 is not None) self.assertFalse(len(data2) > 2) h3 = self.__owm.station_tick_history(987654) # Shall be None self.assertFalse(h3 is not None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_station_hour_history(self): """ Test feature: get station hour weather history for a specific meteostation """ try: h1 = self.__owm.station_hour_history(123) if h1 is not None: sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_hour_history(987654) # Shall be None self.assertFalse(h2 is not None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_station_day_history(self): """ Test feature: get station hour weather history for a specific meteostation """ try: h1 = self.__owm.station_day_history(123) if h1 is not None: sh1 = h1.get_station_history() self.assertTrue(sh1 is not None) data1 = sh1.get_measurements() self.assertTrue(data1 is not None) self.assertFalse(0, len(data1)) h2 = self.__owm.station_day_history(123, limit=3) self.assertTrue(h2 is not None) sh2 = h2.get_station_history() self.assertTrue(sh2 is not None) data2 = sh2.get_measurements() self.assertTrue(data2 is not None) h3 = self.__owm.station_day_history(987654) # Shall be None self.assertFalse(h3 is not None) except unauthorized_error.UnauthorizedError: pass # it's a paid-level API feature def test_weather_at_station(self): """ Test feature: get current weather measurement for a specific meteostation """ o = self.__owm.weather_at_station(1000) self.assertTrue(o is not None) self.assertTrue(o.get_reception_time() is not None) weat = o.get_weather() self.assertTrue(weat is not None) def test_weather_at_stations_in_bbox(self): """ Test feature: get current weather observations from meteostations inside of a bounding box determined by geo-coordinates. """ o = self.__owm.weather_at_stations_in_bbox(49.07, 8.87, 61.26, 65.21) self.assertTrue(isinstance(o, list)) for item in o: self.assertTrue(item is not None) self.assertTrue(item.get_reception_time() is not None) loc = item.get_location() self.assertTrue(loc is not None) weat = item.get_weather() self.assertTrue(weat is not None) def test_uvindex_around_coords(self): """ Test feature: get UV index around geo-coordinates. """ u = self.__owm.uvindex_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_value()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_location()) def test_coindex_around_coords(self): """ Test feature: get CO index around geo-coordinates. """ u = self.__owm.coindex_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_co_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_ozone_around_coords(self): """ Test feature: get ozone around geo-coordinates. """ u = self.__owm.ozone_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_du_value()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_no2index_around_coords(self): """ Test feature: get NO2 index around geo-coordinates. """ u = self.__owm.no2index_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_no2_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location()) def test_so2index_around_coords(self): """ Test feature: get SO2 index around geo-coordinates. """ u = self.__owm.so2index_around_coords(45, 9) self.assertIsNotNone(u) self.assertIsNotNone(u.get_so2_samples()) self.assertIsNotNone(u.get_reception_time()) self.assertIsNotNone(u.get_reference_time()) self.assertIsNotNone(u.get_interval()) self.assertIsNotNone(u.get_location())
class IntegrationTestsStationsAPI30(unittest.TestCase): __owm = OWM25(parsers, os.getenv('OWM_API_KEY', DEFAULT_API_KEY)) def test_stations_CRUD(self): mgr = self.__owm.stations_manager() # check if any previous station exists on this account n_old_stations = len(mgr.get_stations()) # create stat1 stat1 = mgr.create_station('PYOWM1', 'pyowm_test_station_1', 45.0, 9.0, 189.0) # create stat2 stat2 = mgr.create_station('PYOWM2', 'pyowm_test_station_2', 46.0, 18.0, 50.0) # Read all stations = mgr.get_stations() self.assertEqual(n_old_stations + 2, len(stations)) # Read one by one result = mgr.get_station(stat1.id) self.assertEqual(stat1.id, result.id) self.assertEqual(stat1.external_id, result.external_id) self.assertEquals(stat1.name, result.name) self.assertEquals(stat1.lat, result.lat) self.assertEquals(stat1.lon, result.lon) self.assertEquals(stat1.alt, result.alt) result = mgr.get_station(stat2.id) self.assertEquals(stat2.id, result.id) self.assertEquals(stat2.external_id, result.external_id) self.assertEquals(stat2.name, result.name) self.assertEquals(stat2.lat, result.lat) self.assertEquals(stat2.lon, result.lon) self.assertEquals(stat2.alt, result.alt) # create and bufferize some measurements for station n.1 buf = Buffer(stat1.id) buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505231630, temperature=100, wind_speed=2.1, wind_gust=67, humidex=77)) buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505415230, temperature=100, wind_speed=2.1, wind_gust=67, humidex=77)) buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505429694, temperature=100, wind_speed=2.1, wind_gust=67, humidex=77)) mgr.send_buffer(buf) buf.empty() # read the measurements for station 1 msmts = mgr.get_measurements(stat1.id, 'd', 1505200000, 1505430000) for m in msmts: self.assertEquals(m.station_id, stat1.id) # Update a station modified_stat2 = copy.deepcopy(stat2) modified_stat2.eternal = 'modified_pyowm_test_station_2' modified_stat2.lat = 30.6 mgr.update_station(modified_stat2) result = mgr.get_station(modified_stat2.id) self.assertEquals(modified_stat2.id, result.id) self.assertEquals(modified_stat2.external_id, result.external_id) self.assertEquals(modified_stat2.name, result.name) # of course, lat had been modified self.assertEquals(modified_stat2.lon, result.lon) self.assertEquals(modified_stat2.alt, result.alt) # Delete stations one by one mgr.delete_station(stat1) stations = mgr.get_stations() self.assertEqual(n_old_stations + 1, len(stations)) mgr.delete_station(modified_stat2) stations = mgr.get_stations() self.assertEqual(n_old_stations, len(stations))