def test_places_not_found() -> None: """Test when no places are found.""" client = MeteoFranceClient() list_places = client.search_places("sqdmfkjdsmkf") assert not list_places
def test_workflow(city: str) -> None: """Test classical workflow usage with the Python library.""" # Init client client = MeteoFranceClient() # Search a location from name. list_places = client.search_places(city) if (len(list_places) > 0) : my_place = list_places[0] else : print("Rien trouvé") return "" # Fetch weather forecast for the location my_place_weather_forecast = client.get_forecast_for_place(my_place) # Get the daily forecast my_place_daily_forecast = my_place_weather_forecast.daily_forecast # Fetch weather alerts. if my_place.admin2: my_place_weather_alerts = client.get_warning_current_phenomenoms( my_place.admin2 ) readable_warnings = readeable_phenomenoms_dict( my_place_weather_alerts.phenomenons_max_colors ) return [my_place, readable_warnings["Neige-verglas"], my_place_daily_forecast]
def test_no_rain_expected(requests_mock: Mock) -> None: """Test datecomputation when rain is expected within the hour.""" client = MeteoFranceClient() requests_mock.request( "get", f"{METEOFRANCE_API_URL}/rain", json={ "position": { "lat": 48.807166, "lon": 2.239895, "alti": 76, "name": "Meudon", "country": "FR - France", "dept": "92", "timezone": "Europe/Paris", }, "updated_on": 1589995200, "quality": 0, "forecast": [ {"dt": 1589996100, "rain": 1, "desc": "Temps sec"}, {"dt": 1589996400, "rain": 1, "desc": "Temps sec"}, {"dt": 1589996700, "rain": 1, "desc": "Temps sec"}, {"dt": 1589997000, "rain": 1, "desc": "Temps sec"}, {"dt": 1589997300, "rain": 1, "desc": "Temps sec"}, {"dt": 1589997600, "rain": 1, "desc": "Temps sec"}, {"dt": 1589998200, "rain": 1, "desc": "Temps sec"}, {"dt": 1589998800, "rain": 1, "desc": "Temps sec"}, {"dt": 1589999400, "rain": 1, "desc": "Temps sec"}, ], }, ) rain = client.get_rain(latitude=48.8075, longitude=2.24028) assert rain.next_rain_date_locale() is None
def get_meteo(self): client = MeteoFranceClient() marseille = client.search_places('Marseille')[0] current_forecast = client.get_forecast_for_place(marseille).current_forecast return current_forecast
def test_rain() -> None: """Test rain forecast on a covered zone.""" client = MeteoFranceClient() rain = client.get_rain(latitude=48.8075, longitude=2.24028) assert type(rain.position) == dict assert type(rain.updated_on) == int assert type(rain.quality) == int assert "rain" in rain.forecast[0].keys()
def test_thumbnail() -> None: """Test getting France status weather alert map.""" client = MeteoFranceClient() thumbnail_url = client.get_warning_thumbnail() assert thumbnail_url == ( "http://webservice.meteofrance.com/warning/thumbnail" "?&token=__Wj7dVSTjV9YGu1guveLyDq0g7S7TfTjaHBTPTpO0kj8__&" "domain=france")
def test_currentphenomenons_with_coastal_bulletin(dep: str, res: bool) -> None: """Test getting a complete basic bulletin for coastal department.""" client = MeteoFranceClient() current_phenomenoms = client.get_warning_current_phenomenoms( domain=dep, depth=1, with_costal_bulletin=True) has_coastal_phenomenom = any( phenomenom["phenomenon_id"] == 9 for phenomenom in current_phenomenoms.phenomenons_max_colors) assert has_coastal_phenomenom == res
def test_picture_of_the_day() -> None: """Test weather picture of the day results from API.""" client = MeteoFranceClient() potd = client.get_picture_of_the_day() assert "http://" in potd.image_url assert ".jpg" in potd.image_url assert "http://" in potd.image_hd_url assert ".jpg" in potd.image_hd_url assert potd.description
def test_full_with_coastal_bulletint(dep: str, res: bool) -> None: """Test getting a complete advanced bulletin for coastal department.""" client = MeteoFranceClient() full_phenomenoms = client.get_warning_full(domain=dep, with_costal_bulletin=True) has_coastal_phenomenom = any( phenomenom["phenomenon_id"] == 9 for phenomenom in full_phenomenoms.phenomenons_items) assert has_coastal_phenomenom == res
def test_currentphenomenons(requests_mock: Mock) -> None: """Test basic weather alert results from API.""" client = MeteoFranceClient() requests_mock.request( "get", f"{METEOFRANCE_API_URL}/warning/currentphenomenons", json={ "update_time": 1591279200, "end_validity_time": 1591365600, "domain_id": "32", "phenomenons_max_colors": [ { "phenomenon_id": 6, "phenomenon_max_color_id": 1 }, { "phenomenon_id": 4, "phenomenon_max_color_id": 1 }, { "phenomenon_id": 5, "phenomenon_max_color_id": 3 }, { "phenomenon_id": 2, "phenomenon_max_color_id": 1 }, { "phenomenon_id": 1, "phenomenon_max_color_id": 1 }, { "phenomenon_id": 3, "phenomenon_max_color_id": 2 }, ], }, ) current_phenomenoms = client.get_warning_current_phenomenoms(domain="32", depth=1) assert type(current_phenomenoms.update_time) == int assert type(current_phenomenoms.end_validity_time) == int assert type(current_phenomenoms.domain_id) == str assert "phenomenon_id" in current_phenomenoms.phenomenons_max_colors[ 0].keys() assert current_phenomenoms.get_domain_max_color() == 3
def test_places_with_gps() -> None: """Test a place search by specifying a GPS point to search arround.""" client = MeteoFranceClient() list_places = client.search_places("montreal", "45.50884", "-73.58") assert list_places place = list_places[0] assert place.name == "Montréal" assert place.country == "CA" assert place.admin == "Quebec" assert place.admin2 == "06"
def test_fulls() -> None: """Test advanced weather alert results from API.""" client = MeteoFranceClient() warning_full = client.get_warning_full(domain="31") assert type(warning_full.update_time) == int assert type(warning_full.end_validity_time) == int assert type(warning_full.domain_id) == str assert warning_full.domain_id == "31" assert warning_full.color_max in WARNING_COLOR_LIST assert (warning_full.timelaps[0]["timelaps_items"][0]["color_id"] in WARNING_COLOR_LIST) assert (warning_full.phenomenons_items[0]["phenomenon_max_color_id"] in WARNING_COLOR_LIST)
def test_forecast_place() -> None: """Test weather forecast results from API.""" client = MeteoFranceClient() weather_forecast = client.get_forecast_for_place( place=Place(MOUNTAIN_CITY)) assert type(weather_forecast.position) == dict assert type(weather_forecast.updated_on) == int assert "T" in weather_forecast.daily_forecast[0].keys() assert "humidity" in weather_forecast.daily_forecast[0].keys() assert "rain" in weather_forecast.probability_forecast[0].keys() assert "clouds" in weather_forecast.forecast[0].keys() assert (type( weather_forecast.timestamp_to_locale_time( weather_forecast.daily_forecast[0]["dt"])) == datetime)
def test_places_print() -> None: """Test different way to print Places class.""" client = MeteoFranceClient() place_in_france = client.search_places("montreal")[0] place_not_in_france = client.search_places("montreal", "45.50884", "-73.58")[0] assert (repr(place_in_france) == "<Place(name=Montréal, country=FR, admin=Languedoc-Roussillon)>") assert str(place_in_france) == "Montréal - Languedoc-Roussillon (11) - FR" assert (repr(place_not_in_france) == "<Place(name=Montréal, country=CA, admin=Quebec)>") assert str(place_not_in_france) == "Montréal - Quebec - CA" assert f"I live in {place_not_in_france}" == "I live in Montréal - Quebec - CA"
def test_places() -> None: """Test for simple seach of Place.""" client = MeteoFranceClient() list_places = client.search_places("montreal") assert list_places place = list_places[0] assert place.insee assert place.latitude assert place.longitude assert place.postal_code assert place.name == "Montréal" assert place.country == "FR" assert place.admin == "Languedoc-Roussillon" assert place.admin2 == "11"
def test_forecast_france() -> None: """Test weather forecast results from API.""" client = MeteoFranceClient() weather_forecast = client.get_forecast(latitude=48.8075, longitude=2.24028) now_ts = int(time.time()) assert type(weather_forecast.position) == dict assert type(weather_forecast.updated_on) == int assert "T" in weather_forecast.daily_forecast[0].keys() assert "humidity" in weather_forecast.daily_forecast[0].keys() assert "rain" in weather_forecast.probability_forecast[0].keys() assert "clouds" in weather_forecast.forecast[0].keys() assert (type( weather_forecast.timestamp_to_locale_time( weather_forecast.daily_forecast[0]["dt"])) == datetime) assert abs(weather_forecast.nearest_forecast["dt"] - now_ts) <= 30 * 60 assert now_ts - 3600 <= weather_forecast.current_forecast["dt"] <= now_ts assert (weather_forecast.today_forecast["dt"] == weather_forecast.daily_forecast[0]["dt"])
def test_rain_expected(requests_mock: Mock) -> None: """Test datecomputation when rain is expected within the hour.""" client = MeteoFranceClient() requests_mock.request( "get", f"{METEOFRANCE_API_URL}/rain", json={ "position": { "lat": 48.807166, "lon": 2.239895, "alti": 76, "name": "Meudon", "country": "FR - France", "dept": "92", "timezone": "Europe/Paris", }, "updated_on": 1589995200, "quality": 0, "forecast": [ {"dt": 1589996100, "rain": 1, "desc": "Temps sec"}, {"dt": 1589996400, "rain": 1, "desc": "Temps sec"}, {"dt": 1589996700, "rain": 1, "desc": "Temps sec"}, {"dt": 1589997000, "rain": 2, "desc": "Pluie faible"}, {"dt": 1589997300, "rain": 3, "desc": "Pluie modérée"}, {"dt": 1589997600, "rain": 2, "desc": "Pluie faible"}, {"dt": 1589998200, "rain": 1, "desc": "Temps sec"}, {"dt": 1589998800, "rain": 1, "desc": "Temps sec"}, {"dt": 1589999400, "rain": 1, "desc": "Temps sec"}, ], }, ) rain = client.get_rain(latitude=48.8075, longitude=2.24028) date_rain = rain.next_rain_date_locale() assert str(date_rain) == "2020-05-20 19:50:00+02:00" assert ( str(rain.timestamp_to_locale_time(rain.forecast[3]["dt"])) == "2020-05-20 19:50:00+02:00" )
def test_forecast_world() -> None: """Test weather forecast results from API.""" client = MeteoFranceClient() weather_forecast = client.get_forecast(latitude=45.5016889, longitude=73.567256) now_ts = int(time.time()) assert type(weather_forecast.position) == dict assert type(weather_forecast.updated_on) == int assert "T" in weather_forecast.daily_forecast[0].keys() assert "humidity" in weather_forecast.daily_forecast[0].keys() assert not weather_forecast.probability_forecast assert "clouds" in weather_forecast.forecast[0].keys() assert (type( weather_forecast.timestamp_to_locale_time( weather_forecast.daily_forecast[0]["dt"])) == datetime) assert abs(weather_forecast.nearest_forecast["dt"] - now_ts) == min( abs(x["dt"] - now_ts) for x in weather_forecast.forecast) assert (weather_forecast.current_forecast["dt"] == weather_forecast.nearest_forecast["dt"]) assert (weather_forecast.today_forecast["dt"] == weather_forecast.daily_forecast[0]["dt"])
def test_workflow(city: str) -> None: """Test classical workflow usage with the Python library.""" # Init client client = MeteoFranceClient() # Search a location from name. list_places = client.search_places(city) my_place = list_places[0] # Fetch weather forecast for the location my_place_weather_forecast = client.get_forecast_for_place(my_place) # Get the daily forecast my_place_daily_forecast = my_place_weather_forecast.daily_forecast # If rain in the hour forecast is available, get it. if my_place_weather_forecast.position["rain_product_available"] == 1: my_place_rain_forecast = client.get_rain(my_place.latitude, my_place.longitude) next_rain_dt = my_place_rain_forecast.next_rain_date_locale() if not next_rain_dt: rain_status = "No rain expected in the following hour." else: rain_status = next_rain_dt.strftime("%H:%M") else: rain_status = "No rain forecast available." # Fetch weather alerts. if my_place.admin2: my_place_weather_alerts = client.get_warning_current_phenomenoms( my_place.admin2 ) readable_warnings = readeable_phenomenoms_dict( my_place_weather_alerts.phenomenons_max_colors ) assert type(my_place_daily_forecast) == list assert rain_status assert type(readable_warnings) == dict
def test_rain_not_covered() -> None: """Test rain forecast result on a non covered zone.""" client = MeteoFranceClient() with pytest.raises(requests.HTTPError, match=r"400 .*"): client.get_rain(latitude=45.508, longitude=-73.58)