def test_get_fuel_prices_for_station(self, m: Mocker) -> None: m.get('{}/prices/station/100'.format(API_URL_BASE), json={ 'prices': [{ 'fueltype': 'E10', 'price': 146.9, 'lastupdated': '02/06/2018 02:03:04', }, { 'fueltype': 'P95', 'price': 150.0, 'lastupdated': '02/06/2018 02:03:04', }] }) client = FuelCheckClient() result = client.get_fuel_prices_for_station(100) self.assertEqual(len(result), 2) self.assertEqual(result[0].fuel_type, 'E10') self.assertEqual(result[0].price, 146.9) self.assertEqual( result[0].last_updated, datetime.datetime( day=2, month=6, year=2018, hour=2, minute=3, second=4, ))
def test_get_fuel_prices_for_station_server_error(self, m: Mocker) -> None: m.get( '{}/prices/station/21199'.format(API_URL_BASE), status_code=500, text='Internal Server Error.', ) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_fuel_prices_for_station(21199) self.assertEqual(str(cm.exception), 'Internal Server Error.')
def test_get_reference_data_server_error(self, m: Mocker) -> None: m.get( '{}/lovs'.format(API_URL_BASE), status_code=500, text='Internal Server Error.', ) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_reference_data() self.assertEqual(str(cm.exception), 'Internal Server Error.')
def test_get_fuel_price_trends_server_error(self, m: Mocker) -> None: m.post( '{}/prices/trends/'.format(API_URL_BASE), status_code=500, text='Internal Server Error.', ) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_fuel_price_trends(longitude=151.0, latitude=-33.0, fuel_types=['E10', 'P95']) self.assertEqual(str(cm.exception), 'Internal Server Error.')
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the NSW Fuel Station sensor.""" station_id = config[CONF_STATION_ID] fuel_types = config[CONF_FUEL_TYPES] client = FuelCheckClient() station_data = StationPriceData(client, station_id) station_data.update() if station_data.error is not None: message = ( "Error: {}. Check the logs for additional information.").format( station_data.error) hass.components.persistent_notification.create( message, title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID) return available_fuel_types = station_data.get_available_fuel_types() add_entities([ StationPriceSensor(station_data, fuel_type) for fuel_type in fuel_types if fuel_type in available_fuel_types ])
def test_get_reference_data_client_error(self, m: Mocker) -> None: m.get('{}/lovs'.format(API_URL_BASE), status_code=400, json={ "errorDetails": { "code": "-2146233033", "message": "String was not recognized as a valid DateTime." } }) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_reference_data() self.assertEqual(str(cm.exception), 'String was not recognized as a valid DateTime.')
def test_get_fuel_prices_for_station_client_error(self, m: Mocker) -> None: m.get('{}/prices/station/21199'.format(API_URL_BASE), status_code=400, json={ "errorDetails": [{ "code": "E0014", "description": "Invalid service station code \"21199\"" }] }) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_fuel_prices_for_station(21199) self.assertEqual(str(cm.exception), 'Invalid service station code "21199"')
def test_get_fuel_prices(self, m: Mocker) -> None: fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures/all_prices.json') with open(fixture_path) as fixture: m.get('{}/prices'.format(API_URL_BASE), json=json.load(fixture)) client = FuelCheckClient() response = client.get_fuel_prices() self.assertEqual(len(response.stations), 2) self.assertEqual(len(response.prices), 5) self.assertEqual(response.stations[0].name, 'Cool Fuel Brand Hurstville') self.assertEqual(response.stations[1].name, 'Fake Fuel Brand Kogarah') self.assertEqual(response.prices[0].fuel_type, 'DL') self.assertEqual(response.prices[1].fuel_type, 'E10') self.assertEqual(response.prices[1].station_code, 1) self.assertEqual(response.prices[3].fuel_type, 'P95') self.assertEqual(response.prices[3].station_code, 2)
class FuelCheckClientIntegrationTest(unittest.TestCase): def setUp(self): self.client = FuelCheckClient() def test_get_reference_data(self) -> None: response = self.client.get_reference_data() self.assertGreater(len(response.stations), 1500) def test_get_fuel_prices(self) -> None: response = self.client.get_fuel_prices() self.assertGreater(len(response.stations), 1500) self.assertGreater(len(response.prices), 1500) def test_get_fuel_prices_for_station(self) -> None: response = self.client.get_reference_data() station_id = response.stations[0].code response = self.client.get_fuel_prices_for_station(station_id) self.assertGreaterEqual(len(response), 1)
def test_get_reference_data(self, m: Mocker) -> None: fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures/lovs.json') with open(fixture_path) as fixture: m.get('{}/lovs'.format(API_URL_BASE), json=json.load(fixture)) client = FuelCheckClient() response = client.get_reference_data() self.assertEqual(len(response.brands), 2) self.assertEqual(len(response.fuel_types), 2) self.assertEqual(len(response.stations), 2) self.assertEqual(len(response.trend_periods), 2) self.assertEqual(len(response.sort_fields), 2) self.assertEqual(response.brands[0], 'Cool Fuel Brand') self.assertEqual(response.fuel_types[0].code, 'E10') self.assertEqual(response.fuel_types[0].name, 'Ethanol 94') self.assertEqual(response.stations[0].name, 'Cool Fuel Brand Hurstville') self.assertEqual(response.trend_periods[0].period, 'Day') self.assertEqual(response.trend_periods[0].description, 'Description for day') self.assertEqual(response.sort_fields[0].code, 'Sort 1') self.assertEqual(response.sort_fields[0].name, 'Sort field 1')
def fetch_station_price_data( client: FuelCheckClient) -> StationPriceData | None: """Fetch fuel price and station data.""" try: raw_price_data = client.get_fuel_prices() # Restructure prices and station details to be indexed by station code # for O(1) lookup return StationPriceData( stations={s.code: s for s in raw_price_data.stations}, prices={(p.station_code, p.fuel_type): p.price for p in raw_price_data.prices}, ) except FuelCheckError as exc: _LOGGER.error("Failed to fetch NSW Fuel station price data. %s", exc) return None
async def async_setup(hass, config): """Set up the NSW Fuel Station platform.""" client = FuelCheckClient() async def async_update_data(): return await hass.async_add_executor_job(fetch_station_price_data, client) coordinator = DataUpdateCoordinator( hass, _LOGGER, name="sensor", update_interval=SCAN_INTERVAL, update_method=async_update_data, ) hass.data[DATA_NSW_FUEL_STATION] = coordinator await coordinator.async_refresh() return True
def test_get_fuel_prices_within_radius(self, m: Mocker) -> None: m.post('{}/prices/nearby'.format(API_URL_BASE), json={ 'stations': [ { 'stationid': 'SAAAAAA', 'brandid': 'BAAAAAA', 'brand': 'Cool Fuel Brand', 'code': 678, 'name': 'Cool Fuel Brand Luxembourg', 'address': '123 Fake Street', 'location': {}, }, { 'stationid': 'SAAAAAB', 'brandid': 'BAAAAAB', 'brand': 'Fake Fuel Brand', 'code': 679, 'name': 'Fake Fuel Brand Luxembourg', 'address': '123 Fake Street', 'location': {}, }, { 'stationid': 'SAAAAAB', 'brandid': 'BAAAAAB', 'brand': 'Fake Fuel Brand2', 'code': 880, 'name': 'Fake Fuel Brand2 Luxembourg', 'address': '123 Fake Street', 'location': {}, }, ], 'prices': [{ 'stationcode': 678, 'fueltype': 'P95', 'price': 150.9, 'priceunit': 'litre', 'description': None, 'lastupdated': '2018-06-02 00:46:31' }, { 'stationcode': 678, 'fueltype': 'P95', 'price': 130.9, 'priceunit': 'litre', 'description': None, 'lastupdated': '2018-06-02 00:46:31' }, { 'stationcode': 880, 'fueltype': 'P95', 'price': 155.9, 'priceunit': 'litre', 'description': None, 'lastupdated': '2018-06-02 00:46:31' }], }) client = FuelCheckClient() result = client.get_fuel_prices_within_radius( longitude=151.0, latitude=-33.0, radius=10, fuel_type='E10', ) self.assertEqual(len(result), 3) self.assertEqual(result[0].station.code, 678) self.assertEqual(result[0].price.price, 150.9)
def test_get_fuel_price_trends(self, m: Mocker) -> None: m.post('{}/prices/trends/'.format(API_URL_BASE), json={ 'Variances': [ { 'Code': 'E10', 'Period': 'Day', 'Price': 150.0 }, { 'Code': 'E10', 'Period': 'Week', 'Price': 151.0 }, { 'Code': 'E10', 'Period': 'Month', 'Price': 152.0 }, { 'Code': 'E10', 'Period': 'Year', 'Price': 153.0 }, { 'Code': 'P95', 'Period': 'Day', 'Price': 150.0 }, { 'Code': 'P95', 'Period': 'Week', 'Price': 151.0 }, { 'Code': 'P95', 'Period': 'Month', 'Price': 152.0 }, { 'Code': 'P95', 'Period': 'Year', 'Price': 153.0 }, ], 'AveragePrices': [{ 'Code': 'E10', 'Period': 'Day', 'Price': 150.0, 'Captured': '2018-06-02' }, { 'Code': 'E10', 'Period': 'Year', 'Price': 151.0, 'Captured': 'October 2017' }], }) client = FuelCheckClient() result = client.get_fuel_price_trends(longitude=151.0, latitude=-33.0, fuel_types=['E10', 'P95']) self.assertEqual(len(result.variances), 8) self.assertEqual(result.variances[0].price, 150.0) self.assertEqual(result.variances[0].period, Period.DAY) self.assertEqual(result.variances[0].fuel_type, 'E10') self.assertEqual(len(result.average_prices), 2) self.assertEqual(result.average_prices[0].fuel_type, 'E10') self.assertEqual(result.average_prices[0].period, Period.DAY) self.assertEqual(result.average_prices[0].captured, datetime.datetime(year=2018, month=6, day=2)) self.assertEqual(result.average_prices[0].price, 150.0) self.assertEqual(result.average_prices[1].period, Period.YEAR) self.assertEqual(result.average_prices[1].captured, datetime.datetime(year=2017, month=10, day=1))
def test_construction(self) -> None: FuelCheckClient()
def setUp(self): self.client = FuelCheckClient()