def read_list_from_caom(config): query = "SELECT A.uri FROM caom2.Observation AS O " \ "JOIN caom2.Plane AS P ON O.obsID = P.obsID " \ "JOIN caom2.Artifact AS A ON P.planeID = A.planeID " \ "WHERE O.collection='{}'".format(config.archive) subject = net.Subject(certificate=config.proxy_fqn) tap_client = CadcTapClient(subject, resource_id=config.tap_id) buffer = io.BytesIO() tap_client.query(query, output_file=buffer) temp = parse_single_table(buffer).to_table() return [ii.decode().replace('ad:{}/'.format(config.archive), '').strip() for ii in temp['uri']]
def test_query(base_post_mock): client = CadcTapClient(net.Subject()) # default format def_name = 'tmptable' def_table = os.path.join(TESTDATA_DIR, 'votable.xml') fields = {'LANG': 'ADQL', 'QUERY': 'query', 'FORMAT': 'VOTable'} tablefile = os.path.basename(def_table) fields['UPLOAD'] = '{},param:{}'.format(def_name, tablefile) fields[tablefile] = (def_table, open(def_table, 'rb')) client.query('query', tmptable='tmptable:'+def_table) print(base_post_mock.call_args_list[0][0][0]) assert base_post_mock.call_args_list[0][0][0] == \ (QUERY_CAPABILITY_ID, None, 'uws:Sync')
def test_query(caps_get_mock, base_post_mock): caps_get_mock.return_value = BASE_URL response = Mock() response.status_code = 200 response.raw.read.return_value = b'<VOTable format>' response.text = 'Header 1\nVal1\nVal2\n' # NOTE: post mock returns a context manager with the responose, hence # the __enter__ base_post_mock.return_value.__enter__.return_value = response client = CadcTapClient(net.Subject()) # default format def_name = 'tmptable' def_table = os.path.join(TESTDATA_DIR, 'votable.xml') fields = {'LANG': 'ADQL', 'QUERY': 'query', 'FORMAT': 'VOTable'} tablefile = os.path.basename(def_table) fields['UPLOAD'] = '{},param:{}'.format(def_name, tablefile) fields[tablefile] = (def_table, open(def_table, 'rb')) with patch('cadctap.core.sys.stdout', new_callable=BytesIO): client.query('query', tmptable='tmptable:'+def_table) assert base_post_mock.call_args_list[0][0][0] == \ '{}/{}'.format(BASE_URL, 'sync') base_post_mock.reset_mock() with patch('sys.stdout', new_callable=StringIO) as stdout_mock: client.query('query', data_only=True, response_format='tsv') assert stdout_mock.getvalue() == 'Val1\nVal2\n' # save in a file tf = tempfile.NamedTemporaryFile() base_post_mock.reset_mock() base_post_mock.return_value.__enter__.return_value = response client.query('query', output_file=tf.name, response_format='tsv') actual = open(tf.name).read() assert actual == 'Header 1\n-----------------------\n' \ 'Val1\nVal2\n\n(2 rows affected)\n' # different format => result from server not altered base_post_mock.reset_mock() base_post_mock.return_value.__enter__.return_value = response with patch('sys.stdout', new_callable=BytesIO) as stdout_mock: client.query('query') assert stdout_mock.getvalue() == response.raw.read()
subject = net.Subject(certificate=proxy_fqn) ad_client = CadcTapClient(subject, resource_id='ivo://cadc.nrc.ca/ad') ops_client = CadcTapClient(subject, resource_id=f'ivo://cadc.nrc.ca/ams/{service}') caom_client = CAOM2RepoClient(subject, resource_id='ivo://cadc.nrc.ca/ams') print(':::1 - Find the name of a file to test with.') ops_query = f"SELECT O.observationID, A.uri " \ f"FROM caom2.Observation as O " \ f"JOIN caom2.Plane as P on O.obsID = P.obsID " \ f"JOIN caom2.Artifact as A on P.planeID = A.planeID " \ f"WHERE O.collection = '{archive}' " \ f"AND A.uri like '%.fits%' " \ f"LIMIT 1" ops_buffer = io.StringIO() ops_client.query(ops_query, output_file=ops_buffer, data_only=True, response_format='csv') ops_table = Table.read(ops_buffer.getvalue().split('\n'), format='csv') if len(ops_table) == 1: obs_id = ops_table[0]['observationID'] uri = ops_table[0]['uri'] ignore_scheme, ignore_path, f_name = mc.decompose_uri(uri) print(f':::Looking for {obs_id} and {f_name}') else: print(f':::No observation records found for collection {archive}') sys.exit(-1) obs = caom_client.read(archive, obs_id) obs_fqn = f'/usr/src/app/expected.{obs_id}.xml' mc.write_obs_to_file(obs, obs_fqn) print(f':::2 - Get {f_name}')