From 1a29c198d797456e2fbb780aaf89582448490c74 Mon Sep 17 00:00:00 2001 From: csparpa Date: Tue, 6 Nov 2018 23:14:48 +0100 Subject: [PATCH] Porting back fix to issue #272 to the Py2 LTS branch --- pyowm/stationsapi30/stations_manager.py | 73 ++++++++++--------- .../test_integration_stationsapi30.py | 65 +++++++++++------ .../stationsapi30/test_stations_manager.py | 35 +++++++++ 3 files changed, 116 insertions(+), 57 deletions(-) diff --git a/pyowm/stationsapi30/stations_manager.py b/pyowm/stationsapi30/stations_manager.py index bb0e0f69..402e54a7 100644 --- a/pyowm/stationsapi30/stations_manager.py +++ b/pyowm/stationsapi30/stations_manager.py @@ -149,7 +149,7 @@ def send_measurement(self, measurement): status, _ = self.http_client.post( MEASUREMENTS_URI, params={'appid': self.API_key}, - data=[measurement.to_dict()], + data=[self._structure_dict(measurement)], headers={'Content-Type': 'application/json'}) def send_measurements(self, list_of_measurements): @@ -165,7 +165,7 @@ def send_measurements(self, list_of_measurements): """ assert list_of_measurements is not None assert all([m.station_id is not None for m in list_of_measurements]) - msmts = [m.to_dict() for m in list_of_measurements] + msmts = [self._structure_dict(m) for m in list_of_measurements] status, _ = self.http_client.post( MEASUREMENTS_URI, params={'appid': self.API_key}, @@ -227,42 +227,43 @@ def send_buffer(self, buffer): :returns: `None` if creation is successful, an exception otherwise """ assert buffer is not None - msmts = [] - for x in buffer.measurements: - m = x.to_dict() - item = dict() - item['station_id'] = m['station_id'] - item['dt'] = m['timestamp'] - item['temperature'] = m['temperature'] - item['wind_speed'] = m['wind_speed'] - item['wind_gust'] = m['wind_gust'] - item['wind_deg'] = m['wind_deg'] - item['pressure'] = m['pressure'] - item['humidity'] = m['humidity'] - item['rain_1h'] = m['rain_1h'] - item['rain_6h'] = m['rain_6h'] - item['rain_24h'] = m['rain_24h'] - item['snow_1h'] = m['snow_1h'] - item['snow_6h'] = m['snow_6h'] - item['snow_24h'] = m['snow_24h'] - item['dew_point'] = m['dew_point'] - item['humidex'] = m['humidex'] - item['heat_index'] = m['heat_index'] - item['visibility_distance'] = m['visibility_distance'] - item['visibility_prefix'] = m['visibility_prefix'] - item['clouds'] = [dict(distance=m['clouds_distance']), - dict(condition=m['clouds_condition']), - dict(cumulus=m['clouds_cumulus'])] - item['weather'] = [ - dict(precipitation=m['weather_precipitation']), - dict(descriptor=m['weather_descriptor']), - dict(intensity=m['weather_intensity']), - dict(proximity=m['weather_proximity']), - dict(obscuration=m['weather_obscuration']), - dict(other=m['weather_other'])] - msmts.append(item) + msmts = [self._structure_dict(m) for m in buffer.measurements] status, _ = self.http_client.post( MEASUREMENTS_URI, params={'appid': self.API_key}, data=msmts, headers={'Content-Type': 'application/json'}) + + def _structure_dict(self, measurement): + d = measurement.to_dict() + item = dict() + item['station_id'] = d['station_id'] + item['dt'] = d['timestamp'] + item['temperature'] = d['temperature'] + item['wind_speed'] = d['wind_speed'] + item['wind_gust'] = d['wind_gust'] + item['wind_deg'] = d['wind_deg'] + item['pressure'] = d['pressure'] + item['humidity'] = d['humidity'] + item['rain_1h'] = d['rain_1h'] + item['rain_6h'] = d['rain_6h'] + item['rain_24h'] = d['rain_24h'] + item['snow_1h'] = d['snow_1h'] + item['snow_6h'] = d['snow_6h'] + item['snow_24h'] = d['snow_24h'] + item['dew_point'] = d['dew_point'] + item['humidex'] = d['humidex'] + item['heat_index'] = d['heat_index'] + item['visibility_distance'] = d['visibility_distance'] + item['visibility_prefix'] = d['visibility_prefix'] + item['clouds'] = [dict(distance=d['clouds_distance']), + dict(condition=d['clouds_condition']), + dict(cumulus=d['clouds_cumulus'])] + item['weather'] = [ + dict(precipitation=d['weather_precipitation']), + dict(descriptor=d['weather_descriptor']), + dict(intensity=d['weather_intensity']), + dict(proximity=d['weather_proximity']), + dict(obscuration=d['weather_obscuration']), + dict(other=d['weather_other'])] + return item \ No newline at end of file diff --git a/tests/integration/stationsapi30/test_integration_stationsapi30.py b/tests/integration/stationsapi30/test_integration_stationsapi30.py index 4adf85a7..85eb9a55 100644 --- a/tests/integration/stationsapi30/test_integration_stationsapi30.py +++ b/tests/integration/stationsapi30/test_integration_stationsapi30.py @@ -5,6 +5,7 @@ from pyowm.webapi25.configuration25 import parsers from pyowm.webapi25.owm25 import OWM25 from pyowm.stationsapi30.buffer import Buffer +from pyowm.stationsapi30.measurement import Measurement class IntegrationTestsStationsAPI30(unittest.TestCase): @@ -47,25 +48,6 @@ def test_stations_CRUD(self): self.assertEqual(stat2.lon, result.lon) self.assertEqual(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' @@ -88,7 +70,48 @@ def test_stations_CRUD(self): stations = mgr.get_stations() self.assertEqual(n_old_stations, len(stations)) + def test_measurements_and_buffers(self): + mgr = self.__owm.stations_manager() -if __name__ == "__main__": - unittest.main() + # check if any previous station exists on this account + n_old_stations = len(mgr.get_stations()) + + # create station + test_station = mgr.create_station('PYOWM_TEST_BUFFERS', 'pyowm_test_buffers', 45.0, 9.0, 189.0) + + # create and bufferize some measurements for the test station + buf = Buffer(test_station.id) + buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505231630, + temperature=100, wind_speed=2.1, + wind_gust=67, humidex=77)) + buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505429694, + temperature=100, wind_speed=2.1, + wind_gust=67, humidex=77)) + mgr.send_buffer(buf) + + # now directly send measurements + measurement = Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505415230, + temperature=100, wind_speed=2.1, + wind_gust=67, humidex=77)) + measurements_list = [ + Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505315230, + temperature=100, wind_speed=2.1, + wind_gust=67, humidex=77)) + ] + mgr.send_measurement(measurement) + mgr.send_measurements(measurements_list) + # read the measurements for station + msmts = mgr.get_measurements(test_station.id, 'd', 1505200000, 1505430000) + for m in msmts: + self.assertEqual(test_station.id, m.station_id) + self.assertEqual('d', m.aggregated_on) + + # Delete stations one by one + mgr.delete_station(test_station) + stations = mgr.get_stations() + self.assertEqual(n_old_stations, len(stations)) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/tests/unit/stationsapi30/test_stations_manager.py b/tests/unit/stationsapi30/test_stations_manager.py index 86cf5544..83cf5917 100644 --- a/tests/unit/stationsapi30/test_stations_manager.py +++ b/tests/unit/stationsapi30/test_stations_manager.py @@ -268,3 +268,38 @@ def test_send_buffer_failing(self): with self.assertRaises(AssertionError): instance.send_buffer(None) + + def test__structure_dict(self): + temp = dict(min=0, max=100) + msmt = Measurement('test_station', 1378459200, + temperature=temp, wind_speed=2.1, wind_gust=67, + humidex=77, weather_other=dict(key='val')) + expected = { + 'station_id': 'test_station', + 'dt': 1378459200, + 'temperature': temp, + 'wind_speed': 2.1, + 'wind_gust': 67, + 'humidex': 77, + 'weather': [ + { + 'other': { + 'key': 'val' + } + } + ] + } + instance = StationsManager('API-Key') + result = instance._structure_dict(msmt) + self.assertEqual(expected['station_id'], result['station_id']) + self.assertEqual(expected['dt'], result['dt']) + self.assertEqual(expected['wind_speed'], result['wind_speed']) + self.assertEqual(expected['wind_gust'], result['wind_gust']) + self.assertEqual(expected['humidex'], result['humidex']) + self.assertEqual(expected['temperature'], result['temperature']) + for item in result['weather']: + content = item.get('other') + if content: + self.assertEqual(expected['weather'][0]['other'], content) + return + self.fail()