def test_get_program_client_initialization_failure(self, mock_init): """ Verify that the function 'get_program' raises exception when API client fails to initialize. """ mock_init.side_effect = Exception with self.assertRaises(Exception): get_program(self.program_id)
def test_get_program_data_retrieval_failure(self): """ Verify that an empty list will be returned if the data can't be retrieved from the Programs API due to a server error. """ self.mock_programs_api_500(program_id=self.program_id) with self.assertRaises(exceptions.HttpServerError): get_program(self.program_id)
def test_get_program_with_no_data(self): """ Verify that the function 'get_program' raises exception if the Programs API gives 404. """ self.mock_programs_api_404(program_id=self.program_id) with self.assertRaises(exceptions.HttpNotFoundError): get_program(self.program_id)
def test_get_program_caching(self): """ Verify that when the value is set, the cache is used for getting programs. """ self.mock_programs_api(program_id=self.program_id) # hit the Programs API twice for _ in range(2): get_program(self.program_id) # verify that only one request has been made self.assertEqual(len(httpretty.httpretty.latest_requests), 1)
def test_get_program(self): """ Verify that the programs data can be retrieved. """ self.mock_programs_api(program_id=self.program_id) actual_programs_api_response = get_program(self.program_id) self.assertEqual( actual_programs_api_response, self.PROGRAMS_API_RESPONSE ) # verify the API was actually hit (not the cache) self.assertEqual(len(httpretty.httpretty.latest_requests), 1)
def _get_program_data(self, program_id): """ Get the program data from program service. Arguments: program_id (int): Unique id of the program for retrieval Returns: dict, representing a parsed program data returned by the Program service. """ program_data = get_program(program_id) return { 'name': program_data['name'], 'course_count': len(program_data['course_codes']), 'organization_key': program_data['organizations'][0]['key'], 'category': program_data['category'], }