def test_forecast(self): """Testing that forecasts are formatted correctly""" forecast = forecastio.manual(os.path.join('fixtures', 'us.json')) wd = models.WeatherData(forecast, self.location) self.weatherbot_strings['forecast_endings'] = [] wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) forecast_string = wbs.forecast() self.assertIn(forecast_string, wbs.forecasts) self.weatherbot_strings['forecast_endings'] = ['Test ending!'] self.weatherbot_strings['forecasts'] = ['The forecast for today is {summary_lower} {high}/{low}.'] wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) forecast_string = wbs.forecast() self.assertEqual(forecast_string, 'The forecast for today is mostly cloudy throughout the day. 66ºF/50ºF. Test ending!')
def test_update_weather_data(self): """Testing that new weather data is loaded correctly""" forecast1 = forecastio.manual(os.path.join('fixtures', 'us.json')) wd1 = models.WeatherData(forecast1, self.location) forecast2 = forecastio.manual( os.path.join('fixtures', 'us_cincinnati.json')) wd2 = models.WeatherData(forecast2, self.location) wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd1) self.assertEqual(50.84, wbs.weather_data.apparentTemperature) self.assertIn('51', wbs.normal()) self.assertIn('66ºF', wbs.forecast()) self.assertEqual('normal', wbs.special().type) self.assertEqual(0, len(wd1.alerts)) wbs.set_weather(wd2) self.assertEqual(73.09, wbs.weather_data.apparentTemperature) self.assertIn('73', wbs.normal()) self.assertIn('78ºF', wbs.forecast()) self.assertEqual('moderate-rain', wbs.special().type) alert = wbs.alert(wd2.alerts[0], wd2.timezone) self.assertIn('Severe Thunderstorm Warning', alert) self.assertIn('Wed, Oct 19 at 19:30:00 EDT', alert) self.assertIn( 'https://alerts.weather.gov/cap/wwacapget.php?x=OH12561A63BE38.SevereThunderstormWarning.' '12561A63C2E8OH.ILNSVSILN.f17bc0b3ead1db18bf60532894d9925e', alert)
def test_update_weather_data(self, mock_get): """Testing that new weather data is loaded correctly""" forecast1 = forecastio.manual(os.path.join('fixtures', 'us.json')) wd1 = models.WeatherData(forecast1, self.location) forecast2 = forecastio.manual(os.path.join('fixtures', 'us_cincinnati.json')) wd2 = models.WeatherData(forecast2, self.location) wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd1) self.assertEqual(50.84, wbs.weather_data.apparentTemperature) self.assertIn('51', wbs.normal()) self.assertIn('66ºF', wbs.forecast()) self.assertEqual('normal', wbs.special().type) dt = datetime.datetime.utcfromtimestamp(1475129665) # datetime.datetime(2016, 9, 29, 6, 14, 25) alert = wbs.alert(title='test1', expires=pytz.utc.localize(dt), uri='test.uri1') self.assertIn('test1', alert) self.assertIn('Thu, Sep 29 at 06:14:25 UTC', alert) self.assertIn('test.uri1', alert) wbs.set_weather(wd2) self.assertEqual(73.09, wbs.weather_data.apparentTemperature) self.assertIn('73', wbs.normal()) self.assertIn('78ºF', wbs.forecast()) self.assertEqual('moderate-rain', wbs.special().type) dt = datetime.datetime.utcfromtimestamp(1475129666) # datetime.datetime(2016, 9, 29, 6, 14, 26) alert = wbs.alert(title='test2', expires=pytz.utc.localize(dt), uri='test.uri2') self.assertIn('test2', alert) self.assertIn('Thu, Sep 29 at 06:14:26 UTC', alert) self.assertIn('test.uri2', alert)
def test_normal(self): """Testing that normal events are formatted""" forecast = forecastio.manual(os.path.join('fixtures', 'us.json')) wd = models.WeatherData(forecast, self.location) wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) normal_string = wbs.normal() self.assertIn(normal_string, wbs.normal_conditions)
def test_alert(self): """Testing that alerts are formatted""" wbs = models.WeatherBotString(self.weatherbot_strings) dt = datetime.datetime.utcfromtimestamp(1475129665) # datetime.datetime(2016, 9, 29, 6, 14, 25) alert = wbs.alert(title='title', expires=pytz.utc.localize(dt), uri='test.uri') self.assertIn('Thu, Sep 29 at 06:14:25 UTC', alert) self.assertIn('title', alert) self.assertIn('test.uri', alert)
def test_precipitation(self): """Testing that precipitation conditions are met""" wbs = models.WeatherBotString(self.weatherbot_strings) forecast_us = forecastio.manual(os.path.join('fixtures', 'us.json')) wd = models.WeatherData(forecast_us, self.location) wbs.set_weather(wd) self.assertEqual(wbs.precipitation(), models.Condition(type='none', text='')) wd.precipIntensity = 0.3 wd.precipProbability = 0.5 wd.precipType = 'rain' wbs.set_weather(wd) self.assertEqual(wbs.precipitation(), models.Condition(type='none', text='')) wd.precipIntensity = 0.3 wd.precipProbability = 1 wd.precipType = 'none' wbs.set_weather(wd) self.assertEqual(wbs.precipitation(), models.Condition(type='none', text='')) wd.precipIntensity = 0 wd.precipProbability = 1 wd.precipType = 'rain' wbs.set_weather(wd) self.assertEqual(wbs.precipitation(), models.Condition(type='none', text='')) wd.precipIntensity = 0 wd.precipProbability = 1 wd.precipType = 'none' wbs.set_weather(wd) self.assertEqual(wbs.precipitation(), models.Condition(type='none', text='')) # testing with a few possible conditions wd.precipIntensity = 0.3 wd.precipProbability = 1 wd.precipType = 'rain' wbs.set_weather(wd) precip = wbs.precipitation() self.assertEqual(precip.type, 'moderate-rain') self.assertIn(precip.text, wbs.precipitations['rain']['moderate']) wd.precipIntensity = 0.4 wd.precipProbability = 0.85 wd.precipType = 'snow' wbs.set_weather(wd) precip = wbs.precipitation() self.assertEqual(precip.type, 'heavy-snow') self.assertIn(precip.text, wbs.precipitations['snow']['heavy']) wd.precipIntensity = 0.06 wd.precipProbability = 1 wd.precipType = 'sleet' wbs.set_weather(wd) precip = wbs.precipitation() self.assertEqual(precip.type, 'light-sleet') self.assertIn(precip.text, wbs.precipitations['sleet']['light']) wd.precipIntensity = 0.005 wd.precipProbability = 1 wd.precipType = 'rain' wbs.set_weather(wd) precip = wbs.precipitation() self.assertEqual(precip.type, 'very-light-rain') self.assertIn(precip.text, wbs.precipitations['rain']['very-light'])
def test_alert(self): """Testing that alerts are formatted""" wbs = models.WeatherBotString(self.weatherbot_strings) forecast = forecastio.manual(os.path.join('fixtures', 'ca_alert.json')) location = models.WeatherLocation(50.564167, -111.898889, 'Brooks, Alberta') wd = models.WeatherData(forecast, location) wbs.set_weather(wd) alert = wbs.alert(wd.alerts[0], wd.timezone) self.assertIn('Snowfall Warning', alert) self.assertIn('https://weather.gc.ca/warnings/report_e.html?ab6', alert)
def main(path): """ Main function called when starting weatherBot. The path is to the configuration file. :type path: str :param path: path to configuration file """ # pylint: disable=broad-except,no-member global CACHE load_config(os.path.abspath(path)) initialize_logger(CONFIG['log']['enabled'], CONFIG['log']['log_path']) logging.debug(CONFIG) keys.set_twitter_env_vars() keys.set_darksky_env_vars() CACHE['throttles']['default'] = pytz.utc.localize(datetime.utcnow()).astimezone(pytz.utc) with open(CONFIG['basic']['strings'], 'r') as file_stream: try: weatherbot_strings = yaml.safe_load(file_stream) logging.debug(weatherbot_strings) wb_string = models.WeatherBotString(weatherbot_strings) except yaml.YAMLError as err: logging.error(err, exc_info=True) logging.error('Could not read YAML file, please correct, run yamllint, and try again.') exit() location = CONFIG['default_location'] updated_time = utils.datetime_to_utc('UTC', datetime.utcnow()) - timedelta(minutes=30) try: while True: # check for new location every 30 minutes now_utc = utils.datetime_to_utc('UTC', datetime.utcnow()) if CONFIG['variable_location']['enabled'] and updated_time + timedelta(minutes=30) < now_utc: location = get_location_from_user_timeline(CONFIG['variable_location']['user'], location) updated_time = now_utc forecast = get_forecast_object(location.lat, location.lng, CONFIG['basic']['units'], wb_string.language) if forecast is not None: weather_data = models.WeatherData(forecast, location) if weather_data.valid: CACHE = get_cache() tweet_logic(weather_data, wb_string) CACHE['throttles'] = cleanse_throttles(CACHE['throttles'], now_utc) set_cache(CACHE) time.sleep(CONFIG['basic']['refresh'] * 60) else: time.sleep(60) except Exception as err: logging.error(err) logging.error('We got an exception!', exc_info=True) if CONFIG['basic']['dm_errors']: api = get_tweepy_api() api.send_direct_message(screen_name=api.me().screen_name, text=str(random.randint(0, 9999)) + traceback.format_exc())
def test_dict(self): """Testing that __dict__ returns the correct data""" forecast = forecastio.manual(os.path.join('fixtures', 'us.json')) wd = models.WeatherData(forecast, self.location) wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) self.assertEqual({ 'language': wbs.language, 'weather_data': wbs.weather_data, 'forecasts': wbs.forecasts, 'forecast_endings': wbs.forecasts_endings, 'normal_conditions': wbs.normal_conditions, 'special_conditions': wbs.special_conditions, 'precipitations': wbs.precipitations }, wbs.__dict__())
def test_special(self): """Testing if special events are triggered""" forecast_si = forecastio.manual(os.path.join('fixtures', 'si.json')) forecast_us = forecastio.manual(os.path.join('fixtures', 'us.json')) forecast_ca = forecastio.manual(os.path.join('fixtures', 'ca.json')) forecast_uk2 = forecastio.manual(os.path.join('fixtures', 'uk2.json')) wd = models.WeatherData(forecast_si, self.location) wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) self.assertEqual('normal', wbs.special().type) self.assertEqual('', wbs.special().text) """Testing if wind-chill type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.apparentTemperature = -34 wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) self.assertEqual('wind-chill', wbs.special().type) self.assertIn(wbs.special().text, wbs.special_conditions[wbs.special().type]) wd = models.WeatherData(forecast_us, self.location) wd.apparentTemperature = -30 wbs = models.WeatherBotString(self.weatherbot_strings) wbs.set_weather(wd) self.assertEqual('wind-chill', wbs.special().type) self.assertIn(wbs.special().text, wbs.special_conditions[wbs.special().type]) """Testing if precip type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.precipProbability = 0.9 wd.precipType = 'rain' wd.precipIntensity = 10.0 wbs.set_weather(wd) self.assertEqual('heavy-rain', wbs.special().type) self.assertIn(wbs.special().text, wbs.precipitations['rain']['heavy']) wd = models.WeatherData(forecast_us, self.location) wd.precipProbability = 0.9 wd.precipType = 'rain' wd.precipIntensity = 1.0 wbs.set_weather(wd) self.assertEqual('heavy-rain', wbs.special().type) self.assertIn(wbs.special().text, wbs.precipitations['rain']['heavy']) wd = models.WeatherData(forecast_us, self.location) wd.precipProbability = 0.9 wd.precipType = 'none' wd.precipIntensity = 1.0 wbs.set_weather(wd) self.assertEqual('normal', wbs.special().type) self.assertEqual('', wbs.special().text) """Testing if medium-wind type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.icon = 'medium-wind' wbs.set_weather(wd) self.assertEqual('medium-wind', wbs.special().type) """Testing if heavy-wind type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.icon = 'heavy-wind' wbs.set_weather(wd) self.assertEqual('heavy-wind', wbs.special().type) wd = models.WeatherData(forecast_si, self.location) wd.windSpeed = 15.0 wbs.set_weather(wd) self.assertEqual('heavy-wind', wbs.special().type) wd = models.WeatherData(forecast_ca, self.location) wd.windSpeed = 56.0 wbs.set_weather(wd) self.assertEqual('heavy-wind', wbs.special().type) wd = models.WeatherData(forecast_us, self.location) wd.windSpeed = 35.0 wbs.set_weather(wd) self.assertEqual('heavy-wind', wbs.special().type) wd = models.WeatherData(forecast_uk2, self.location) wd.windSpeed = 35.0 wbs.set_weather(wd) self.assertEqual('heavy-wind', wbs.special().type) """Testing if fog type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.icon = 'fog' wbs.set_weather(wd) self.assertEqual('fog', wbs.special().type) """Testing if cold type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.temp = -28.0 wbs.set_weather(wd) self.assertEqual('cold', wbs.special().type) wd = models.WeatherData(forecast_us, self.location) wd.temp = -20.0 wbs.set_weather(wd) self.assertEqual('cold', wbs.special().type) """Testing if super-hot type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.temp = 43.0 wbs.set_weather(wd) self.assertEqual('super-hot', wbs.special().type) wd = models.WeatherData(forecast_us, self.location) wd.temp = 110.0 wbs.set_weather(wd) self.assertEqual('super-hot', wbs.special().type) """Testing if hot type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.temp = 37.0 wbs.set_weather(wd) self.assertEqual('hot', wbs.special().type) wd = models.WeatherData(forecast_us, self.location) wd.temp = 100.0 wbs.set_weather(wd) self.assertEqual('hot', wbs.special().type) """Testing if dry type is triggered""" wd = models.WeatherData(forecast_si, self.location) wd.humidity = 25.0 wbs.set_weather(wd) self.assertEqual('dry', wbs.special().type)