def test_get_fuel_prices_server_error(self, m: Mocker) -> None: m.get( '{}/prices'.format(API_URL_BASE), status_code=500, text='Internal Server Error.', ) client = FuelCheckClient() with self.assertRaises(FuelCheckError) as cm: client.get_fuel_prices() self.assertEqual(str(cm.exception), 'Internal Server Error.')
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
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)