Exemple #1
0
    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_assertions_on_instantation(self):
     with self.assertRaises(AssertionError):
         Measurement(None,
                     self.ts,
                     temperature=dict(min=0, max=100),
                     wind_speed=2.1,
                     wind_gust=67,
                     humidex=77,
                     weather_other=dict(key='val'))
     with self.assertRaises(AssertionError):
         Measurement(None,
                     '12345',
                     temperature=dict(min=0, max=100),
                     wind_speed=2.1,
                     wind_gust=67,
                     humidex=77,
                     weather_other=dict(key='val'))
     with self.assertRaises(AssertionError):
         Measurement(None,
                     -123,
                     temperature=dict(min=0, max=100),
                     wind_speed=2.1,
                     wind_gust=67,
                     humidex=77,
                     weather_other=dict(key='val'))
Exemple #3
0
class MockHttpClientMeasurements(HttpClient):
    msmt1 = Measurement('test_station', 1378459200,
        temperature=dict(min=0, max=100), wind_speed=2.1, wind_gust=67,
        humidex=77, weather_other=dict(key='val1'))
    msmt2 = Measurement('test_station', 1878459999,
        temperature=dict(min=20, max=180), wind_speed=0.7, wind_gust=-7,
        humidex=12, weather_other=dict(key='val2'))
    aggr_msmt1 = AggregatedMeasurement('id1', 1200, 'd', temp=dict(max=9,min=1),
                                       precipitation=dict(min=8.2, max=44.8))
    aggr_msmt2 = AggregatedMeasurement('id1', 1500, 'd', temp=dict(max=8,min=-2))
    aggr_msmt3 = AggregatedMeasurement('id1', 3000, 'd', temp=dict(max=9,min=0))

    def get_json(self, uri, params=None, headers=None):
        items = [self.aggr_msmt1.to_dict(),
                 self.aggr_msmt2.to_dict(),
                 self.aggr_msmt3.to_dict()]
        for i in items:
            i['date'] = i['timestamp']
            i['type'] = i['aggregated_on']
            del i['timestamp']
            del i['aggregated_on']

        # filter on time-windos
        new_items = []
        for i in items:
            if params['from'] <= i['date'] <= params['to']:
                new_items.append(i)

        # check optional limit
        if 'limit' in params:
            return 200, new_items[:params['limit']]
        return 200, new_items

    def post(self, uri, params=None, data=None, headers=None):
        return 200, ''
Exemple #4
0
    def append_from_dict(self, the_dict):
        """
        Creates a ``measurement.Measurement`` object from the supplied dict
        and then appends it to the buffer
        :param the_dict: dict

        """
        m = Measurement.from_dict(the_dict)
        self.append(m)
    def append_from_dict(self, the_dict):
        """
        Creates a ``measurement.Measurement`` object from the supplied dict
        and then appends it to the buffer
        :param the_dict: dict

        """
        m = Measurement.from_dict(the_dict)
        self.append(m)
    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))
Exemple #7
0
 def test_from_dict(self):
     _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'))
     result = Measurement.from_dict(_the_dict)
     self.assertEqual(self._test_instance.station_id, result.station_id)
     self.assertEqual(self._test_instance.timestamp, result.timestamp)
     self.assertEqual(self._test_instance.temperature, result.temperature)
     self.assertEqual(self._test_instance.wind_gust, result.wind_gust)
     self.assertEqual(self._test_instance.wind_speed, result.wind_speed)
     self.assertEqual(self._test_instance.humidex, result.humidex)
     self.assertEqual(self._test_instance.weather_other, result.weather_other)
Exemple #8
0
 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')
class TestJSONPersistenceBackendsReadFS(unittest.TestCase):

    basepath = os.path.dirname(os.path.abspath(__file__))
    data_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'))
    measurement = Measurement.from_dict(data_dict)

    def test_json_persistence_backend_with_no_station_id_specified(self):
        be = JSONPersistenceBackend(
            self.basepath + os.sep + 'measurements.json', None)
        with self.assertRaises(ValueError):
            be.load_to_buffer()

    def test_json_persistence_backend_reads(self):
        be = JSONPersistenceBackend(
            self.basepath + os.sep + 'measurements.json', 'mytest')
        buf = be.load_to_buffer()
        self.assertTrue(3, len(buf))
        for item in buf:
            self.assertTrue(isinstance(item, Measurement))

    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()))
Exemple #10
0
 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()
