Skip to content

Commit

Permalink
Porting back fix to issue #272 to the Py2 LTS branch
Browse files Browse the repository at this point in the history
  • Loading branch information
csparpa committed Nov 6, 2018
1 parent c08f84f commit 1a29c19
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 57 deletions.
73 changes: 37 additions & 36 deletions pyowm/stationsapi30/stations_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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},
Expand Down Expand Up @@ -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
65 changes: 44 additions & 21 deletions tests/integration/stationsapi30/test_integration_stationsapi30.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand All @@ -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()
35 changes: 35 additions & 0 deletions tests/unit/stationsapi30/test_stations_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 1a29c19

Please sign in to comment.