def test_iteration(self): buf = Buffer(station_id=self.station_id) buf.append(self.m1) buf.append(self.m2) buf.append(self.m3) for item in buf: self.assertTrue(isinstance(item, Measurement)) self.assertTrue(item in [self.m1, self.m2, self.m3])
def test_append_from_dict(self): buf = Buffer(self.station_id) self.assertEqual(0, len(buf)) the_dict = dict(station_id='mytest', timestamp=1378459200, temperature=dict(min=0, max=100), wind_speed=2.1, wind_gust=67, humidex=77, weather_other=dict(key='val')) buf.append_from_dict(the_dict) self.assertEqual(1, len(buf))
def load_to_buffer(self): if self._station_id is None: raise ValueError('No station ID specified') result = Buffer(self._station_id) with open(self._file_path, 'r') as f: list_of_dicts = json.load(f) for _dict in list_of_dicts: result.append_from_dict(_dict) return result
def test_persist_buffer(self): with patch('os.path.isfile', return_value=True): mocked_file_data = '[{"station_id": "test_id", "timestamp":142332322}]' with patch('builtins.open', unittest.mock.mock_open( read_data=mocked_file_data)) as mocked_open: test_instance = JSONPersistenceBackend(self.file, self.test_id) test_buffer = Buffer(self.test_id) test_measurement = Measurement(self.test_id, self.test_timestamp) test_buffer.measurements = [test_measurement] test_instance.persist_buffer(test_buffer) mocked_open.assert_called_once_with(self.file, 'w')
def test_add(self): buf1 = Buffer(station_id=self.station_id) buf1.append(self.m1) buf2 = Buffer(station_id=self.station_id) buf2.append(self.m2) buf2.append(self.m3) result = buf1 + buf2 self.assertEqual(3, len(result))
def test_creation_time(self): buf = Buffer(self.station_id) ts = buf.creation_time() iso_result = buf.creation_time(timeformat='iso') self.assertEqual(to_ISO8601(ts), iso_result) date_result = buf.creation_time(timeformat='date') self.assertEqual(to_date(ts), date_result) with self.assertRaises(ValueError): buf.creation_time(timeformat='unknown') buf.created_at = None self.assertIsNone(buf.creation_time())
def test_append(self): buf = Buffer(self.station_id) self.assertEqual(0, len(buf)) buf.append(self.m1) self.assertEqual(1, len(buf)) self.assertTrue(self.m1 in buf) buf = Buffer(self.station_id) with self.assertRaises(AssertionError): buf.append('not_a_measurement') msmt = deepcopy(self.m1) msmt.station_id = 'another_id' with self.assertRaises(AssertionError): buf.append(msmt)
def test_sort_reverse_chronologically(self): ordered_reverse_chrono = [self.m2, self.m1, self.m3] buf = Buffer(station_id=self.station_id) buf.append(self.m1) buf.append(self.m2) buf.append(self.m3) self.assertNotEqual(buf.measurements, ordered_reverse_chrono) buf.sort_reverse_chronologically() self.assertEqual(buf.measurements, ordered_reverse_chrono)
def test_json_persistence_backend_writes(self): with tempfile.NamedTemporaryFile() as tmp: # write file target_file = os.path.abspath(tmp.name) be = JSONPersistenceBackend(target_file, 'mytest') buffer = Buffer('mytest') buffer.append(self.measurement) be.persist_buffer(buffer) # check data with open(target_file, 'r') as f: data = f.read() items = json.loads(data) self.assertEqual(1, len(items)) msmt = items[0] self.assertTrue(all(item in msmt.items() for item in self.data_dict.items()))
def test_empty(self): buf = Buffer(self.station_id) self.assertEqual(0, len(buf)) buf.append(self.m1) buf.append(self.m2) buf.append(self.m3) self.assertEqual(3, len(buf)) self.assertTrue(self.m1 in buf) self.assertTrue(self.m2 in buf) self.assertTrue(self.m3 in buf) buf.empty() self.assertEqual(0, len(buf))
def test_measurements_and_buffers(self): mgr = self.__owm.stations_manager() # 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))
def test_measurements_and_buffers(self): mgr = self.__owm.stations_manager() # 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))
def test_add(self): buf1 = Buffer(station_id=self.station_id) buf1.append(self.m1) buf2 = Buffer(station_id=self.station_id) buf2.append(self.m2) buf2.append(self.m3) result = buf1 + buf2 self.assertEquals(3, len(result))
def test_creation_time(self): buf = Buffer(self.station_id) ts = buf.creation_time() iso_result = buf.creation_time(timeformat='iso') self.assertEqual(to_ISO8601(ts), iso_result) date_result = buf.creation_time(timeformat='date') self.assertEqual(to_date(ts), date_result) with self.assertRaises(ValueError): buf.creation_time(timeformat='unknown')
def test_send_buffer(self): instance = self.factory(MockHttpClientMeasurements) buffer = Buffer(MockHttpClientMeasurements.msmt1.station_id) buffer.append(MockHttpClientMeasurements.msmt1) instance.send_buffer(buffer)
def test_repr(self): buf = Buffer(self.station_id) buf.append(self.m2) str(buf)
def test_assertions_on_instantiation(self): with self.assertRaises(AssertionError): Buffer(None)
def test_stations_CRUD(self): mgr = self.__owm.stations_manager() # check if any previous station exists on this account n_old_stations = len(mgr.get_stations()) # create stat1 stat1 = mgr.create_station('PYOWM1', 'pyowm_test_station_1', 45.0, 9.0, 189.0) # create stat2 stat2 = mgr.create_station('PYOWM2', 'pyowm_test_station_2', 46.0, 18.0, 50.0) # Read all stations = mgr.get_stations() self.assertEqual(n_old_stations + 2, len(stations)) # Read one by one result = mgr.get_station(stat1.id) self.assertEqual(stat1.id, result.id) self.assertEqual(stat1.external_id, result.external_id) self.assertEquals(stat1.name, result.name) self.assertEquals(stat1.lat, result.lat) self.assertEquals(stat1.lon, result.lon) self.assertEquals(stat1.alt, result.alt) result = mgr.get_station(stat2.id) self.assertEquals(stat2.id, result.id) self.assertEquals(stat2.external_id, result.external_id) self.assertEquals(stat2.name, result.name) self.assertEquals(stat2.lat, result.lat) self.assertEquals(stat2.lon, result.lon) self.assertEquals(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' modified_stat2.lat = 30.6 mgr.update_station(modified_stat2) result = mgr.get_station(modified_stat2.id) self.assertEquals(modified_stat2.id, result.id) self.assertEquals(modified_stat2.external_id, result.external_id) self.assertEquals(modified_stat2.name, result.name) # of course, lat had been modified self.assertEquals(modified_stat2.lon, result.lon) self.assertEquals(modified_stat2.alt, result.alt) # Delete stations one by one mgr.delete_station(stat1) stations = mgr.get_stations() self.assertEqual(n_old_stations + 1, len(stations)) mgr.delete_station(modified_stat2) stations = mgr.get_stations() self.assertEqual(n_old_stations, len(stations))
def test_contains(self): buf = Buffer(station_id=self.station_id) buf.append(self.m1) self.assertFalse(self.m3 in buf) self.assertTrue(self.m1 in buf) self.assertFalse(self.m2 in buf)