def test_get_weather(self): example_data = { 'coord': { 'lon': 30.21, 'lat': 60.14 }, 'weather': [{ 'main': 'Clear', 'description': 'clear sky' }], 'main': { 'temp': 13, 'pressure': 1023, 'humidity': 58 }, 'wind': { 'speed': 0.81, 'deg': 22.5045 }, 'cod': 200 } self.assertEqual( weather.get_weather(example_data), { 'Weather': example_data['weather'][0]['main'], 'Description': example_data['weather'][0]['description'].title(), 'Temperature, Cel': example_data['main']['temp'], 'Pressure, hPa': example_data['main']['pressure'], 'Humidity, %': example_data['main']['humidity'], 'Wind speed, m/s:': example_data['wind']['speed'] }) print(weather.get_weather(example_data))
def test_invalid_postfix(self): """ Verifies invalid service reaction """ with self.assertRaises(df.ResponseError) as raised_exception: weather.get_weather('London', 'df') self.assertEqual(raised_exception.exception.args[0], 'Code: 404 -- Internal error')
def search(self): """ Requests the coordinates of the addressed address, then asks for the coordinates. :return: Returns the weather, forecast and description of the place found """ location = geo_nominatim.get_coordinates(self.address) description = weather.get_weather(location['coordinates'], 'weather', format_as_json=True) forecast = weather.get_weather(location['coordinates'], 'forecast', format_as_json=True) return description, forecast, location
def test_incorrect_coords_type(self): """ Verifies that the address entered as coordinates, which are incorrect type is correctly processed :return: None """ argument = 56 with self.assertRaises(TypeError) as raised_exception: weather.get_weather(argument, 'weather') self.assertEqual(raised_exception.exception.args[0], f'Wrong location: {argument}!')
def _test_bad_request(self, postfix): """ sends an incorrect request and verifies that the response is correctly processed :param postfix: api service of openweathermap :return: None """ with self.assertRaises(df.ResponseError) as raised_exception: weather.get_weather('Londonx', postfix) self.assertEqual(raised_exception.exception.args[0], 'Code: 404 -- city not found')
def test_incorrect_coords_key(self): """ Verifies that the address entered as coordinates, which contains key-error is correctly processed :return: None """ argument = {'lat': 100, 'ldon': 100} with self.assertRaises(ValueError) as raised_exception: weather.get_weather(argument, 'weather') self.assertEqual(raised_exception.exception.args[0], f'Wrong cords: {argument}!')
def _invalid_api_key(self, postfix): """ sends an incorrect request (Wrong API key )and verifies that the response is correctly processed :param postfix: api service of openweathermap :return: None """ with self.assertRaises(df.ResponseError) as raised_exception: weather.get_weather('London', postfix, api_key='df') self.assertEqual( raised_exception.exception.args[0], 'Code: 401 -- Invalid API key. ' 'Please see http://openweathermap.org/faq#error401 for more info.')
def test_get_current_weather(self, mock_std): example_data = { 'coord': { 'lon': 30.21, 'lat': 60.14 }, 'weather': [{ 'main': 'Clear', 'description': 'clear sky' }], 'main': { 'temp': 13, 'pressure': 1023, 'humidity': 58 }, 'wind': { 'speed': 0.81, 'deg': 22.5045 }, 'cod': 200 } with patch('supertool.weather.get_weather_by_coordinates' ) as mock_weather: mock_weather.return_value = example_data result = weather.get_current_weather('12.05', ('30', '60')) self.assertEqual( mock_std.getvalue(), 'Today'.ljust(10) + 'Right Now'.rjust(32) + f'\n12.05\n' + '\n'.join(f' {k:20}'.ljust(10) + '|' + f'{v:4}'.rjust(20) + '|' for k, v in weather.get_weather(example_data).items()) + '\n') self.assertEqual(result, None)
def _getting_weather_when_response_is_ok(self, address: str or dict) -> None: """ Verifies that the correct response is correctly processed :param address: address which can be string address or geo coordinates :return: """ with patch('requests.get') as mock_get: response = self._get_weather_block("clear sky") # Configure the mock to return a response with an OK status code. # Also, the mock should have a `json()` method. mock_get.return_value = Mock(ok=True) mock_get.return_value.json.return_value = response # Call the service, which will send a request to the server. result = weather.get_weather(address, 'weather') expected_report = ( '--------------------------------------------------\n' ' Current weather in London (GB) \n' '--------------------------------------------------', 'Description clear sky\n' 'temperature (Celsius): 13.81\n' 'pressure (hPa): 1010\n' 'humidity (%): 67\n' 'wind speed (meter/sec): 2.1\n' 'cloudiness (%): 12') self.assertEqual(result, expected_report, 'Incorrect weather description')
def test_getting_weather_forecast_when_response_is_ok(self): """ Verifies that the correct response is correctly processed :return: None """ with patch('requests.get') as mock_get: response = { "city": { "name": "London", "country": "GB", }, "list": [ self._get_weather_block("clear sky"), self._get_weather_block("cloudy") ] } response["list"][0]["dt_txt"] = "2018-05-06 18:00:00" response["list"][1]["dt_txt"] = "2018-05-06 21:00:00" mock_get.return_value = Mock(ok=True) mock_get.return_value.json.return_value = response # Call the service, which will send a request to the server. result = weather.get_weather('London', 'forecast') expected_result = ( '--------------------------------------------------\n' ' The weather forecast in London (GB) \n' '--------------------------------------------------', ' 2018-05-06 18:00:00 \n' 'Description clear sky\n' 'temperature (Celsius): 13.81\n' 'pressure (hPa): 1010\n' 'humidity (%): 67\n' 'wind speed (meter/sec): 2.1\n' 'cloudiness (%): 12\n' ' 2018-05-06 21:00:00 \n' 'Description cloudy\n' 'temperature (Celsius): 13.81\n' 'pressure (hPa): 1010\n' 'humidity (%): 67\n' 'wind speed (meter/sec): 2.1\n' 'cloudiness (%): 12') self.assertEqual(result, expected_result, 'Incorrect weather forecast')
def test_get_weather_without_dict_negative(self): with self.assertRaises(TypeError) as raised_exception: weather.get_weather('reply must be dict') self.assertEqual(raised_exception.exception.args[0], 'reply must be dict')
def _decorator_tester_for_response_error(address): """ Function to test that the decorator does not correctly handle exceptions """ weather.get_weather(address, 'weather')