async def test_get_pitstops(self, mock_fetch): mock_fetch.side_effect = [ get_mock_response('pitstops'), get_mock_response('race_results') ] res = await api.get_pitstops('last', 'current') self.check_data(res['data'])
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.")
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'])
async def test_response_structure(self): # test response for alonso info against mocked data # Get BeautifulSoup obj from API response to test tags actual_result = await api.get_soup(f'{api.BASE_URL}/drivers/alonso') with patch(fetch_path) as mock_get: mock_get.return_value = get_mock_response('driver_info') # url never used as fetch is mocked expected_result = await api.get_soup('mock_url') # Check root tag of real response body for changes self.assertTrue( actual_result.body.find('mrdata'), "Parent response tag not as expected, API may have changed.") # Check tag structure matches for real and mocked self.assertEqual( expected_result.drivertable, actual_result.drivertable, "Expected and actual tags don't match. Check API data structure.")
async def test_get_driver_teams(self, mock_fetch): mock_fetch.return_value = get_mock_response('driver_teams') res = await api.get_driver_teams('alonso') self.assertTrue(res['data'], "Results empty.") self.assertEqual(len(res['data']), 1) self.assertTrue(res['data'][0] == 'Ferrari')
async def test_get_driver_seasons(self, mock_fetch): mock_fetch.return_value = get_mock_response('driver_seasons') res = await api.get_driver_seasons('alonso') self.check_data(res['data']) self.assertEqual(len(res['data']), 1) self.assertTrue(res['data'][0]['year'] == 2001)
async def test_get_driver_poles(self, mock_fetch): mock_fetch.return_value = get_mock_response('driver_poles') res = await api.get_driver_poles('alonso') self.check_data(res['data']) self.check_total_and_num_results(res['total'], res['data'])
async def test_get_all_laps(self, mock_fetch): mock_fetch.return_value = get_mock_response('all_laps') res = await api.get_all_laps(1, 2019) self.assertNotIn(None, res['data'][1])
async def test_get_qualifying_results(self, mock_fetch): mock_fetch.return_value = get_mock_response('qualifying_results') res = await api.get_qualifying_results('last', 'current') self.check_data(res['data'])
async def test_get_team_standings(self, mock_fetch): mock_fetch.return_value = get_mock_response('constructor_standings') res = await api.get_team_standings('current') self.check_data(res['data'])
async def test_none_result_raises_error(self, mock_fetch): # return None to simulate invalid API response mock_fetch.return_value = get_mock_response(None) with self.assertRaises(MissingDataError): await api.get_driver_standings('current')