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)
def test_instantiations_failing_upon_wrong_geocoords(self): with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -422.43, 37.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, -97.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, -56.9, 0)
def parse_JSON(self, JSON_string): """ Parses a *pyowm.stationsapi30.station.Station* instance out of raw JSON data. :param JSON_string: a raw JSON string :type JSON_string: str :return: a *pyowm.stationsapi30.station.Station** instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result """ if JSON_string is None: raise parse_response_error.ParseResponseError('JSON data is None') d = json.loads(JSON_string) try: id = d.get('ID', None) or d.get('id', None) external_id = d.get('external_id', None) lon = d.get('longitude', None) lat = d.get('latitude', None) alt = d.get('altitude', None) except KeyError as e: raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e) name = d.get('name', None) rank = d.get('rank', None) created_at = d.get('created_at', None) updated_at = d.get('updated_at', None) return Station(id, created_at, updated_at, external_id, name, lon, lat, alt, rank)
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)
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)
def test_failing_instantiations(self): with self.assertRaises(AssertionError): Station(None, "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", None, "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", None, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, None, 150, 0)
def test_to_JSON(self): expected = '''{ "alt": 150, "name": "San Francisco Test Station", "lat": 37.76, "lon": -122.43, "created_at": "2016-11-22 12:15:25+00", "external_id": "SF_TEST001", "id": "583436dd9643a9000196b8d6", "rank": 0, "updated_at": "2016-11-22 12:15:25+00"}''' instance = Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) result = instance.to_JSON() self.assertEquals(json.loads(expected), json.loads(result))
class TestStationsParser(unittest.TestCase): test_station_json = '''{"ID": "583436dd9643a9000196b8d6", "created_at": "2016-11-22T12:15:25.967Z", "updated_at": "2016-11-22T12:15:25.967Z", "external_id": "SF_TEST001", "name": "San Francisco Test Station", "longitude": -122.43, "latitude": 37.76, "altitude": 150, "rank": 0}''' test_station = Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) def test_parse_JSON(self): instance = StationParser() result = instance.parse_JSON(self.test_station_json) self.assertTrue(isinstance(result, Station)) self.assertEqual(self.test_station.id, result.id) self.assertEqual(self.test_station.created_at, result.created_at) self.assertEqual(self.test_station.updated_at, result.updated_at) self.assertEqual(self.test_station.name, result.name) self.assertEqual(self.test_station.lon, result.lon) self.assertEqual(self.test_station.lat, result.lat) self.assertEqual(self.test_station.alt, result.alt) self.assertEqual(self.test_station.rank, result.rank) def test_parse_JSON_fails_with_none_input(self): instance = StationParser() with self.assertRaises(parse_response_error.ParseResponseError): instance.parse_JSON(None) def test_parse_dict(self): data_dict = json.loads(self.test_station_json) instance = StationParser() result = instance.parse_dict(data_dict) self.assertTrue(isinstance(result, Station)) self.assertEqual(self.test_station.id, result.id) self.assertEqual(self.test_station.created_at, result.created_at) self.assertEqual(self.test_station.updated_at, result.updated_at) self.assertEqual(self.test_station.name, result.name) self.assertEqual(self.test_station.lon, result.lon) self.assertEqual(self.test_station.lat, result.lat) self.assertEqual(self.test_station.alt, result.alt) self.assertEqual(self.test_station.rank, result.rank) def test_parse_dict_fails_with_wrong_input(self): instance = StationParser() with self.assertRaises(AssertionError): instance.parse_dict(1234)
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]
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)
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)
class TestStation(unittest.TestCase): _test_instance = Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) _test_instance_none_values = Station("583436dd9643a9000196b8d6", None, None, "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, None, 0) def test_format_micros(self): # micros are present and must be padded input_string = "2016-11-22T12:15:25.967Z" expected = "2016-11-22T12:15:25.000967Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # micros are present but too many digits input_string = "2017-08-25T18:16:16.487887736Z" expected = "2017-08-25T18:16:16.487887Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # no micros at all input_string = "2016-11-22T12:15:25Z" expected = "2016-11-22T12:15:25.000000Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # no micros at all n.2 input_string = "2016-11-22T12:15:25" expected = "2016-11-22T12:15:25.000000Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) def test_failing_instantiations(self): with self.assertRaises(AssertionError): Station(None, "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", None, "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", None, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, None, 150, 0) def test_instantiations_failing_upon_wrong_geocoords(self): with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -422.43, 37.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, -97.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, -56.9, 0) def test_init_with_none_values(self): self.assertIsNone(self._test_instance_none_values.alt) self.assertIsNone(self._test_instance_none_values.created_at) self.assertIsNone(self._test_instance_none_values.updated_at) def test_repr(self): print(self._test_instance) 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) def test_to_dict(self): expected = { 'id': '583436dd9643a9000196b8d6', 'altitude': 150, 'created_at': '2016-11-22 12:15:25+00', 'external_id': 'SF_TEST001', 'latitude': 37.76, 'longitude': -122.43, 'name': 'San Francisco Test Station', 'rank': 0, 'updated_at': '2016-11-22 12:15:25+00' } result = self._test_instance.to_dict() self.assertEqual(expected, result) def test_creation_time(self): default_creation_time = self._test_instance.creation_time() self.assertIsInstance(default_creation_time, int) unix_creation_time = self._test_instance.creation_time('unix') self.assertIsInstance(unix_creation_time, int) iso_creation_time = self._test_instance.creation_time('iso') self.assertIsInstance(iso_creation_time, str) date_creation_time = self._test_instance.creation_time('date') self.assertIsInstance(date_creation_time, datetime.datetime) creation_time_is_none = self._test_instance_none_values.creation_time() self.assertIsNone(creation_time_is_none) def test_last_update_time(self): default_last_updated_time = self._test_instance.last_update_time() self.assertIsInstance(default_last_updated_time, int) unix_last_updated_time = self._test_instance.last_update_time('unix') self.assertIsInstance(unix_last_updated_time, int) iso_last_updated_time = self._test_instance.last_update_time('iso') self.assertIsInstance(iso_last_updated_time, str) date_last_updated_time = self._test_instance.last_update_time('date') self.assertIsInstance(date_last_updated_time, datetime.datetime) updated_at_is_none = self._test_instance_none_values.last_update_time() self.assertIsNone(updated_at_is_none)
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)
class TestStation(unittest.TestCase): _test_instance = Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) def test_format_micros(self): # micros are present and must be padded input_string = "2016-11-22T12:15:25.967Z" expected = "2016-11-22T12:15:25.000967Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # micros are present but too many digits input_string = "2017-08-25T18:16:16.487887736Z" expected = "2017-08-25T18:16:16.487887Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # no micros at all input_string = "2016-11-22T12:15:25Z" expected = "2016-11-22T12:15:25.000000Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) # no micros at all n.2 input_string = "2016-11-22T12:15:25" expected = "2016-11-22T12:15:25.000000Z" result = self._test_instance._format_micros(input_string) self.assertEquals(expected, result) def test_failing_instantiations(self): with self.assertRaises(AssertionError): Station(None, "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", None, "San Francisco Test Station", -122.43, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", None, 37.76, 150, 0) with self.assertRaises(AssertionError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, None, 150, 0) def test_instantiations_failing_upon_wrong_geocoords(self): with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -422.43, 37.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, -97.76, 150, 0) with self.assertRaises(ValueError): Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, -56.9, 0) def test_repr(self): print(self._test_instance) def test_to_JSON(self): expected = '''{ "alt": 150, "name": "San Francisco Test Station", "lat": 37.76, "lon": -122.43, "created_at": "2016-11-22 12:15:25+00", "external_id": "SF_TEST001", "id": "583436dd9643a9000196b8d6", "rank": 0, "updated_at": "2016-11-22 12:15:25+00"}''' instance = Station("583436dd9643a9000196b8d6", "2016-11-22T12:15:25.967Z", "2016-11-22T12:15:25.967Z", "SF_TEST001", "San Francisco Test Station", -122.43, 37.76, 150, 0) result = instance.to_JSON() self.assertEquals(json.loads(expected), json.loads(result)) def test_repr(self): str(self._test_instance)