Exemple #11
0
class TestBuffer(unittest.TestCase):

    ts = 1378459200
    iso_ts = "2013-09-06 09:20:00+00:00"
    date_ts = dt.fromisoformat(iso_ts)

    station_id = 'mytest'

    m1 = Measurement(station_id,
                     ts,
                     temperature=dict(min=0, max=100),
                     wind_speed=2.1,
                     wind_gust=67,
                     humidex=77,
                     weather_other=dict(key='val'))
    m2 = Measurement(station_id,
                     ts + 500,
                     temperature=dict(min=30, max=800),
                     wind_speed=8.5,
                     wind_gust=0,
                     humidex=0)
    m3 = Measurement(station_id,
                     ts - 1000,
                     temperature=dict(min=0, max=20),
                     wind_speed=4.4,
                     wind_gust=-12,
                     humidex=2)

    def test_assertions_on_instantiation(self):
        with self.assertRaises(AssertionError):
            Buffer(None)

    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_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 test_append_from_json(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'))
        json_str = json.dumps(the_dict)
        buf.append_from_json(json_str)
        self.assertEqual(1, len(buf))

    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_sort_chronologically(self):
        ordered_chrono = [self.m3, self.m1, self.m2]
        buf = Buffer(station_id=self.station_id)
        buf.append(self.m1)
        buf.append(self.m2)
        buf.append(self.m3)
        self.assertNotEqual(buf.measurements, ordered_chrono)
        buf.sort_chronologically()
        self.assertEqual(buf.measurements, ordered_chrono)

    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_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_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)

    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_repr(self):
        buf = Buffer(self.station_id)
        buf.append(self.m2)
        str(buf)
Exemple #12
0
class TestMeasurement(unittest.TestCase):

    ts = 1378459200
    iso_ts = "2013-09-06 09:20:00+00"
    date_ts = dt.strptime(iso_ts, '%Y-%m-%d %H:%M:%S+00').replace(tzinfo=UTC())

    _test_instance = Measurement('mytest', ts, temperature=dict(min=0, max=100),
        wind_speed=2.1, wind_gust=67, humidex=77, weather_other=dict(key='val'))

    def test_assertions_on_instantation(self):
        with self.assertRaises(AssertionError):
            Measurement(None, self.ts, temperature=dict(min=0, max=100),
                    wind_speed=2.1, wind_gust=67, humidex=77,
                    weather_other=dict(key='val'))
        with self.assertRaises(AssertionError):
            Measurement(None, '12345', temperature=dict(min=0, max=100),
                    wind_speed=2.1, wind_gust=67, humidex=77,
                    weather_other=dict(key='val'))
        with self.assertRaises(AssertionError):
            Measurement(None, -123, temperature=dict(min=0, max=100),
                    wind_speed=2.1, wind_gust=67, humidex=77,
                    weather_other=dict(key='val'))

    def test_creation_time(self):
        result = self._test_instance.creation_time()
        self.assertEqual(self.ts, result)
        result = self._test_instance.creation_time(timeformat='iso')
        self.assertEqual(self.iso_ts, result)
        result = self._test_instance.creation_time(timeformat='date')
        self.assertEqual(self.date_ts, result)
        with self.assertRaises(ValueError):
            self._test_instance.creation_time(timeformat='unknown')

    def test_from_dict(self):
        _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'))
        result = Measurement.from_dict(_the_dict)
        self.assertEqual(self._test_instance.station_id, result.station_id)
        self.assertEqual(self._test_instance.timestamp, result.timestamp)
        self.assertEqual(self._test_instance.temperature, result.temperature)
        self.assertEqual(self._test_instance.wind_gust, result.wind_gust)
        self.assertEqual(self._test_instance.wind_speed, result.wind_speed)
        self.assertEqual(self._test_instance.humidex, result.humidex)
        self.assertEqual(self._test_instance.weather_other, result.weather_other)

    def test_from_dict_with_missing_values(self):
        with self.assertRaises(KeyError):
            Measurement.from_dict(dict(timestamp=123456789))
        with self.assertRaises(KeyError):
            Measurement.from_dict(dict(station_id='mytest'))

    def test_to_dict(self):
        expected_dict = {
            "station_id": "mytest",
            "timestamp": 1378459200,
            "temperature": {"min":0, "max": 100},
            "wind_speed": 2.1,
            "wind_gust": 67,
            "humidex": 77,
            "weather_other": {"key": "val"}}
        result_dict = self._test_instance.to_dict()
        self.assertTrue(all(item in result_dict.items()
                            for item in expected_dict.items()))

    def test_to_JSON(self):
        expected = '''
        {"station_id": "mytest",
        "timestamp": 1378459200,
        "temperature":{"min":0, "max": 100},
        "wind_speed":2.1,
        "wind_gust":67,
        "humidex":77,
        "weather_other":{"key":"val"}}
        '''
        result_dict = json.loads(self._test_instance.to_JSON())
        expected_dict = json.loads(expected)
        self.assertTrue(all(item in result_dict.items()
                            for item in expected_dict.items()))

    def test_repr(self):
        str(self._test_instance)
Exemple #13
0
 def test_from_dict_with_missing_values(self):
     with self.assertRaises(KeyError):
         Measurement.from_dict(dict(timestamp=123456789))
     with self.assertRaises(KeyError):
         Measurement.from_dict(dict(station_id='mytest'))