def test_scan_upload_with_retrying(fixture_problems_with_storage: Any, prepare_environment: Any, synchronous_celery: Any) -> None: """Test application for Scan upload with retrying.""" api_client = get_api_client() user_token = get_token_for_logged_in_user('admin') # Step 1. Prepare a structure for the test DatasetsRepository.add_new_dataset('KIDNEYS', 'Kidneys') # Step 2. Add Scan to the system payload = {'dataset': 'KIDNEYS', 'number_of_slices': 3} response = api_client.post('/api/v1/scans', data=json.dumps(payload), headers=get_headers(token=user_token, json=True)) json_response = json.loads(response.data) scan_id = json_response['scan_id'] # Step 3. Send Slices for file in glob.glob('tests/assets/example_scan/*.dcm'): with open(file, 'rb') as image: # First request to the API will fail with unknown error due to Storage issues fixture_problems_with_storage.side_effect = WriteTimeout('Internal Storage Error', write_type=0) response = api_client.post('/api/v1/scans/{}/slices'.format(scan_id), data={ 'image': (image, 'slice_1.dcm'), }, headers=get_headers(token=user_token, multipart=True)) assert response.status_code == 500 # After such error, UI will retry the request (this time it will be fine...) # As request will close the image file, we've got to open it again with open(file, 'rb') as image: fixture_problems_with_storage.side_effect = None response = api_client.post('/api/v1/scans/{}/slices'.format(scan_id), data={ 'image': (image, 'slice_1.dcm'), }, headers=get_headers(token=user_token, multipart=True)) assert response.status_code == 201 # Step 4. Check number of Slices in the databases z_slices = SlicesRepository.get_slices_by_scan_id(scan_id) assert len(z_slices) == 3
def to_exception(self): return WriteTimeout(self.summary_msg(), **self.info)