def test_from_dictionary(self):
        the_dict = {
            'id': '583436dd9643a9000196b8d6',
            'altitude': 150,
            'created_at': '2016-11-22T12:15:25.967Z',
            'external_id': 'SF_TEST001',
            'latitude': 37.76,
            'longitude': -122.43,
            'name': 'San Francisco Test Station',
            'rank': 0,
            'updated_at': '2016-11-22T12:15:25.967Z'
        }

        result = Station.from_dict(the_dict)
        self.assertTrue(isinstance(result, Station))
        self.assertEqual(self._test_instance.id, result.id)
        self.assertEqual(self._test_instance.created_at, result.created_at)
        self.assertEqual(self._test_instance.updated_at, result.updated_at)
        self.assertEqual(self._test_instance.name, result.name)
        self.assertEqual(self._test_instance.lon, result.lon)
        self.assertEqual(self._test_instance.lat, result.lat)
        self.assertEqual(self._test_instance.alt, result.alt)
        self.assertEqual(self._test_instance.rank, result.rank)

        with self.assertRaises(pyowm.commons.exceptions.ParseAPIResponseError):
            Station.from_dict(None)
예제 #2
0
 def test_delete_station_fails_when_id_is_none(self):
     instance = self.factory(MockHttpClient)
     station = Station.from_dict(
         json.loads(MockHttpClient.test_station_json))
     station.id = None
     with self.assertRaises(AssertionError):
         instance.delete_station(station)
예제 #3
0
 def test_update_station(self):
     instance = self.factory(MockHttpClient)
     modified_station = Station.from_dict(
         json.loads(MockHttpClient.test_station_json))
     modified_station.external_id = 'CHNG'
     result = instance.update_station(modified_station)
     self.assertIsNone(result)
예제 #4
0
    def get_stations(self):
        """
        Retrieves all of the user's stations registered on the Stations API.

        :returns: list of *pyowm.stationsapi30.station.Station* objects

        """

        status, data = self.http_client.get_json(
            STATIONS_URI,
            params={'appid': self.API_key},
            headers={'Content-Type': 'application/json'})
        return [Station.from_dict(item) for item in data]
예제 #5
0
    def get_station(self, id):
        """
        Retrieves a named station registered on the Stations API.

        :param id: the ID of the station
        :type id: str
        :returns: a *pyowm.stationsapi30.station.Station* object

        """
        status, data = self.http_client.get_json(
            NAMED_STATION_URI % str(id),
            params={'appid': self.API_key},
            headers={'Content-Type': 'application/json'})
        return Station.from_dict(data)
예제 #6
0
    def create_station(self, external_id, name, lat, lon, alt=None):
        """
        Create a new station on the Station API with the given parameters

        :param external_id: the user-given ID of the station
        :type external_id: str
        :param name: the name of the station
        :type name: str
        :param lat: latitude of the station
        :type lat: float
        :param lon: longitude of the station
        :type lon: float
        :param alt: altitude of the station
        :type alt: float
        :returns: the new *pyowm.stationsapi30.station.Station* object
        """
        assert external_id is not None
        assert name is not None
        assert lon is not None
        assert lat is not None
        if lon < -180.0 or lon > 180.0:
            raise ValueError("'lon' value must be between -180 and 180")
        if lat < -90.0 or lat > 90.0:
            raise ValueError("'lat' value must be between -90 and 90")
        if alt is not None:
            if alt < 0.0:
                raise ValueError("'alt' value must not be negative")
        status, payload = self.http_client.post(
            STATIONS_URI,
            params={'appid': self.API_key},
            data=dict(external_id=external_id,
                      name=name,
                      lat=lat,
                      lon=lon,
                      alt=alt),
            headers={'Content-Type': 'application/json'})
        return Station.from_dict(payload)
예제 #7
0
 def test_delete_station(self):
     instance = self.factory(MockHttpClient)
     station = Station.from_dict(
         json.loads(MockHttpClient.test_station_json))
     result = instance.delete_station(station)
     self.assertIsNone(result)