def test_get_driver_info(self): res_with_id = api.get_driver_info('alonso') res_with_no = api.get_driver_info('14') res_with_code = api.get_driver_info('ALO') self.assertEqual(res_with_id['id'], 'alonso') self.assertEqual(res_with_no['id'], 'alonso') self.assertEqual(res_with_no['number'], '14') self.assertEqual(res_with_code['id'], 'alonso') self.assertEqual(res_with_code['code'], 'ALO')
async def test_get_all_laps_for_driver(self, mock_fetch): mock_fetch.return_value = get_mock_response('all_laps') driver = api.get_driver_info('alonso') laps = await api.get_all_laps(15, 2008) res = await api.get_all_laps_for_driver(driver, laps) self.check_data(res['data']) self.assertEqual(res['data'][0]['Lap'], 1, "First lap should be 1.") self.assertEqual(res['driver']['surname'], 'Alonso', "Driver doesn't match that provided.")
def plot_race_pos(lap_timings): """Plot line graph visualising race position change per driver. `lap_timings` : dict lap:timings pairs for each lap and driver, respectively, as returned by `api.get_all_laps` or filtered laps from `utils.filter_laps_by_driver()`. """ laps = np.array(list(lap_timings['data'].keys()), int) # Get all the drivers from the first lap drivers = [x['id'] for x in lap_timings['data'][1]] fig = plt.figure(figsize=FIGSIZE) # Reshape data from per lap to per driver # Get each driver:value pair for i, driver in enumerate(drivers, 0): driver_info = get_driver_info(driver) # Get only the data for current driver filtered_laps = filter_laps_by_driver(lap_timings, [driver]) # Add the race pos per lap to the list of positions for the current driver positions = np.array([ int(x[0]['Pos']) if x else None for x in filtered_laps['data'].values() ], dtype=object) # Plot the drivers positions plt.plot(laps, positions, figure=fig, label=driver_info['code']) plt.title( f"Race position - {lap_timings['race']} ({lap_timings['season']})") plt.xlabel('Lap') plt.yticks(np.arange(1, len(drivers))) plt.ylabel('Position') plt.gca().invert_yaxis() plt.gca().tick_params(axis='y', right=True, left=True, labelleft=True, labelright=True) plt.legend(title='Drivers', bbox_to_anchor=(-0.05, 1.04), loc='upper right') save_figure(fig, name='plot_pos.png')
async def test_get_driver_career(self, mock_fetch): mock_fetch.side_effect = [ get_mock_response('driver_championships'), get_mock_response('driver_wins'), get_mock_response('driver_poles'), get_mock_response('driver_seasons'), get_mock_response('driver_teams'), ] driver = api.get_driver_info('alonso') res = await api.get_driver_career(driver) self.assertEqual(res['driver']['surname'], 'Alonso') # Check length of results data = res['data'] self.check_total_and_num_results(data['Championships']['total'], data['Championships']['years']) self.check_total_and_num_results(data['Seasons']['total'], data['Seasons']['years']) self.check_total_and_num_results(data['Teams']['total'], data['Teams']['names'])
def plot_all_driver_laps(lap_timings): """Plot all race lap times for the driver(s) as a line graph and output a file. Parameters ---------- `lap_timings` : dict The dict returned by `api.get_all_laps_for_driver()` or filtered laps from `utils.filter_laps_by_driver()`. """ # Get data arrays laps = np.array(list(lap_timings['data'].keys()), int) # Get all the drivers from the first lap drivers = [x['id'] for x in lap_timings['data'][1]] # Plot data fig = plt.figure(figsize=FIGSIZE) # Reshape data from per lap to per driver # Get each driver:value pair for i, driver in enumerate(drivers, 0): driver_info = get_driver_info(driver) # Get only the data for current driver filtered_laps = filter_laps_by_driver(lap_timings, [driver]) # Get the times for each lap and convert to seconds # or fill with 0 if there is no data for that lap (e.g. retired) times = np.array([ lap_time_to_seconds(lap[0]['Time']) if lap else None for lap in filtered_laps['data'].values() ], object) # Plot the drivers lap times plt.plot(laps, times, figure=fig, label=driver_info['code']) plt.title(f"Lap Times {lap_timings['race']} ({lap_timings['season']})") plt.xlabel("Lap") plt.ylabel("Time (s)") plt.grid(axis='y') plt.legend(bbox_to_anchor=(1, 1), loc='upper left') save_figure(fig, name='plot_laps.png')
def test_get_driver_info_with_invalid_driver(self): with self.assertRaises(DriverNotFoundError): api.get_driver_info('smc12')
def test_get_driver_info_code_or_number_conversion(self): res = api.get_driver_info('abate') self.assertEqual(res['id'], 'abate') self.assertTrue(res['number'] is None) self.assertTrue(res['code'] is None)