Skip to content

Commit 1a29c19

Browse files
committed
Porting back fix to issue #272 to the Py2 LTS branch
1 parent c08f84f commit 1a29c19

File tree

3 files changed

+116
-57
lines changed

3 files changed

+116
-57
lines changed

pyowm/stationsapi30/stations_manager.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def send_measurement(self, measurement):
149149
status, _ = self.http_client.post(
150150
MEASUREMENTS_URI,
151151
params={'appid': self.API_key},
152-
data=[measurement.to_dict()],
152+
data=[self._structure_dict(measurement)],
153153
headers={'Content-Type': 'application/json'})
154154

155155
def send_measurements(self, list_of_measurements):
@@ -165,7 +165,7 @@ def send_measurements(self, list_of_measurements):
165165
"""
166166
assert list_of_measurements is not None
167167
assert all([m.station_id is not None for m in list_of_measurements])
168-
msmts = [m.to_dict() for m in list_of_measurements]
168+
msmts = [self._structure_dict(m) for m in list_of_measurements]
169169
status, _ = self.http_client.post(
170170
MEASUREMENTS_URI,
171171
params={'appid': self.API_key},
@@ -227,42 +227,43 @@ def send_buffer(self, buffer):
227227
:returns: `None` if creation is successful, an exception otherwise
228228
"""
229229
assert buffer is not None
230-
msmts = []
231-
for x in buffer.measurements:
232-
m = x.to_dict()
233-
item = dict()
234-
item['station_id'] = m['station_id']
235-
item['dt'] = m['timestamp']
236-
item['temperature'] = m['temperature']
237-
item['wind_speed'] = m['wind_speed']
238-
item['wind_gust'] = m['wind_gust']
239-
item['wind_deg'] = m['wind_deg']
240-
item['pressure'] = m['pressure']
241-
item['humidity'] = m['humidity']
242-
item['rain_1h'] = m['rain_1h']
243-
item['rain_6h'] = m['rain_6h']
244-
item['rain_24h'] = m['rain_24h']
245-
item['snow_1h'] = m['snow_1h']
246-
item['snow_6h'] = m['snow_6h']
247-
item['snow_24h'] = m['snow_24h']
248-
item['dew_point'] = m['dew_point']
249-
item['humidex'] = m['humidex']
250-
item['heat_index'] = m['heat_index']
251-
item['visibility_distance'] = m['visibility_distance']
252-
item['visibility_prefix'] = m['visibility_prefix']
253-
item['clouds'] = [dict(distance=m['clouds_distance']),
254-
dict(condition=m['clouds_condition']),
255-
dict(cumulus=m['clouds_cumulus'])]
256-
item['weather'] = [
257-
dict(precipitation=m['weather_precipitation']),
258-
dict(descriptor=m['weather_descriptor']),
259-
dict(intensity=m['weather_intensity']),
260-
dict(proximity=m['weather_proximity']),
261-
dict(obscuration=m['weather_obscuration']),
262-
dict(other=m['weather_other'])]
263-
msmts.append(item)
230+
msmts = [self._structure_dict(m) for m in buffer.measurements]
264231
status, _ = self.http_client.post(
265232
MEASUREMENTS_URI,
266233
params={'appid': self.API_key},
267234
data=msmts,
268235
headers={'Content-Type': 'application/json'})
236+
237+
def _structure_dict(self, measurement):
238+
d = measurement.to_dict()
239+
item = dict()
240+
item['station_id'] = d['station_id']
241+
item['dt'] = d['timestamp']
242+
item['temperature'] = d['temperature']
243+
item['wind_speed'] = d['wind_speed']
244+
item['wind_gust'] = d['wind_gust']
245+
item['wind_deg'] = d['wind_deg']
246+
item['pressure'] = d['pressure']
247+
item['humidity'] = d['humidity']
248+
item['rain_1h'] = d['rain_1h']
249+
item['rain_6h'] = d['rain_6h']
250+
item['rain_24h'] = d['rain_24h']
251+
item['snow_1h'] = d['snow_1h']
252+
item['snow_6h'] = d['snow_6h']
253+
item['snow_24h'] = d['snow_24h']
254+
item['dew_point'] = d['dew_point']
255+
item['humidex'] = d['humidex']
256+
item['heat_index'] = d['heat_index']
257+
item['visibility_distance'] = d['visibility_distance']
258+
item['visibility_prefix'] = d['visibility_prefix']
259+
item['clouds'] = [dict(distance=d['clouds_distance']),
260+
dict(condition=d['clouds_condition']),
261+
dict(cumulus=d['clouds_cumulus'])]
262+
item['weather'] = [
263+
dict(precipitation=d['weather_precipitation']),
264+
dict(descriptor=d['weather_descriptor']),
265+
dict(intensity=d['weather_intensity']),
266+
dict(proximity=d['weather_proximity']),
267+
dict(obscuration=d['weather_obscuration']),
268+
dict(other=d['weather_other'])]
269+
return item

