def test_throws_when_name_is_missing(self, m: Mocker): mangled_response = json.loads(RESPONSE_SERVICE_LIST) mangled_response['data'][0]['resourceMetadata'].pop('name') mangled_response['data'][1]['resourceMetadata'].pop('name') m.get('/service', json=mangled_response) with self.assertRaises(piazza.InvalidResponse): piazza.get_services(pattern='^test-pattern$')
def test_throws_when_descriptors_are_malformed(self, m: Mocker): mangled_response = json.loads(RESPONSE_SERVICE_LIST) (descriptor, _) = mangled_response['data'] descriptor.pop('serviceId') m.get('/service', json=mangled_response) with self.assertRaises(piazza.InvalidResponse): piazza.get_services(pattern='^test-pattern$')
def test_calls_correct_url(self, m: Mocker): m.get('/service', text=RESPONSE_SERVICE_LIST) piazza.get_services(pattern='^test-pattern$') self.assertEqual('https://test-piazza-host.localdomain/service?', m.request_history[0].url[:45]) self.assertSetEqual({ 'perPage=100', 'keyword=%5Etest-pattern%24', }, set(m.request_history[0].url[45:].split('&')))
def list_all() -> List[Algorithm]: log = logging.getLogger(__name__) log.info('Algorithms service list all algorithms', action='Service algorithms list all ') try: log.info('Fetching beachfront services from Piazza', action='fetch services') services = piazza.get_services('^BF_Algo_') except piazza.Error as err: log.error('Service discovery failed: %s', err) raise algorithms = [] for service in services: if 'metadata' not in service.metadata: log.warning('Algorithm <%s> missing `metadata` hash', service.service_id) continue try: algorithms.append( Algorithm( interface=_extract_interface(service), description=_extract_description(service), max_cloud_cover=_extract_max_cloud_cover(service), name=_extract_name(service), service_id=service.service_id, version=_extract_version(service), )) except ValidationError as err: log.error('Algorithm conversion failed: %s', err) continue return algorithms
def test_returns_a_list_of_service_descriptors(self, m: Mocker): m.get('/service', text=RESPONSE_SERVICE_LIST) descriptors = piazza.get_services(pattern='^test-pattern$') self.assertIsInstance(descriptors, list) self.assertEqual(2, len(descriptors)) self.assertIsInstance(descriptors[0], piazza.ServiceDescriptor) self.assertIsInstance(descriptors[1], piazza.ServiceDescriptor)
def test_deserializes_canonical_data(self, m: Mocker): m.get('/service', text=RESPONSE_SERVICE_LIST) descriptors = piazza.get_services(pattern='^test-pattern$') self.assertEqual('test-id-1', descriptors[0].service_id) self.assertEqual('test-description', descriptors[0].description) self.assertEqual('test-name', descriptors[0].name) self.assertEqual('test-id-2', descriptors[1].service_id) self.assertEqual('test-description', descriptors[1].description) self.assertEqual('test-name', descriptors[1].name)
def test_deserializes_metadata(self, m: Mocker): m.get('/service', text=RESPONSE_SERVICE_LIST) descriptor = piazza.get_services(pattern='^test-pattern$')[0] self.assertEqual( { 'classType': { 'classification': 'UNCLASSIFIED' }, 'version': 'test-version' }, descriptor.metadata)
def test_throws_when_data_is_malformed(self, m: Mocker): mangled_response = json.loads(RESPONSE_SERVICE_LIST) mangled_response['data'] = {} m.get('/service', json=mangled_response) with self.assertRaises(piazza.InvalidResponse): piazza.get_services(pattern='^test-pattern$')
def test_throws_when_piazza_is_unreachable(self, _): with unittest.mock.patch('requests.get') as stub: stub.side_effect = ConnectionError(request=unittest.mock.Mock()) with self.assertRaises(piazza.Unreachable): piazza.get_services(pattern='^test-pattern$')
def test_handles_http_errors_gracefully(self, m: Mocker): m.get('/service', text=RESPONSE_ERROR_GENERIC, status_code=500) with self.assertRaises(piazza.ServerError): piazza.get_services(pattern='^test-pattern$')