def test_preview_scan(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 0 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'preview_scan' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '7d185cd48e077baefaf7bc216488ee49' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_preview_scan(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 0 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({'id': id, 'method': 'preview_scan'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '1b9723cd7e9ecd54f28c7dae13e38511' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_describe(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'describe', 'params': { 'method': 'acquisition_params' } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert response.json()['result'] \ == ApiAdapter.acquisition_params.description # Try describing a method that doesn't exist request = jsonrpc_message({ 'id': id, 'method': 'describe', 'params': { 'method': 'no_where_man' } }) response = requests.post(acquisition_server.url, json=request) expected = { 'message': 'Method no_where_man not found.', 'code': -32000 } assert response.status_code == 500 error = response.json()['error'] del error['data'] assert error == expected
def test_stem_acquire(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'stem_rotation': 2.1 } }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({'id': id, 'method': 'stem_acquire'}) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '3b3d4b6163f48ec0f665fe114fac0d15' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_invalid_request(handler): request = {} response = handler.rpc(request) excepted = jsonrpc_message({ 'id': None, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted id = 1234 request = {'id': id} response = handler.rpc(request) excepted = jsonrpc_message({ 'id': id, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted request = {'id': id, 'method': 'foo'} response = handler.rpc(request) excepted = jsonrpc_message({ 'id': id, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted
def test_connected_missing_regex(passive_acquisition_server, tmpdir): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath, 'fileNameRegexGroups': ['angle'], 'groupRegexSubstitutions': [{ 'n[': '-' }] } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 500, response.content request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath, 'fileNameRegex': '.*', 'groupRegexSubstitutions': [{ 'n[': '-' }] } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 500, response.content
def test_describe(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'describe', 'params': { 'method': 'acquisition_params' } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert response.json()['result'] \ == ApiAdapter.acquisition_params.description # Try describing a method that doesn't exist request = jsonrpc_message({ 'id': id, 'method': 'describe', 'params': { 'method': 'no_where_man' } }) response = requests.post(acquisition_server.url, json=request) expected = {'message': 'Method no_where_man not found.', 'code': -32000} assert response.status_code == 500 error = response.json()['error'] del error['data'] assert error == expected
def test_stem_acquire(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 1 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({'id': id, 'method': 'stem_acquire'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '2dbadcaa028e763a0a69efd371b48c9d' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_acquisition_params(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'acquisition_params', }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 expected = { 'test': 1, 'foo': 'foo' } assert response.status_code == 200 assert response.json()['result'] == expected # Now update request = jsonrpc_message({ 'id': id, 'method': 'acquisition_params', 'params': { 'foo': 'bar' } }) response = requests.post(acquisition_server.url, json=request) expected = { 'test': 1, 'foo': 'bar' } assert response.status_code == 200 assert response.json()['result'] == expected
def test_method_dict_params(handler): message = 'Hey!' def test(message=None): return { 'data': message } handler.add_method('test', test) id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'test', 'params': { 'message': message } }) response = handler.rpc(request) expected = jsonrpc_message({ 'id': id, 'result': { 'data': message } }) assert response == expected
def test_stem_acquire(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 0 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({'id': id, 'method': 'stem_acquire'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '7d185cd48e077baefaf7bc216488ee49' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected # Try out of range request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 74 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({'id': id, 'method': 'stem_acquire'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = 'ac70e27a7db5710e1433393adeda4940' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_method_no_params(handler): def test(): return {'data': 'big data!'} handler.add_method('test', test) id = 1234 request = jsonrpc_message({'id': id, 'method': 'test'}) response = handler.rpc(request) expected = jsonrpc_message({'id': id, 'result': {'data': 'big data!'}}) assert response == expected
def test_connection(acquisition_server): id = 1234 request = jsonrpc_message({'id': id, 'method': 'connect'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert response.json()['result'] # Now disconnect request = jsonrpc_message({'id': id, 'method': 'disconnect'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert not response.json()['result']
def test_method_not_found(handler): id = 1234 request = jsonrpc_message({'id': id, 'method': 'foo'}) response = handler.rpc(request) expected = jsonrpc_message({ 'id': id, 'error': { 'message': 'Method "foo" not found.', 'code': -32601, 'data': 'foo' } }) assert response == expected
def test_tiff_stem_acquire(passive_acquisition_server, tmpdir, mock_tiff_tiltseries_writer): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath, 'fileNameRegex': r'.*\.tif' } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) tilt_series = [] for _ in range(0, 1000): response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content # No images left to fetch result = response.json()['result'] if result is None: continue url = result['imageUrl'] response = requests.get(url) tilt_series.append(response.content) if len(tilt_series) == mock_tiff_tiltseries_writer.series_size: break # make sure we got all the images assert len(tilt_series) == mock_tiff_tiltseries_writer.series_size # Now check we got the write images with Image.open(test_image()) as image_stack: for i in range(0, image_stack.n_frames): md5 = hashlib.md5() md5.update(tilt_series[i]) image_stack.seek(i) image_slice = tobytes(image_stack) expected_md5 = hashlib.md5() expected_md5.update(image_slice) assert md5.hexdigest() == expected_md5.hexdigest()
def test_tilt_params(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 23 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 expected = jsonrpc_message({'id': id, 'result': 23}) assert response.json() == expected
def test_method_not_found(handler): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'foo' }) response = handler.rpc(request) expected = jsonrpc_message({ 'id': id, 'error': { 'message': 'Method "foo" not found.', 'code': -32601, 'data': 'foo' } }) assert response == expected
def test_method_list_params(handler): message = 'Hey!' def test(message): return {'data': message} handler.add_method('test', test) id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'test', 'params': [message] }) response = handler.rpc(request) expected = jsonrpc_message({'id': id, 'result': {'data': message}}) assert response == expected
def test_tilt_params(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 23 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 expected = jsonrpc_message({ 'id': id, 'result': 23 }) assert response.json() == expected
def test_method_not_found(acquisition_server): source = 'test' id = 1234 request = jsonrpc_message({'id': id, 'method': 'test', 'params': [source]}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 404 expected = jsonrpc_message({ 'id': id, 'error': { 'message': 'Method "test" not found.', 'data': 'test', 'code': -32601 } }) assert response.json() == expected
def test_connection(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert response.json()['result'] # Now disconnect request = jsonrpc_message({ 'id': id, 'method': 'disconnect' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 assert not response.json()['result']
def test_describe_adapter(acquisition_server): id = 1234 request = jsonrpc_message({'id': id, 'method': 'describe'}) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200, response.json() expected = { 'name': '%s.%s' % (inspect.getmodule(ApiAdapter).__name__, ApiAdapter.__name__) } assert response.json()['result'] \ == expected, response.json()
def test_not_connected(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'stem_rotation': 2.1 } }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 500
def test_connected_invalid_path(passive_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': '/bogus' } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 500, response.content
def test_acquisition_params(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'acquisition_params', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 expected = { 'calX': 1.2e-08, 'calY': 1.2e-08, 'units': 'nm' } assert response.json()['result']['size'] == expected
def test_invalid_content_type(acquisition_server): response = requests.post(acquisition_server.url, data='test') expected = jsonrpc_message({ 'id': None, 'error': { 'message': 'Invalid content type.', 'data': 'text/plain', 'code': -32700 } }) assert response.json() == expected
def test_tilt_params(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 2.1 } }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 assert response.json()['result'] == 2.1
def test_method_not_found(acquisition_server): source = 'test' id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'test', 'params': [source] }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 404 expected = jsonrpc_message({ 'id': id, 'error': { 'message': 'Method "test" not found.', 'data': 'test', 'code': -32601 } }) assert response.json() == expected
def test_invalid_request(handler): request = {} response = handler.rpc(request) excepted = jsonrpc_message({ 'id': None, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted id = 1234 request = { 'id': id } response = handler.rpc(request) excepted = jsonrpc_message({ 'id': id, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted request = { 'id': id, 'method': 'foo' } response = handler.rpc(request) excepted = jsonrpc_message({ 'id': id, 'error': { 'message': 'Invalid request.', 'code': -32600 } }) assert response == excepted
def test_stem_acquire(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 2.1 } }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '0cdcc5139186b0cbb84042eacfca1a13' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_method_no_params(handler): def test(): return { 'data': 'big data!' } handler.add_method('test', test) id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'test' }) response = handler.rpc(request) expected = jsonrpc_message({ 'id': id, 'result': { 'data': 'big data!' } }) assert response == expected
def test_describe_adapter(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'describe' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200, response.json() expected = { 'name': '%s.%s' % (inspect.getmodule(ApiAdapter).__name__, ApiAdapter.__name__) } assert response.json()['result'] \ == expected, response.json()
def test_invalid_json(acquisition_server): headers = {'content-type': 'application/json'} response = requests.post(acquisition_server.url, headers=headers, data='test') expected = jsonrpc_message({ 'id': None, 'error': { 'message': 'Invalid JSON.', 'code': -32700 } }) response_json = response.json() del response_json['error']['data'] assert response_json == expected
def test_acquisition_params(fei_acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'acquisition_params', }) response = requests.post(fei_acquisition_server.url, json=request) assert response.status_code == 200 expected = {'dwell_time': 3.1, 'binning': 10, 'image_size': 'FULL'} assert response.json()['result'] == expected # Now update request = jsonrpc_message({ 'id': id, 'method': 'acquisition_params', 'params': { 'dwell_time': 5.2, 'binning': 100, 'image_size': 1 } }) response = requests.post(fei_acquisition_server.url, json=request) expected = { 'dwell_time': 5.2, 'binning': 100, 'image_size': 'ACQIMAGESIZE_HALF' } assert response.status_code == 200 assert response.json()['result'] == expected
def test_connected(passive_acquisition_server, tmpdir): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content # As we haven't started a mock tilt series write calls to should # return noting response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200 result = response.json()['result'] assert result is None
def test_stem_acquire(acquisition_server): id = 1234 request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 0 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = '7d185cd48e077baefaf7bc216488ee49' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected # Try out of range request = jsonrpc_message({ 'id': id, 'method': 'tilt_params', 'params': { 'angle': 74 } }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) response = requests.post(acquisition_server.url, json=request) assert response.status_code == 200 # Now fetch the image url = response.json()['result'] response = requests.get(url) assert response.status_code == 200 expected = 'ac70e27a7db5710e1433393adeda4940' md5 = hashlib.md5() md5.update(response.content) assert md5.hexdigest() == expected
def test_deploy(acquisition_dev_server, sentinel_path1, sentinel_path2): adapter_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'source1.py') with open(adapter_path) as fp: src = fp.read() request = jsonrpc_message({ 'id': 1234, 'method': 'deploy_adapter', 'params': ['foo', 'ApiAdapter1', src] }) url = '%s/dev' % acquisition_dev_server.base_url response = requests.post(url, json=request) assert response.status_code == 200 magic = '299792458' # Call connect and make sure it writes out a file request = jsonrpc_message({ 'id': 1234, 'method': 'connect', 'params': { 'magic': magic } }) assert not os.path.exists(sentinel_path1) response = requests.post(acquisition_dev_server.url, json=request) assert response.status_code == 200 assert os.path.exists(sentinel_path1) with open(sentinel_path1) as fp: assert fp.read() == magic # Now redeploy adapter_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'source2.py') with open(adapter_path) as fp: src = fp.read() request = jsonrpc_message({ 'id': 1234, 'method': 'deploy_adapter', 'params': ['foo', 'ApiAdapter2', src] }) url = '%s/dev' % acquisition_dev_server.base_url response = requests.post(url, json=request) assert response.status_code == 200 magic = '299792458' # Call connect and make sure it writes out a file request = jsonrpc_message({ 'id': 1234, 'method': 'connect', 'params': { 'magic': magic } }) assert not os.path.exists(sentinel_path2) response = requests.post(acquisition_dev_server.url, json=request) assert response.status_code == 200 assert os.path.exists(sentinel_path2) with open(sentinel_path2) as fp: assert fp.read() == '%sx2' % magic
def test_dm3_stem_acquire(passive_acquisition_server, tmpdir, mock_dm3_tiltseries_writer): id = 1234 angle_regex = r'.*_([n,p]{1}[\d,\.]+)degree.*\.dm3' request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath, 'fileNameRegex': angle_regex, 'fileNameRegexGroups': ['angle'], 'groupRegexSubstitutions': { 'angle': [{ 'n': '-', 'p': '+' }] } } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) tilt_series = [] tilt_series_metadata = [] for _ in range(0, 1000): response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content # No images left to fetch result = response.json()['result'] if result is None: continue url = result['imageUrl'] response = requests.get(url) tilt_series.append(response.content) if 'meta' in result: tilt_series_metadata.append(result['meta']) if len(tilt_series) == mock_dm3_tiltseries_writer.series_size: break # make sure we got all the images assert len(tilt_series) == mock_dm3_tiltseries_writer.series_size # Now check we got the write images for (i, (filename, fp)) in enumerate(test_dm3_tilt_series()): dm3_file = dm3.DM3(fp) expected_metadata \ = {k: v.decode('utf8') for k, v in dm3_file.info.items()} expected_metadata['fileName'] = filename angle = re.match(angle_regex, filename).group(1) angle = angle.replace('n', '-') angle = angle.replace('p', '+') expected_metadata['angle'] = angle assert tilt_series_metadata[i] == expected_metadata md5 = hashlib.md5() md5.update(tilt_series[i]) image = Image.fromarray(dm3_file.imagedata) tiff_image_data = tobytes(image) expected_md5 = hashlib.md5() expected_md5.update(tiff_image_data) assert md5.hexdigest() == expected_md5.hexdigest()