tests/integration/stationsapi30/test_integration_stationsapi30.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pyowm.webapi25.configuration25 import parsers
66
from pyowm.webapi25.owm25 import OWM25
77
from pyowm.stationsapi30.buffer import Buffer
8+
from pyowm.stationsapi30.measurement import Measurement
89

910

1011
class IntegrationTestsStationsAPI30(unittest.TestCase):
@@ -47,25 +48,6 @@ def test_stations_CRUD(self):
4748
self.assertEqual(stat2.lon, result.lon)
4849
self.assertEqual(stat2.alt, result.alt)
4950

50-
# create and bufferize some measurements for station n.1
51-
buf = Buffer(stat1.id)
52-
buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505231630,
53-
temperature=100, wind_speed=2.1,
54-
wind_gust=67, humidex=77))
55-
buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505415230,
56-
temperature=100, wind_speed=2.1,
57-
wind_gust=67, humidex=77))
58-
buf.append_from_dict(dict(station_id=stat1.id, timestamp=1505429694,
59-
temperature=100, wind_speed=2.1,
60-
wind_gust=67, humidex=77))
61-
mgr.send_buffer(buf)
62-
buf.empty()
63-
64-
# read the measurements for station 1
65-
msmts = mgr.get_measurements(stat1.id, 'd', 1505200000, 1505430000)
66-
for m in msmts:
67-
self.assertEquals(m.station_id, stat1.id)
68-
6951
# Update a station
7052
modified_stat2 = copy.deepcopy(stat2)
7153
modified_stat2.eternal = 'modified_pyowm_test_station_2'
@@ -88,7 +70,48 @@ def test_stations_CRUD(self):
8870
stations = mgr.get_stations()
8971
self.assertEqual(n_old_stations, len(stations))
9072

73+
def test_measurements_and_buffers(self):
74+
mgr = self.__owm.stations_manager()
9175

92-
if __name__ == "__main__":
93-
unittest.main()
76+
# check if any previous station exists on this account
77+
n_old_stations = len(mgr.get_stations())
78+
79+
# create station
80+
test_station = mgr.create_station('PYOWM_TEST_BUFFERS', 'pyowm_test_buffers', 45.0, 9.0, 189.0)
81+
82+
# create and bufferize some measurements for the test station
83+
buf = Buffer(test_station.id)
84+
buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505231630,
85+
temperature=100, wind_speed=2.1,
86+
wind_gust=67, humidex=77))
87+
buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505429694,
88+
temperature=100, wind_speed=2.1,
89+
wind_gust=67, humidex=77))
90+
mgr.send_buffer(buf)
91+
92+
# now directly send measurements
93+
measurement = Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505415230,
94+
temperature=100, wind_speed=2.1,
95+
wind_gust=67, humidex=77))
96+
measurements_list = [
97+
Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505315230,
98+
temperature=100, wind_speed=2.1,
99+
wind_gust=67, humidex=77))
100+
]
101+
mgr.send_measurement(measurement)
102+
mgr.send_measurements(measurements_list)
94103

104+
# read the measurements for station
105+
msmts = mgr.get_measurements(test_station.id, 'd', 1505200000, 1505430000)
106+
for m in msmts:
107+
self.assertEqual(test_station.id, m.station_id)
108+
self.assertEqual('d', m.aggregated_on)
109+
110+
# Delete stations one by one
111+
mgr.delete_station(test_station)
112+
stations = mgr.get_stations()
113+
self.assertEqual(n_old_stations, len(stations))
114+
115+
116+
if __name__ == "__main__":
117+
unittest.main()

tests/unit/stationsapi30/test_stations_manager.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,38 @@ def test_send_buffer_failing(self):
268268

269269
with self.assertRaises(AssertionError):
270270
instance.send_buffer(None)
271+
272+
def test__structure_dict(self):
273+
temp = dict(min=0, max=100)
274+
msmt = Measurement('test_station', 1378459200,
275+
temperature=temp, wind_speed=2.1, wind_gust=67,
276+
humidex=77, weather_other=dict(key='val'))
277+
expected = {
278+
'station_id': 'test_station',
279+
'dt': 1378459200,
280+
'temperature': temp,
281+
'wind_speed': 2.1,
282+
'wind_gust': 67,
283+
'humidex': 77,
284+
'weather': [
285+
{
286+
'other': {
287+
'key': 'val'
288+
}
289+
}
290+
]
291+
}
292+
instance = StationsManager('API-Key')
293+
result = instance._structure_dict(msmt)
294+
self.assertEqual(expected['station_id'], result['station_id'])
295+
self.assertEqual(expected['dt'], result['dt'])
296+
self.assertEqual(expected['wind_speed'], result['wind_speed'])
297+
self.assertEqual(expected['wind_gust'], result['wind_gust'])
298+
self.assertEqual(expected['humidex'], result['humidex'])
299+
self.assertEqual(expected['temperature'], result['temperature'])
300+
for item in result['weather']:
301+
content = item.get('other')
302+
if content:
303+
self.assertEqual(expected['weather'][0]['other'], content)
304+
return
305+
self.fail()

0 commit comments

Comments
 (0)