Exemple #1
0
    def test_load_job(self):
        connHandler = DummyConnHandlerCadc()
        tap = TapPlusCadc("http://test:1111/tap", connhandler=connHandler)
        jobid = 1234
        # Response
        response = DummyResponse()
        response.set_status_code(200)
        response.set_message("OK")
        jobDataPath = data_path('test_jobs_async.xml')
        jobData = utils.read_file_content(jobDataPath)
        response.set_data(method='GET',
                          context=None,
                          body=jobData,
                          headers=None)
        req = "async/" + str(jobid)
        connHandler.set_response(req, response)
        # Phase response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        responsePhaseHeaders = [[
            'location', 'http://test:1111/tap/async/' + str(jobid)
        ]]
        responsePhase.set_data(method='POST',
                               context=None,
                               body="COMPLETED",
                               headers=responsePhaseHeaders)
        req = "async/" + str(jobid) + "/phase"
        connHandler.set_response(req, responsePhase)
        # Results response
        responseResultsJob = DummyResponse()
        responseResultsJob.set_status_code(200)
        responseResultsJob.set_message("OK")

        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseResultsJob.set_data(method='GET',
                                    context=None,
                                    body=jobData,
                                    headers=None)
        req = "async/" + str(jobid) + "/results/result"
        connHandler.set_response(req, responseResultsJob)

        job = tap.load_async_job(jobid)

        assert job.jobid == '1234', "Jobid is wrong"
        assert job._phase == 'COMPLETED', 'Phase is wrong'
        assert job.startTime == '2016-11-17T13:33:50.755+0100', \
            "Start time is wrong"
        assert job.parameters['LANG'] == 'ADQL', 'LANG is wrong'
        assert job.parameters['QUERY'] == 'SELECT * FROM table', \
            'QUERY is wrong'
        assert job.errmessage == \
            'IllegalArgumentException:net.sf.jsqlparser.JSQLParserException',\
            'Error message is wrong'
Exemple #2
0
def test_datalink():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap",
                  datalink_context="datalink",
                  connhandler=connHandler)
    responseResultsJob = DummyResponse()
    responseResultsJob.set_status_code(200)
    responseResultsJob.set_message("OK")
    jobDataFile = data_path('job_1.vot')
    jobData = utils.read_file_content(jobDataFile)
    responseResultsJob.set_data(method='GET',
                                context=None,
                                body=jobData,
                                headers=None)
    req = "links?ID=1,2"
    connHandler.set_response(req, responseResultsJob)

    # error
    responseResultsJob.set_status_code(500)
    responseResultsJob.set_message("ERROR")
    with pytest.raises(Exception):
        # missing IDS parameter
        tap.get_datalinks(ids=None)

    # OK
    responseResultsJob.set_status_code(200)
    responseResultsJob.set_message("OK")
    # results
    results = tap.get_datalinks("1,2")
    assert len(results) == 3
    results = tap.get_datalinks([1, 2])
    assert len(results) == 3
    results = tap.get_datalinks(['1', '2'])
    assert len(results) == 3
Exemple #3
0
    def test_list_async_jobs(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        response = DummyResponse()
        response.set_status_code(500)
        response.set_message("ERROR")
        jobDataFile = data_path('jobs_list.xml')
        jobData = utils.read_file_content(jobDataFile)
        response.set_data(method='GET',
                          context=None,
                          body=jobData,
                          headers=None)
        req = "async"
        connHandler.set_response(req, response)
        with pytest.raises(Exception):
            tap.list_async_jobs()

        response.set_status_code(200)
        response.set_message("OK")
        jobs = tap.list_async_jobs()
        assert len(jobs) == 2, \
            "Wrong jobs number. Expected: %d, found %d" % \
            (2, len(jobs))
        assert jobs[0].get_jobid() == '12345', \
            "Wrong job id. Expected: %s, found %s" % \
            ('12345', jobs[0].get_jobid())
        assert jobs[0].get_phase() == 'COMPLETED', \
            "Wrong job phase for job %s. Expected: %s, found %s" % \
            (jobs[0].get_jobid(), 'COMPLETED', jobs[0].get_phase())
        assert jobs[1].get_jobid() == '77777', \
            "Wrong job id. Expected: %s, found %s" % \
            ('77777', jobs[1].get_jobid())
        assert jobs[1].get_phase() == 'ERROR', \
            "Wrong job phase for job %s. Expected: %s, found %s" % \
            (jobs[1].get_jobid(), 'ERROR', jobs[1].get_phase())
Exemple #4
0
def test_list_async_jobs():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    response = DummyResponse()
    response.set_status_code(500)
    response.set_message("ERROR")
    jobDataFile = data_path('jobs_list.xml')
    jobData = utils.read_file_content(jobDataFile)
    response.set_data(method='GET',
                      context=None,
                      body=jobData,
                      headers=None)
    req = "async"
    connHandler.set_response(req, response)
    with pytest.raises(Exception):
        tap.list_async_jobs()

    response.set_status_code(200)
    response.set_message("OK")
    jobs = tap.list_async_jobs()
    assert len(jobs) == 2
    assert jobs[0].jobid == '12345'
    assert jobs[0].get_phase() == 'COMPLETED'
    assert jobs[1].jobid == '77777'
    assert jobs[1].get_phase() == 'ERROR'
Exemple #5
0
    def test_job_load(self):
        connHandler = DummyConnHandlerCadc()
        job = JobCadc(async_job=True, query='query', connhandler=connHandler)
        jobid = 1234
        job.jobid = str(jobid)
        job.parameters['format'] = 'csv'
        # Job Phase response
        jobPhase = DummyResponse()
        jobPhase.set_status_code(200)
        jobPhase.set_message("OK")
        jobPhase.set_data(method='GET',
                          context=None,
                          body="ERROR",
                          headers=None)
        req = "async/" + str(jobid) + "/phase"
        connHandler.set_response(req, jobPhase)
        # Error response
        error = DummyResponse()
        error.set_status_code(200)
        error.set_message("OK")
        jobDataPath = data_path('test_jobs_async.xml')
        jobData = utils.read_file_content(jobDataPath)
        error.set_data(method='GET', context=None, body=jobData, headers=None)
        req = "async/" + str(jobid)
        connHandler.set_response(req, error)

        with pytest.raises(Exception):
            job.get_results()
Exemple #6
0
    def test_launch_sync_job(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(500)
        responseLaunchJob.set_message("ERROR")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        query = 'select top 5 * from table'
        dTmp = {"q": query}
        dTmpEncoded = connHandler.url_encode(dTmp)
        p = dTmpEncoded.find("=")
        q = dTmpEncoded[p + 1:]
        dictTmp = {
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable",
            "tapclient": str(TAP_CLIENT_ID),
            "PHASE": "RUN",
            "QUERY": str(q)
        }
        sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
        jobRequest = "sync?" + sortedKey
        connHandler.set_response(jobRequest, responseLaunchJob)

        with pytest.raises(Exception):
            tap.launch_job(query)

        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        job = tap.launch_job(query)
        assert job is not None, "Expected a valid job"
        assert job.async_ is False, "Expected a synchronous job"
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        assert job.failed is False, "Wrong job status (set Failed = True)"
        # results
        results = job.get_results()
        assert len(results) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(results))
        self.__check_results_column(results, 'alpha', 'alpha', None,
                                    np.float64)
        self.__check_results_column(results, 'delta', 'delta', None,
                                    np.float64)
        self.__check_results_column(results, 'source_id', 'source_id', None,
                                    np.object)
        self.__check_results_column(results, 'table1_oid', 'table1_oid', None,
                                    np.int32)
    def test_query_object(self):
        connHandler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
        tap = GaiaClass(connHandler, tapplus)
        # Launch response: we use default response because the query contains
        # decimals
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        # The query contains decimals: force default response
        connHandler.set_default_response(responseLaunchJob)
        sc = SkyCoord(ra=29.0,
                      dec=15.0,
                      unit=(u.degree, u.degree),
                      frame='icrs')
        with pytest.raises(ValueError) as err:
            tap.query_object(sc)
        assert "Missing required argument: 'width'" in err.value.args[0]

        width = Quantity(12, u.deg)

        with pytest.raises(ValueError) as err:
            tap.query_object(sc, width=width)
        assert "Missing required argument: 'height'" in err.value.args[0]

        height = Quantity(10, u.deg)
        table = tap.query_object(sc, width=width, height=height)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    np.object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)
        # by radius
        radius = Quantity(1, u.deg)
        table = tap.query_object(sc, radius=radius)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    np.object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)
Exemple #8
0
def test_job_get_results(capsys, tmpdir):
    job = Job(async_job=True)
    jobid = "12345"
    outputFormat = "votable"
    job.jobid = jobid
    job.parameters['format'] = outputFormat
    responseCheckPhase = DummyResponse()
    responseCheckPhase.set_status_code(500)
    responseCheckPhase.set_message("ERROR")
    responseCheckPhase.set_data(method='GET',
                                context=None,
                                body='FINISHED',
                                headers=None)
    waitRequest = f"async/{jobid}/phase"
    connHandler = DummyConnHandler()
    connHandler.set_response(waitRequest, responseCheckPhase)
    job.connHandler = connHandler

    with pytest.raises(Exception):
        job.get_results()

    responseCheckPhase.set_status_code(200)
    responseCheckPhase.set_message("OK")
    responseGetData = DummyResponse()
    responseGetData.set_status_code(500)
    responseGetData.set_message("ERROR")
    jobContentFileName = data_path('result_1.vot')
    jobContent = utils.read_file_content(jobContentFileName)
    responseGetData.set_data(method='GET',
                             context=None,
                             body=jobContent,
                             headers=None)
    dataRequest = f"async/{jobid}/results/result"
    connHandler.set_response(dataRequest, responseGetData)

    with pytest.raises(Exception):
        job.get_results()

    responseGetData.set_status_code(200)
    responseGetData.set_message("OK")
    res = job.get_results()
    assert len(res) == 3
    assert len(res.columns) == 4
    for cn in ['alpha', 'delta', 'source_id', 'table1_oid']:
        if cn not in res.colnames:
            pytest.fail(f"{cn} column name not found: {res.colnames}")

    # Regression test for #2299; messages were printed even with `verbose=False`
    capsys.readouterr()
    job._Job__resultInMemory = False
    job.save_results(verbose=False)
    assert 'Saving results to:' not in capsys.readouterr().out
    job.save_results(verbose=True)
    assert 'Saving results to:' in capsys.readouterr().out
Exemple #9
0
    def test_load_tables(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        responseLoadTable = DummyResponse()
        responseLoadTable.set_status_code(500)
        responseLoadTable.set_message("ERROR")
        tableDataFile = data_path('test_tables.xml')
        tableData = utils.read_file_content(tableDataFile)
        responseLoadTable.set_data(method='GET',
                                   context=None,
                                   body=tableData,
                                   headers=None)
        tableRequest = "tables"
        connHandler.set_response(tableRequest, responseLoadTable)
        with pytest.raises(Exception):
            tap.load_tables()

        responseLoadTable.set_status_code(200)
        responseLoadTable.set_message("OK")
        res = tap.load_tables()
        assert len(res) == 2, \
            "Number of tables expected: %d, found: %d" % (2, len(res))
        # Table 1
        table = self.__find_table('public', 'table1', res)
        assert table.description == 'Table1 desc', \
            "Wrong description for table1. Expected: %s, found %s" % \
            ('Table1 desc', table.description)
        columns = table.columns
        assert len(columns) == 2, \
            "Number of columns for table1. Expected: %d, found: %d" % \
            (2, len(columns))
        col = self.__find_column('table1_col1', columns)
        self.__check_column(col, 'Table1 Column1 desc', '', 'VARCHAR',
                            'indexed')
        col = self.__find_column('table1_col2', columns)
        self.__check_column(col, 'Table1 Column2 desc', '', 'INTEGER', None)
        # Table 2
        table = self.__find_table('public', 'table2', res)
        assert table.description == 'Table2 desc', \
            "Wrong description for table2. Expected: %s, found %s" % \
            ('Table2 desc', table.description)
        columns = table.columns
        assert len(columns) == 3, \
            "Number of columns for table2. Expected: %d, found: %d" % \
            (3, len(columns))
        col = self.__find_column('table2_col1', columns)
        self.__check_column(col, 'Table2 Column1 desc', '', 'VARCHAR',
                            'indexed')
        col = self.__find_column('table2_col2', columns)
        self.__check_column(col, 'Table2 Column2 desc', '', 'INTEGER', None)
        col = self.__find_column('table2_col3', columns)
        self.__check_column(col, 'Table2 Column3 desc', '', 'INTEGER', None)
Exemple #10
0
 def test_cone_search_sync(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(connHandler, tapplus)
     # Launch response: we use default response because the query contains
     # decimals
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(200)
     responseLaunchJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=jobData,
                                headers=None)
     ra = 19.0
     dec = 20.0
     sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
     radius = Quantity(1.0, u.deg)
     connHandler.set_default_response(responseLaunchJob)
     job = tap.cone_search(sc, radius)
     assert job is not None, "Expected a valid job"
     assert job.async_ is False, "Expected a synchronous job"
     assert job.get_phase() == 'COMPLETED', \
         "Wrong job phase. Expected: %s, found %s" % \
         ('COMPLETED', job.get_phase())
     assert job.failed is False, "Wrong job status (set Failed = True)"
     # results
     results = job.get_results()
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
     self.__check_results_column(results,
                                 'alpha',
                                 'alpha',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'delta',
                                 'delta',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'source_id',
                                 'source_id',
                                 None,
                                 np.object)
     self.__check_results_column(results,
                                 'table1_oid',
                                 'table1_oid',
                                 None,
                                 np.int32)
Exemple #11
0
 def test_load_tables_parameters(self):
     connHandler = DummyConnHandler()
     tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
     responseLoadTable = DummyResponse()
     responseLoadTable.set_status_code(200)
     responseLoadTable.set_message("OK")
     tableDataFile = data_path('test_tables.xml')
     tableData = utils.read_file_content(tableDataFile)
     responseLoadTable.set_data(method='GET',
                                context=None,
                                body=tableData,
                                headers=None)
     tableRequest = "tables"
     connHandler.set_response(tableRequest, responseLoadTable)
     # empty request
     tap.load_tables()
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Empty request. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names=false & share_accessible=false: equals to
     # empty request
     tap.load_tables(only_names=False, include_shared_tables=False)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Empty request. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names
     tableRequest = "tables?only_tables=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(only_names=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flag only_names. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag share_accessible=true
     tableRequest = "tables?share_accessible=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(include_shared_tables=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flag share_accessigle. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names=true & share_accessible=true
     tableRequest = "tables?only_tables=true&share_accessible=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(only_names=True, include_shared_tables=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flags only_names and share_accessible. " +\
         "Expected: '%s', found: '%s'" % \
         (tableRequest, request)
Exemple #12
0
 def test_cone_search_sync(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(tapplus)
     # Launch response: we use default response because the query contains decimals
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(200)
     responseLaunchJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=jobData,
                                headers=None)
     ra = 19.0
     dec = 20.0
     sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
     radius = Quantity(1.0, u.deg)
     connHandler.set_default_response(responseLaunchJob)
     job = tap.cone_search(sc, radius)
     assert job is not None, "Expected a valid job"
     assert job.async_ is False, "Expected a synchronous job"
     assert job.get_phase() == 'COMPLETED', \
         "Wrong job phase. Expected: %s, found %s" % \
         ('COMPLETED', job.get_phase())
     assert job.failed is False, "Wrong job status (set Failed = True)"
     # results
     results = job.get_results()
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
     self.__check_results_column(results,
                                 'alpha',
                                 'alpha',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'delta',
                                 'delta',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'source_id',
                                 'source_id',
                                 None,
                                 np.object)
     self.__check_results_column(results,
                                 'table1_oid',
                                 'table1_oid',
                                 None,
                                 np.int32)
Exemple #13
0
 def test_load_tables_parameters(self):
     connHandler = DummyConnHandler()
     tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
     responseLoadTable = DummyResponse()
     responseLoadTable.set_status_code(200)
     responseLoadTable.set_message("OK")
     tableDataFile = data_path('test_tables.xml')
     tableData = utils.read_file_content(tableDataFile)
     responseLoadTable.set_data(method='GET',
                                context=None,
                                body=tableData,
                                headers=None)
     tableRequest = "tables"
     connHandler.set_response(tableRequest, responseLoadTable)
     # empty request
     tap.load_tables()
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Empty request. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names=false & share_accessible=false: equals to empty request
     tap.load_tables(only_names=False, include_shared_tables=False)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Empty request. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names
     tableRequest = "tables?only_tables=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(only_names=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flag only_names. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag share_accessible=true
     tableRequest = "tables?share_accessible=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(include_shared_tables=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flag share_accessigle. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
     # flag only_names=true & share_accessible=true
     tableRequest = "tables?only_tables=true&share_accessible=true"
     connHandler.set_response(tableRequest, responseLoadTable)
     tap.load_tables(only_names=True, include_shared_tables=True)
     request = connHandler.get_last_request()
     assert request == tableRequest, \
         "Flags only_names and share_accessible. Expected: '%s', found: '%s'" % \
         (tableRequest, request)
Exemple #14
0
    def test_load_tables(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        responseLoadTable = DummyResponse()
        responseLoadTable.set_status_code(500)
        responseLoadTable.set_message("ERROR")
        tableDataFile = data_path('test_tables.xml')
        tableData = utils.read_file_content(tableDataFile)
        responseLoadTable.set_data(method='GET',
                                   context=None,
                                   body=tableData,
                                   headers=None)
        tableRequest = "tables"
        connHandler.set_response(tableRequest, responseLoadTable)
        with pytest.raises(Exception):
            tap.load_tables()

        responseLoadTable.set_status_code(200)
        responseLoadTable.set_message("OK")
        res = tap.load_tables()
        assert len(res) == 2, \
            "Number of tables expected: %d, found: %d" % (2, len(res))
        # Table 1
        table = self.__find_table('public', 'table1', res)
        assert table.get_description() == 'Table1 desc', \
            "Wrong description for table1. Expected: %s, found %s" % \
            ('Table1 desc', table.get_description())
        columns = table.get_columns()
        assert len(columns) == 2, \
            "Number of columns for table1. Expected: %d, found: %d" % \
            (2, len(columns))
        col = self.__find_column('table1_col1', columns)
        self.__check_column(col, 'Table1 Column1 desc', '', 'VARCHAR', 'indexed')
        col = self.__find_column('table1_col2', columns)
        self.__check_column(col, 'Table1 Column2 desc', '', 'INTEGER', None)
        # Table 2
        table = self.__find_table('public', 'table2', res)
        assert table.get_description() == 'Table2 desc', \
            "Wrong description for table2. Expected: %s, found %s" % \
            ('Table2 desc', table.get_description())
        columns = table.get_columns()
        assert len(columns) == 3, \
            "Number of columns for table2. Expected: %d, found: %d" % \
            (3, len(columns))
        col = self.__find_column('table2_col1', columns)
        self.__check_column(col, 'Table2 Column1 desc', '', 'VARCHAR', 'indexed')
        col = self.__find_column('table2_col2', columns)
        self.__check_column(col, 'Table2 Column2 desc', '', 'INTEGER', None)
        col = self.__find_column('table2_col3', columns)
        self.__check_column(col, 'Table2 Column3 desc', '', 'INTEGER', None)
Exemple #15
0
    def test_save_results(self):
        connHandler = DummyConnHandlerCadc()
        tap = TapPlusCadc("http://test:1111/tap", connhandler=connHandler)
        job = JobCadc(async_job=True, query='query', connhandler=connHandler)
        jobid = 1234
        job.jobid = str(jobid)
        job.parameters['format'] = 'csv'
        # Phase response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        responsePhaseHeaders = [[
            'location', 'http://test:1111/tap/async/' + str(jobid)
        ]]
        responsePhase.set_data(method='GET',
                               context=None,
                               body="COMPLETED",
                               headers=responsePhaseHeaders)
        req = "async/" + str(jobid) + "/phase"
        connHandler.set_response(req, responsePhase)
        # Results response
        responseResultsJob = DummyResponse()
        responseResultsJob.set_status_code(303)
        responseResultsJob.set_message("OK")
        responseResultsHeaders = [[
            'location',
            'http://test:1111/tap/async/' + str(jobid) + '/redirect'
        ]]

        responseResultsJob.set_data(method='GET',
                                    context=None,
                                    body=None,
                                    headers=responseResultsHeaders)
        req = "async/" + str(jobid) + "/results/result"
        connHandler.set_response(req, responseResultsJob)
        # Results redirect response
        responseRedirect = DummyResponse()
        responseRedirect.set_status_code(200)
        responseRedirect.set_message("OK")

        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseRedirect.set_data(method='GET',
                                  context=None,
                                  body=jobData,
                                  headers=None)
        req = "http://test:1111/tap/async/" + str(jobid) + "/redirect"
        connHandler.set_response(req, responseRedirect)
        tap.save_results(job, 'file.txt')
Exemple #16
0
    def test_job_get_results(self):
        job = Job(async_job=True)
        jobid = "12345"
        outputFormat = "votable"
        job.jobid = jobid
        job.parameters['format'] = outputFormat
        responseCheckPhase = DummyResponse()
        responseCheckPhase.set_status_code(500)
        responseCheckPhase.set_message("ERROR")
        responseCheckPhase.set_data(method='GET',
                                    context=None,
                                    body='FINISHED',
                                    headers=None)
        waitRequest = "async/"+str(jobid)+"/phase"
        connHandler = DummyConnHandler()
        connHandler.set_response(waitRequest, responseCheckPhase)
        job.connHandler = connHandler

        with pytest.raises(Exception):
            job.get_results()

        responseCheckPhase.set_status_code(200)
        responseCheckPhase.set_message("OK")
        responseGetData = DummyResponse()
        responseGetData.set_status_code(500)
        responseGetData.set_message("ERROR")
        jobContentFileName = data_path('result_1.vot')
        jobContent = utils.read_file_content(jobContentFileName)
        responseGetData.set_data(method='GET',
                                context=None,
                                body=jobContent,
                                headers=None)
        dataRequest = "async/" + str(jobid) + "/results/result"
        connHandler.set_response(dataRequest, responseGetData)

        with pytest.raises(Exception):
            job.get_results()

        responseGetData.set_status_code(200)
        responseGetData.set_message("OK")
        res = job.get_results()
        assert len(res) == 3, \
            "Num rows. Expected %d, found %d" % (3, len(res))
        assert len(res.columns) == 4, \
            "Num cols. Expected %d, found %d" % (4, len(res.columns))
        for cn in ['alpha', 'delta', 'source_id', 'table1_oid']:
            if cn not in res.colnames:
                self.fail(cn + " column name not found" + str(res.colnames))
Exemple #17
0
    def test_job_get_results(self):
        job = Job(async_job=True)
        jobid = "12345"
        outputFormat = "votable"
        job.jobid = jobid
        job.parameters['format'] = outputFormat
        responseCheckPhase = DummyResponse()
        responseCheckPhase.set_status_code(500)
        responseCheckPhase.set_message("ERROR")
        responseCheckPhase.set_data(method='GET',
                                    context=None,
                                    body='FINISHED',
                                    headers=None)
        waitRequest = "async/" + str(jobid) + "/phase"
        connHandler = DummyConnHandler()
        connHandler.set_response(waitRequest, responseCheckPhase)
        job.connHandler = connHandler

        with pytest.raises(Exception):
            job.get_results()

        responseCheckPhase.set_status_code(200)
        responseCheckPhase.set_message("OK")
        responseGetData = DummyResponse()
        responseGetData.set_status_code(500)
        responseGetData.set_message("ERROR")
        jobContentFileName = data_path('result_1.vot')
        jobContent = utils.read_file_content(jobContentFileName)
        responseGetData.set_data(method='GET',
                                 context=None,
                                 body=jobContent,
                                 headers=None)
        dataRequest = "async/" + str(jobid) + "/results/result"
        connHandler.set_response(dataRequest, responseGetData)

        with pytest.raises(Exception):
            job.get_results()

        responseGetData.set_status_code(200)
        responseGetData.set_message("OK")
        res = job.get_results()
        assert len(res) == 3, \
            "Num rows. Expected %d, found %d" % (3, len(res))
        assert len(res.columns) == 4, \
            "Num cols. Expected %d, found %d" % (4, len(res.columns))
        for cn in ['alpha', 'delta', 'source_id', 'table1_oid']:
            if cn not in res.colnames:
                self.fail(cn + " column name not found" + str(res.colnames))
Exemple #18
0
def test_launch_sync_job():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    responseLaunchJob = DummyResponse()
    responseLaunchJob.set_status_code(500)
    responseLaunchJob.set_message("ERROR")
    jobDataFile = data_path('job_1.vot')
    jobData = utils.read_file_content(jobDataFile)
    responseLaunchJob.set_data(method='POST',
                               context=None,
                               body=jobData,
                               headers=None)
    query = 'select top 5 * from table'
    dTmp = {"q": query}
    dTmpEncoded = connHandler.url_encode(dTmp)
    p = dTmpEncoded.find("=")
    q = dTmpEncoded[p + 1:]
    dictTmp = {
        "REQUEST": "doQuery",
        "LANG": "ADQL",
        "FORMAT": "votable",
        "tapclient": str(TAP_CLIENT_ID),
        "PHASE": "RUN",
        "QUERY": str(q)
    }
    sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
    jobRequest = f"sync?{sortedKey}"
    connHandler.set_response(jobRequest, responseLaunchJob)

    with pytest.raises(Exception):
        tap.launch_job(query)

    responseLaunchJob.set_status_code(200)
    responseLaunchJob.set_message("OK")
    job = tap.launch_job(query)

    assert job is not None
    assert job.async_ is False
    assert job.get_phase() == 'COMPLETED'
    assert job.failed is False

    # results
    results = job.get_results()
    assert len(results) == 3
    __check_results_column(results, 'ra', 'ra', None, np.float64)
    __check_results_column(results, 'dec', 'dec', None, np.float64)
    __check_results_column(results, 'source_id', 'source_id', None, object)
    __check_results_column(results, 'table1_oid', 'table1_oid', None, np.int32)
Exemple #19
0
    def test_load_table(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)

        # No arguments
        with pytest.raises(Exception):
            tap.load_table()

        responseLoadTable = DummyResponse()
        responseLoadTable.set_status_code(500)
        responseLoadTable.set_message("ERROR")
        tableDataFile = data_path('test_table1.xml')
        tableData = utils.read_file_content(tableDataFile)
        responseLoadTable.set_data(method='GET',
                                   context=None,
                                   body=tableData,
                                   headers=None)
        tableSchema = "public"
        tableName = "table1"
        fullQualifiedTableName = tableSchema + "." + tableName
        tableRequest = "tables?tables=" + fullQualifiedTableName
        connHandler.set_response(tableRequest, responseLoadTable)

        with pytest.raises(Exception):
            tap.load_table(fullQualifiedTableName)

        responseLoadTable.set_status_code(200)
        responseLoadTable.set_message("OK")
        table = tap.load_table(fullQualifiedTableName)
        assert table is not None, \
            "Table '%s' not found" % (fullQualifiedTableName)
        assert table.description == 'Table1 desc', \
            "Wrong description for table1. Expected: %s, found %s" % \
            ('Table1 desc', table.description)
        columns = table.columns
        assert len(columns) == 2, \
            "Number of columns for table1. Expected: %d, found: %d" % \
            (2, len(columns))
        col = self.__find_column('table1_col1', columns)
        self.__check_column(col, 'Table1 Column1 desc', '', 'VARCHAR',
                            'indexed')
        col = self.__find_column('table1_col2', columns)
        self.__check_column(col, 'Table1 Column2 desc', '', 'INTEGER', None)
Exemple #20
0
 def test_data(self):
     connHandler = DummyConnHandler()
     tap = TapPlus("http://test:1111/tap",
                   data_context="data",
                   connhandler=connHandler)
     responseResultsJob = DummyResponse()
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseResultsJob.set_data(method='GET',
                                 context=None,
                                 body=jobData,
                                 headers=None)
     req = "?ID=1%2C2&format=votable"
     connHandler.set_response(req, responseResultsJob)
     req = "?ID=1%2C2"
     connHandler.set_response(req, responseResultsJob)
     # error
     responseResultsJob.set_status_code(500)
     responseResultsJob.set_message("ERROR")
     params_dict = {}
     params_dict['ID'] = "1,2"
     with pytest.raises(Exception):
         tap.load_data(params_dict)
     # OK
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     # results
     results = tap.load_data(params_dict)
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
     # error: no params dictionary
     with pytest.raises(Exception):
         # no dictionary: exception
         tap.load_data("1,2")
     params_dict['format'] = "votable"
     results = tap.load_data(params_dict)
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
Exemple #21
0
def test_rename_table():
    tableName = 'user_test.table_test_rename'
    newTableName = 'user_test.table_test_rename_new'
    newColumnNames = {'ra': 'alpha', 'dec': 'delta'}
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    dummyResponse = DummyResponse()
    dummyResponse.set_status_code(200)
    dummyResponse.set_message("OK")
    tableDataFile = data_path('test_table_rename.xml')
    tableData = utils.read_file_content(tableDataFile)
    dummyResponse.set_data(method='GET',
                           context=None,
                           body=tableData,
                           headers=None)

    with pytest.raises(Exception):
        tap.rename_table()
    with pytest.raises(Exception):
        tap.rename_table(table_name=tableName)
    with pytest.raises(Exception):
        tap.rename_table(table_name=tableName,
                         new_table_name=None,
                         new_column_names_dict=None)

    # Test OK.
    responseRenameTable = DummyResponse()
    responseRenameTable.set_status_code(200)
    responseRenameTable.set_message("OK")
    dictArgs = {
        "action": "rename",
        "new_column_names": "ra:alpha,dec:delta",
        "new_table_name": newTableName,
        "table_name": tableName,
    }
    data = connHandler.url_encode(dictArgs)
    req = f"TableTool?{data}"
    connHandler.set_response(req, responseRenameTable)
    tap.rename_table(table_name=tableName,
                     new_table_name=newTableName,
                     new_column_names_dict=newColumnNames)
Exemple #22
0
    def test_load_table(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        responseLoadTable = DummyResponse()
        responseLoadTable.set_status_code(500)
        responseLoadTable.set_message("ERROR")
        tableDataFile = data_path('test_table1.xml')
        tableData = utils.read_file_content(tableDataFile)
        responseLoadTable.set_data(method='GET',
                                   context=None,
                                   body=tableData,
                                   headers=None)
        tableSchema = "public"
        tableName = "table1"
        fullQualifiedTableName = tableSchema + "." + tableName
        tableRequest = "tables?tables=" + fullQualifiedTableName
        connHandler.set_response(tableRequest, responseLoadTable)

        with pytest.raises(Exception):
            tap.load_table(fullQualifiedTableName)

        responseLoadTable.set_status_code(200)
        responseLoadTable.set_message("OK")
        table = tap.load_table(fullQualifiedTableName)
        assert table is not None, \
            "Table '%s' not found" % (fullQualifiedTableName)
        assert table.get_description() == 'Table1 desc', \
            "Wrong description for table1. Expected: %s, found %s" % \
            ('Table1 desc', table.get_description())
        columns = table.get_columns()
        assert len(columns) == 2, \
            "Number of columns for table1. Expected: %d, found: %d" % \
            (2, len(columns))
        col = self.__find_column('table1_col1', columns)
        self.__check_column(col, 'Table1 Column1 desc', '', 'VARCHAR', 'indexed')
        col = self.__find_column('table1_col2', columns)
        self.__check_column(col, 'Table1 Column2 desc', '', 'INTEGER', None)
Exemple #23
0
def test_load_table():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)

    # No arguments
    with pytest.raises(Exception):
        tap.load_table()

    responseLoadTable = DummyResponse()
    responseLoadTable.set_status_code(500)
    responseLoadTable.set_message("ERROR")
    tableDataFile = data_path('test_table1.xml')
    tableData = utils.read_file_content(tableDataFile)
    responseLoadTable.set_data(method='GET',
                               context=None,
                               body=tableData,
                               headers=None)
    tableSchema = "public"
    tableName = "table1"
    fullQualifiedTableName = f"{tableSchema}.{tableName}"
    tableRequest = f"tables?tables={fullQualifiedTableName}"
    connHandler.set_response(tableRequest, responseLoadTable)

    with pytest.raises(Exception):
        tap.load_table(fullQualifiedTableName)

    responseLoadTable.set_status_code(200)
    responseLoadTable.set_message("OK")
    table = tap.load_table(fullQualifiedTableName)
    assert table is not None
    assert table.description == 'Table1 desc'
    columns = table.columns
    assert len(columns) == 2
    col = __find_column('table1_col1', columns)
    __check_column(col, 'Table1 Column1 desc', '', 'VARCHAR', 'indexed')
    col = __find_column('table1_col2', columns)
    __check_column(col, 'Table1 Column2 desc', '', 'INTEGER', None)
Exemple #24
0
    def test_launch_sync_job(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(500)
        responseLaunchJob.set_message("ERROR")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        query = 'select top 5 * from table'
        dTmp = {"q": query}
        dTmpEncoded = connHandler.url_encode(dTmp)
        p = dTmpEncoded.find("=")
        q = dTmpEncoded[p+1:]
        dictTmp = {
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable",
            "tapclient": str(TAP_CLIENT_ID),
            "PHASE": "RUN",
            "QUERY": str(q)}
        sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
        jobRequest = "sync?" + sortedKey
        connHandler.set_response(jobRequest, responseLaunchJob)

        with pytest.raises(Exception):
            tap.launch_job(query)

        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        job = tap.launch_job(query)
        assert job is not None, "Expected a valid job"
        assert job.is_sync(), "Expected a synchronous job"
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        assert job.is_failed() is False, "Wrong job status (set Failed = True)"
        # results
        results = job.get_results()
        assert len(results) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(results))
        self.__check_results_column(results,
                                    'alpha',
                                    'alpha',
                                    None,
                                    np.float64)
        self.__check_results_column(results,
                                    'delta',
                                    'delta',
                                    None,
                                    np.float64)
        self.__check_results_column(results,
                                    'source_id',
                                    'source_id',
                                    None,
                                    np.object)
        self.__check_results_column(results,
                                    'table1_oid',
                                    'table1_oid',
                                    None,
                                    np.int32)
 def test_query_object_async(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(connHandler, tapplus)
     jobid = '12345'
     # Launch response
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(303)
     responseLaunchJob.set_message("OK")
     # list of list (httplib implementation for headers in response)
     launchResponseHeaders = [[
         'location', 'http://test:1111/tap/async/' + jobid
     ]]
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=None,
                                headers=launchResponseHeaders)
     connHandler.set_default_response(responseLaunchJob)
     # Phase response
     responsePhase = DummyResponse()
     responsePhase.set_status_code(200)
     responsePhase.set_message("OK")
     responsePhase.set_data(method='GET',
                            context=None,
                            body="COMPLETED",
                            headers=None)
     req = "async/" + jobid + "/phase"
     connHandler.set_response(req, responsePhase)
     # Results response
     responseResultsJob = DummyResponse()
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseResultsJob.set_data(method='GET',
                                 context=None,
                                 body=jobData,
                                 headers=None)
     req = "async/" + jobid + "/results/result"
     connHandler.set_response(req, responseResultsJob)
     sc = SkyCoord(ra=29.0,
                   dec=15.0,
                   unit=(u.degree, u.degree),
                   frame='icrs')
     width = Quantity(12, u.deg)
     height = Quantity(10, u.deg)
     table = tap.query_object_async(sc, width=width, height=height)
     assert len(table) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(table))
     self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
     self.__check_results_column(table, 'delta', 'delta', None, np.float64)
     self.__check_results_column(table, 'source_id', 'source_id', None,
                                 np.object)
     self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                 np.int32)
     # by radius
     radius = Quantity(1, u.deg)
     table = tap.query_object_async(sc, radius=radius)
     assert len(table) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(table))
     self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
     self.__check_results_column(table, 'delta', 'delta', None, np.float64)
     self.__check_results_column(table, 'source_id', 'source_id', None,
                                 np.object)
     self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                 np.int32)
Exemple #26
0
def test_launch_async_job():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    jobid = '12345'
    # Launch response
    responseLaunchJob = DummyResponse()
    responseLaunchJob.set_status_code(500)
    responseLaunchJob.set_message("ERROR")
    # list of list (httplib implementation for headers in response)
    launchResponseHeaders = [[
        'location', f'http://test:1111/tap/async/{jobid}'
    ]]
    responseLaunchJob.set_data(method='POST',
                               context=None,
                               body=None,
                               headers=launchResponseHeaders)
    query = 'query'
    dictTmp = {
        "REQUEST": "doQuery",
        "LANG": "ADQL",
        "FORMAT": "votable",
        "tapclient": str(TAP_CLIENT_ID),
        "PHASE": "RUN",
        "QUERY": str(query)
    }
    sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
    req = f"async?{sortedKey}"
    connHandler.set_response(req, responseLaunchJob)
    # Phase response
    responsePhase = DummyResponse()
    responsePhase.set_status_code(500)
    responsePhase.set_message("ERROR")
    responsePhase.set_data(method='GET',
                           context=None,
                           body="COMPLETED",
                           headers=None)
    req = f"async/{jobid}/phase"
    connHandler.set_response(req, responsePhase)
    # Results response
    responseResultsJob = DummyResponse()
    responseResultsJob.set_status_code(500)
    responseResultsJob.set_message("ERROR")
    jobDataFile = data_path('job_1.vot')
    jobData = utils.read_file_content(jobDataFile)
    responseResultsJob.set_data(method='GET',
                                context=None,
                                body=jobData,
                                headers=None)
    req = f"async/{jobid}/results/result"
    connHandler.set_response(req, responseResultsJob)

    with pytest.raises(Exception):
        tap.launch_job_async(query)

    responseLaunchJob.set_status_code(303)
    responseLaunchJob.set_message("OK")
    with pytest.raises(Exception):
        tap.launch_job_async(query)

    responsePhase.set_status_code(200)
    responsePhase.set_message("OK")
    with pytest.raises(Exception):
        tap.launch_job_async(query)

    responseResultsJob.set_status_code(200)
    responseResultsJob.set_message("OK")
    job = tap.launch_job_async(query)
    assert job is not None
    assert job.async_ is True
    assert job.get_phase() == 'COMPLETED'
    assert job.failed is False

    # results
    results = job.get_results()
    assert len(results) == 3
    __check_results_column(results, 'alpha', 'alpha', None, np.float64)
    __check_results_column(results, 'delta', 'delta', None, np.float64)
    __check_results_column(results, 'source_id', 'source_id', None, object)
    __check_results_column(results, 'table1_oid', 'table1_oid', None, np.int32)
    def test_xmatch(self):
        connHandler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
        tap = GaiaClass(connHandler, tapplus)
        jobid = '12345'
        # Launch response
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(303)
        responseLaunchJob.set_message("OK")
        # list of list (httplib implementation for headers in response)
        launchResponseHeaders = [[
            'location', 'http://test:1111/tap/async/' + jobid
        ]]
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=None,
                                   headers=launchResponseHeaders)
        connHandler.set_default_response(responseLaunchJob)
        # Phase response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        responsePhase.set_data(method='GET',
                               context=None,
                               body="COMPLETED",
                               headers=None)
        req = "async/" + jobid + "/phase"
        connHandler.set_response(req, responsePhase)
        # Results response
        responseResultsJob = DummyResponse()
        responseResultsJob.set_status_code(200)
        responseResultsJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseResultsJob.set_data(method='GET',
                                    context=None,
                                    body=jobData,
                                    headers=None)
        req = "async/" + jobid + "/results/result"
        connHandler.set_response(req, responseResultsJob)
        query = ("SELECT crossmatch_positional(",
                 "'schemaA','tableA','schemaB','tableB',1.0,'results')",
                 "FROM dual;")
        dTmp = {"q": query}
        dTmpEncoded = connHandler.url_encode(dTmp)
        p = dTmpEncoded.find("=")
        q = dTmpEncoded[p + 1:]
        dictTmp = {
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable",
            "tapclient": str(TAP_CLIENT_ID),
            "PHASE": "RUN",
            "QUERY": str(q)
        }
        sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
        jobRequest = "sync?" + sortedKey
        connHandler.set_response(jobRequest, responseLaunchJob)
        # check parameters
        # missing table A
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a=None,
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name='results')
        assert "Table name A argument is mandatory" in err.value.args[0]
        # missing schema A
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='tableA',
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name='results')
        assert "Not found schema name in full qualified table A: 'tableA'" \
            in err.value.args[0]
        # missing table B
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b=None,
                            results_table_name='results')
        assert "Table name B argument is mandatory" in err.value.args[0]
        # missing schema B
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b='tableB',
                            results_table_name='results')
        assert "Not found schema name in full qualified table B: 'tableB'" \
            in err.value.args[0]
        # missing results table
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name=None)
        assert "Results table name argument is mandatory" in err.value.args[0]
        # wrong results table (with schema)
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name='schema.results')
        assert "Please, do not specify schema for 'results_table_name'" \
            in err.value.args[0]
        # radius < 0.1
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name='results',
                            radius=0.01)
        assert "Invalid radius value. Found 0.01, valid range is: 0.1 to 10.0" \
            in err.value.args[0]
        # radius > 10.0
        with pytest.raises(ValueError) as err:
            tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                            full_qualified_table_name_b='schemaB.tableB',
                            results_table_name='results',
                            radius=10.1)
        assert "Invalid radius value. Found 10.1, valid range is: 0.1 to 10.0" \
            in err.value.args[0]
        # check default parameters
        parameters = {}
        query = "SELECT crossmatch_positional(\
            'schemaA','tableA',\
            'schemaB','tableB',\
            1.0,\
            'results')\
            FROM dual;"

        parameters['query'] = query
        parameters['name'] = 'results'
        parameters['output_file'] = None
        parameters['output_format'] = 'votable'
        parameters['verbose'] = False
        parameters['dump_to_file'] = False
        parameters['background'] = False
        parameters['upload_resource'] = None
        parameters['upload_table_name'] = None
        job = tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                              full_qualified_table_name_b='schemaB.tableB',
                              results_table_name='results')
        assert job.async_ is True, "Expected an asynchronous job"
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        assert job.failed is False, "Wrong job status (set Failed = True)"
        job = tap.cross_match(full_qualified_table_name_a='schemaA.tableA',
                              full_qualified_table_name_b='schemaB.tableB',
                              results_table_name='results',
                              background=True)
        assert job.async_ is True, "Expected an asynchronous job"
        assert job.get_phase() == 'EXECUTING', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('EXECUTING', job.get_phase())
        assert job.failed is False, "Wrong job status (set Failed = True)"
Exemple #28
0
    def test_query_object(self):
        conn_handler = DummyConnHandler()
        # Launch response: we use default response because the query contains
        # decimals
        dummy_response = DummyResponse()
        dummy_response.set_status_code(200)
        dummy_response.set_message("OK")

        message_text = "1653401204784D[type: -100,-1]=Gaia dev is under maintenance"

        dummy_response.set_data(method='GET',
                                context=None,
                                body=message_text,
                                headers=None)
        conn_handler.set_default_response(dummy_response)

        # show_server_messages
        tableRequest = 'notification?action=GetNotifications'
        conn_handler.set_response(tableRequest, dummy_response)

        tapplus = TapPlus("http://test:1111/tap", connhandler=conn_handler)
        tap = GaiaClass(conn_handler, tapplus, show_server_messages=True)
        # Launch response: we use default response because the query contains
        # decimals
        response_launch_job = DummyResponse()
        response_launch_job.set_status_code(200)
        response_launch_job.set_message("OK")
        job_data_file = data_path('job_1.vot')
        job_data = utils.read_file_content(job_data_file)
        response_launch_job.set_data(method='POST',
                                     context=None,
                                     body=job_data,
                                     headers=None)
        # The query contains decimals: force default response
        conn_handler.set_default_response(response_launch_job)
        sc = SkyCoord(ra=29.0,
                      dec=15.0,
                      unit=(u.degree, u.degree),
                      frame='icrs')
        with pytest.raises(ValueError) as err:
            tap.query_object(sc)
        assert "Missing required argument: width" in err.value.args[0]

        width = Quantity(12, u.deg)

        with pytest.raises(ValueError) as err:
            tap.query_object(sc, width=width)
        assert "Missing required argument: height" in err.value.args[0]

        height = Quantity(10, u.deg)
        table = tap.query_object(sc, width=width, height=height)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)
        # by radius
        radius = Quantity(1, u.deg)
        table = tap.query_object(sc, radius=radius)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)
Exemple #29
0
 def test_cone_search_async(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(tapplus)
     jobid = '12345'
     # Launch response
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(303)
     responseLaunchJob.set_message("OK")
     # list of list (httplib implementation for headers in response)
     launchResponseHeaders = [
         ['location', 'http://test:1111/tap/async/' + jobid]
         ]
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=None,
                                headers=launchResponseHeaders)
     ra = 19
     dec = 20
     sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
     radius = Quantity(1.0, u.deg)
     connHandler.set_default_response(responseLaunchJob)
     # Phase response
     responsePhase = DummyResponse()
     responsePhase.set_status_code(200)
     responsePhase.set_message("OK")
     responsePhase.set_data(method='GET',
                            context=None,
                            body="COMPLETED",
                            headers=None)
     req = "async/" + jobid + "/phase"
     connHandler.set_response(req, responsePhase)
     # Results response
     responseResultsJob = DummyResponse()
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseResultsJob.set_data(method='GET',
                                 context=None,
                                 body=jobData,
                                 headers=None)
     req = "async/" + jobid + "/results/result"
     connHandler.set_response(req, responseResultsJob)
     job = tap.cone_search_async(sc, radius)
     assert job is not None, "Expected a valid job"
     assert job.async_ is True, "Expected an asynchronous job"
     assert job.get_phase() == 'COMPLETED', \
         "Wrong job phase. Expected: %s, found %s" % \
         ('COMPLETED', job.get_phase())
     assert job.failed is False, "Wrong job status (set Failed = True)"
     # results
     results = job.get_results()
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
     self.__check_results_column(results,
                                 'alpha',
                                 'alpha',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'delta',
                                 'delta',
                                 None,
                                 np.float64)
     self.__check_results_column(results,
                                 'source_id',
                                 'source_id',
                                 None,
                                 np.object)
     self.__check_results_column(results,
                                 'table1_oid',
                                 'table1_oid',
                                 None,
                                 np.int32)
Exemple #30
0
    def test_start_job(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        jobid = '12345'
        # Phase POST response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        responsePhase.set_data(method='POST',
                               context=None,
                               body=None,
                               headers=None)
        req = "async/" + jobid + "/phase?PHASE=RUN"
        connHandler.set_response(req, responsePhase)
        # Launch response
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(303)
        responseLaunchJob.set_message("OK")
        # list of list (httplib implementation for headers in response)
        launchResponseHeaders = [[
            'location', 'http://test:1111/tap/async/' + jobid
        ]]
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=None,
                                   headers=launchResponseHeaders)
        query = 'query'
        dictTmp = {
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable",
            "tapclient": str(TAP_CLIENT_ID),
            "QUERY": str(query)
        }
        sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
        req = "async?" + sortedKey
        connHandler.set_response(req, responseLaunchJob)
        # Phase response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        responsePhase.set_data(method='GET',
                               context=None,
                               body="COMPLETED",
                               headers=None)
        req = "async/" + jobid + "/phase"
        connHandler.set_response(req, responsePhase)
        # Results response
        responseResultsJob = DummyResponse()
        responseResultsJob.set_status_code(200)
        responseResultsJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseResultsJob.set_data(method='GET',
                                    context=None,
                                    body=jobData,
                                    headers=None)
        req = "async/" + jobid + "/results/result"
        connHandler.set_response(req, responseResultsJob)

        responseResultsJob.set_status_code(200)
        responseResultsJob.set_message("OK")
        job = tap.launch_job_async(query, autorun=False)
        assert job is not None, "Expected a valid job"
        assert job.get_phase() == 'PENDING', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('PENDING', job.get_phase())
        # start job
        job.start()
        assert job.get_phase() == 'QUEUED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('QUEUED', job.get_phase())
        # results
        results = job.get_results()
        assert len(results) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(results))
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        # try to start again
        with pytest.raises(Exception):
            job.start()
Exemple #31
0
def test_job_parameters():
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    jobid = '12345'
    # Launch response
    responseLaunchJob = DummyResponse()
    responseLaunchJob.set_status_code(303)
    responseLaunchJob.set_message("OK")
    # list of list (httplib implementation for headers in response)
    launchResponseHeaders = [[
        'location', f'http://test:1111/tap/async/{jobid}'
    ]]
    responseLaunchJob.set_data(method='POST',
                               context=None,
                               body=None,
                               headers=launchResponseHeaders)
    query = 'query'
    dictTmp = {
        "REQUEST": "doQuery",
        "LANG": "ADQL",
        "FORMAT": "votable",
        "tapclient": str(TAP_CLIENT_ID),
        "QUERY": str(query)
    }
    sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
    req = f"async?{sortedKey}"
    connHandler.set_response(req, responseLaunchJob)
    # Phase response
    responsePhase = DummyResponse()
    responsePhase.set_status_code(200)
    responsePhase.set_message("OK")
    responsePhase.set_data(method='GET',
                           context=None,
                           body="COMPLETED",
                           headers=None)
    req = f"async/{jobid}/phase"
    connHandler.set_response(req, responsePhase)
    # Results response
    responseResultsJob = DummyResponse()
    responseResultsJob.set_status_code(200)
    responseResultsJob.set_message("OK")
    jobDataFile = data_path('job_1.vot')
    jobData = utils.read_file_content(jobDataFile)
    responseResultsJob.set_data(method='GET',
                                context=None,
                                body=jobData,
                                headers=None)
    req = f"async/{jobid}/results/result"
    connHandler.set_response(req, responseResultsJob)

    responseResultsJob.set_status_code(200)
    responseResultsJob.set_message("OK")
    job = tap.launch_job_async(query, autorun=False)
    assert job is not None
    assert job.get_phase() == 'PENDING'

    # parameter response
    responseParameters = DummyResponse()
    responseParameters.set_status_code(200)
    responseParameters.set_message("OK")
    responseParameters.set_data(method='GET',
                                context=None,
                                body=None,
                                headers=None)
    req = f"async/{jobid}?param1=value1"
    connHandler.set_response(req, responseParameters)
    # Phase POST response
    responsePhase = DummyResponse()
    responsePhase.set_status_code(200)
    responsePhase.set_message("OK")
    responsePhase.set_data(method='POST',
                           context=None,
                           body=None,
                           headers=None)
    req = f"async/{jobid}/phase?PHASE=RUN"
    connHandler.set_response(req, responsePhase)

    # send parameter OK
    job.send_parameter("param1", "value1")
    # start job
    job.start()
    assert job.get_phase() == 'QUEUED'
    # try to send a parameter after execution
    with pytest.raises(Exception):
        job.send_parameter("param2", "value2")
Exemple #32
0
    def test_query_object(self):
        connHandler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
        tap = GaiaClass(tapplus)
        # Launch response: we use default response because the query contains decimals
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        # The query contains decimals: force default response
        connHandler.set_default_response(responseLaunchJob)
        sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree), frame='icrs')
        with pytest.raises(ValueError) as err:
            tap.query_object(sc)
        assert "Missing required argument: 'width'" in err.value.args[0]

        width = Quantity(12, u.deg)

        with pytest.raises(ValueError) as err:
            tap.query_object(sc, width=width)
        assert "Missing required argument: 'height'" in err.value.args[0]

        height = Quantity(10, u.deg)
        table = tap.query_object(sc, width=width, height=height)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table,
                                    'alpha',
                                    'alpha',
                                    None,
                                    np.float64)
        self.__check_results_column(table,
                                    'delta',
                                    'delta',
                                    None,
                                    np.float64)
        self.__check_results_column(table,
                                    'source_id',
                                    'source_id',
                                    None,
                                    np.object)
        self.__check_results_column(table,
                                    'table1_oid',
                                    'table1_oid',
                                    None,
                                    np.int32)
        # by radius
        radius = Quantity(1, u.deg)
        table = tap.query_object(sc, radius=radius)
        assert len(table) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(table))
        self.__check_results_column(table,
                                    'alpha',
                                    'alpha',
                                    None,
                                    np.float64)
        self.__check_results_column(table,
                                    'delta',
                                    'delta',
                                    None,
                                    np.float64)
        self.__check_results_column(table,
                                    'source_id',
                                    'source_id',
                                    None,
                                    np.object)
        self.__check_results_column(table,
                                    'table1_oid',
                                    'table1_oid',
                                    None,
                                    np.int32)
Exemple #33
0
    def test_cone_search_sync(self):
        connHandler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
        tap = JwstClass(tap_plus_handler=tapplus, show_messages=False)
        # Launch response: we use default response because the
        # query contains decimals
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        ra = 19.0
        dec = 20.0
        sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
        radius = Quantity(1.0, u.deg)
        connHandler.set_default_response(responseLaunchJob)
        job = tap.cone_search(sc, radius)
        assert job is not None, "Expected a valid job"
        assert job.async_ is False, "Expected a synchronous job"
        assert job.get_phase(
        ) == 'COMPLETED', f"Wrong job phase. Expected: {'COMPLETED'}, found {job.get_phase()}"
        assert job.failed is False, "Wrong job status (set Failed = True)"
        # results
        results = job.get_results()
        assert len(
            results
        ) == 3, f"Wrong job results (num rows). Expected: {3}, found {len(results)}"
        self.__check_results_column(results, 'alpha', 'alpha', None,
                                    np.float64)
        self.__check_results_column(results, 'delta', 'delta', None,
                                    np.float64)
        self.__check_results_column(results, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(results, 'table1_oid', 'table1_oid', None,
                                    np.int32)

        # Test observation_id argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, observation_id=1)
        assert "observation_id must be string" in err.value.args[0]

        # Test cal_level argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, cal_level='a')
        assert "cal_level must be either 'Top' or an integer" in err.value.args[
            0]

        # Test only_public
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, only_public='a')
        assert "only_public must be boolean" in err.value.args[0]

        # Test dataproduct_type argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, prod_type=1)
        assert "prod_type must be string" in err.value.args[0]

        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, prod_type='a')
        assert "prod_type must be one of: " in err.value.args[0]

        # Test instrument_name argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, instrument_name=1)
        assert "instrument_name must be string" in err.value.args[0]

        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, instrument_name='a')
        assert "instrument_name must be one of: " in err.value.args[0]

        # Test filter_name argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, filter_name=1)
        assert "filter_name must be string" in err.value.args[0]

        # Test proposal_id argument
        with pytest.raises(ValueError) as err:
            tap.cone_search(sc, radius, proposal_id=123)
        assert "proposal_id must be string" in err.value.args[0]
Exemple #34
0
    def test_launc_async_job(self):
        connHandler = DummyConnHandler()
        tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
        jobid = '12345'
        # Launch response
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(500)
        responseLaunchJob.set_message("ERROR")
        # list of list (httplib implementation for headers in response)
        launchResponseHeaders = [
            ['location', 'http://test:1111/tap/async/' + jobid]
            ]
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=None,
                                   headers=launchResponseHeaders)
        query = 'query'
        dictTmp = {
            "REQUEST": "doQuery",
            "LANG": "ADQL",
            "FORMAT": "votable",
            "tapclient": str(TAP_CLIENT_ID),
            "PHASE": "RUN",
            "QUERY": str(query)}
        sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
        req = "async?" + sortedKey
        connHandler.set_response(req, responseLaunchJob)
        # Phase response
        responsePhase = DummyResponse()
        responsePhase.set_status_code(500)
        responsePhase.set_message("ERROR")
        responsePhase.set_data(method='GET',
                               context=None,
                               body="COMPLETED",
                               headers=None)
        req = "async/" + jobid + "/phase"
        connHandler.set_response(req, responsePhase)
        # Results response
        responseResultsJob = DummyResponse()
        responseResultsJob.set_status_code(500)
        responseResultsJob.set_message("ERROR")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseResultsJob.set_data(method='GET',
                                    context=None,
                                    body=jobData,
                                    headers=None)
        req = "async/" + jobid + "/results/result"
        connHandler.set_response(req, responseResultsJob)

        with pytest.raises(Exception):
            tap.launch_job_async(query)

        responseLaunchJob.set_status_code(303)
        responseLaunchJob.set_message("OK")
        with pytest.raises(Exception):
            tap.launch_job_async(query)

        responsePhase.set_status_code(200)
        responsePhase.set_message("OK")
        with pytest.raises(Exception):
            tap.launch_job_async(query)

        responseResultsJob.set_status_code(200)
        responseResultsJob.set_message("OK")
        job = tap.launch_job_async(query)
        assert job is not None, "Expected a valid job"
        assert job.is_sync() is False, "Expected an asynchronous job"
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        assert job.is_failed() is False, "Wrong job status (set Failed = True)"
        # results
        results = job.get_results()
        assert len(results) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(results))
        self.__check_results_column(results,
                                    'alpha',
                                    'alpha',
                                    None,
                                    np.float64)
        self.__check_results_column(results,
                                    'delta',
                                    'delta',
                                    None,
                                    np.float64)
        self.__check_results_column(results,
                                    'source_id',
                                    'source_id',
                                    None,
                                    np.object)
        self.__check_results_column(results,
                                    'table1_oid',
                                    'table1_oid',
                                    None,
                                    np.int32)
 def test_cone_search_async(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(connHandler, tapplus)
     jobid = '12345'
     # Launch response
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(303)
     responseLaunchJob.set_message("OK")
     # list of list (httplib implementation for headers in response)
     launchResponseHeaders = [[
         'location', 'http://test:1111/tap/async/' + jobid
     ]]
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=None,
                                headers=launchResponseHeaders)
     ra = 19
     dec = 20
     sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
     radius = Quantity(1.0, u.deg)
     connHandler.set_default_response(responseLaunchJob)
     # Phase response
     responsePhase = DummyResponse()
     responsePhase.set_status_code(200)
     responsePhase.set_message("OK")
     responsePhase.set_data(method='GET',
                            context=None,
                            body="COMPLETED",
                            headers=None)
     req = "async/" + jobid + "/phase"
     connHandler.set_response(req, responsePhase)
     # Results response
     responseResultsJob = DummyResponse()
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseResultsJob.set_data(method='GET',
                                 context=None,
                                 body=jobData,
                                 headers=None)
     req = "async/" + jobid + "/results/result"
     connHandler.set_response(req, responseResultsJob)
     job = tap.cone_search_async(sc, radius)
     assert job is not None, "Expected a valid job"
     assert job.async_ is True, "Expected an asynchronous job"
     assert job.get_phase() == 'COMPLETED', \
         "Wrong job phase. Expected: %s, found %s" % \
         ('COMPLETED', job.get_phase())
     assert job.failed is False, "Wrong job status (set Failed = True)"
     # results
     results = job.get_results()
     assert len(results) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(results))
     self.__check_results_column(results, 'alpha', 'alpha', None,
                                 np.float64)
     self.__check_results_column(results, 'delta', 'delta', None,
                                 np.float64)
     self.__check_results_column(results, 'source_id', 'source_id', None,
                                 np.object)
     self.__check_results_column(results, 'table1_oid', 'table1_oid', None,
                                 np.int32)
Exemple #36
0
 def test_query_object_async(self):
     connHandler = DummyConnHandler()
     tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
     tap = GaiaClass(tapplus)
     jobid = '12345'
     # Launch response
     responseLaunchJob = DummyResponse()
     responseLaunchJob.set_status_code(303)
     responseLaunchJob.set_message("OK")
     # list of list (httplib implementation for headers in response)
     launchResponseHeaders = [
         ['location', 'http://test:1111/tap/async/' + jobid]
         ]
     responseLaunchJob.set_data(method='POST',
                                context=None,
                                body=None,
                                headers=launchResponseHeaders)
     connHandler.set_default_response(responseLaunchJob)
     # Phase response
     responsePhase = DummyResponse()
     responsePhase.set_status_code(200)
     responsePhase.set_message("OK")
     responsePhase.set_data(method='GET',
                            context=None,
                            body="COMPLETED",
                            headers=None)
     req = "async/" + jobid + "/phase"
     connHandler.set_response(req, responsePhase)
     # Results response
     responseResultsJob = DummyResponse()
     responseResultsJob.set_status_code(200)
     responseResultsJob.set_message("OK")
     jobDataFile = data_path('job_1.vot')
     jobData = utils.read_file_content(jobDataFile)
     responseResultsJob.set_data(method='GET',
                                 context=None,
                                 body=jobData,
                                 headers=None)
     req = "async/" + jobid + "/results/result"
     connHandler.set_response(req, responseResultsJob)
     sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree), frame='icrs')
     width = Quantity(12, u.deg)
     height = Quantity(10, u.deg)
     table = tap.query_object_async(sc, width=width, height=height)
     assert len(table) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(table))
     self.__check_results_column(table,
                                 'alpha',
                                 'alpha',
                                 None,
                                 np.float64)
     self.__check_results_column(table,
                                 'delta',
                                 'delta',
                                 None,
                                 np.float64)
     self.__check_results_column(table,
                                 'source_id',
                                 'source_id',
                                 None,
                                 np.object)
     self.__check_results_column(table,
                                 'table1_oid',
                                 'table1_oid',
                                 None,
                                 np.int32)
     # by radius
     radius = Quantity(1, u.deg)
     table = tap.query_object_async(sc, radius=radius)
     assert len(table) == 3, \
         "Wrong job results (num rows). Expected: %d, found %d" % \
         (3, len(table))
     self.__check_results_column(table,
                                 'alpha',
                                 'alpha',
                                 None,
                                 np.float64)
     self.__check_results_column(table,
                                 'delta',
                                 'delta',
                                 None,
                                 np.float64)
     self.__check_results_column(table,
                                 'source_id',
                                 'source_id',
                                 None,
                                 np.object)
     self.__check_results_column(table,
                                 'table1_oid',
                                 'table1_oid',
                                 None,
                                 np.int32)
Exemple #37
0
    def test_cone_search_async(self):
        conn_handler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=conn_handler)
        tap = GaiaClass(conn_handler, tapplus, show_server_messages=False)
        jobid = '12345'
        # Launch response
        response_launch_job = DummyResponse()
        response_launch_job.set_status_code(303)
        response_launch_job.set_message("OK")
        # list of list (httplib implementation for headers in response)
        launch_response_headers = [[
            'location', 'http://test:1111/tap/async/' + jobid
        ]]
        response_launch_job.set_data(method='POST',
                                     context=None,
                                     body=None,
                                     headers=launch_response_headers)
        ra = 19
        dec = 20
        sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
        radius = Quantity(1.0, u.deg)
        conn_handler.set_default_response(response_launch_job)
        # Phase response
        response_phase = DummyResponse()
        response_phase.set_status_code(200)
        response_phase.set_message("OK")
        response_phase.set_data(method='GET',
                                context=None,
                                body="COMPLETED",
                                headers=None)
        req = "async/" + jobid + "/phase"
        conn_handler.set_response(req, response_phase)
        # Results response
        response_results_job = DummyResponse()
        response_results_job.set_status_code(200)
        response_results_job.set_message("OK")
        job_data_file = data_path('job_1.vot')
        job_data = utils.read_file_content(job_data_file)
        response_results_job.set_data(method='GET',
                                      context=None,
                                      body=job_data,
                                      headers=None)
        req = "async/" + jobid + "/results/result"
        conn_handler.set_response(req, response_results_job)
        job = tap.cone_search_async(sc, radius)
        assert job is not None, "Expected a valid job"
        assert job.async_ is True, "Expected an asynchronous job"
        assert job.get_phase() == 'COMPLETED', \
            "Wrong job phase. Expected: %s, found %s" % \
            ('COMPLETED', job.get_phase())
        assert job.failed is False, "Wrong job status (set Failed = True)"
        # results
        results = job.get_results()
        assert len(results) == 3, \
            "Wrong job results (num rows). Expected: %d, found %d" % \
            (3, len(results))
        self.__check_results_column(results, 'alpha', 'alpha', None,
                                    np.float64)
        self.__check_results_column(results, 'delta', 'delta', None,
                                    np.float64)
        self.__check_results_column(results, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(results, 'table1_oid', 'table1_oid', None,
                                    np.int32)

        # Regression test for #2093 and #2099 - changing the MAIN_GAIA_TABLE
        # had no effect.
        # The preceding tests should have used the default value.
        assert 'gaiadr2.gaia_source' in job.parameters['query']
        # Test changing the table name through conf.
        conf.MAIN_GAIA_TABLE = 'name_from_conf'
        job = tap.cone_search_async(sc, radius)
        assert 'name_from_conf' in job.parameters['query']
        # Changing the value through the class should overrule conf.
        tap.MAIN_GAIA_TABLE = 'name_from_class'
        job = tap.cone_search_async(sc, radius)
        assert 'name_from_class' in job.parameters['query']
        # Cleanup.
        conf.reset('MAIN_GAIA_TABLE')
Exemple #38
0
def test_update_user_table():
    tableName = 'table'
    connHandler = DummyConnHandler()
    tap = TapPlus("http://test:1111/tap", connhandler=connHandler)
    dummyResponse = DummyResponse()
    dummyResponse.set_status_code(200)
    dummyResponse.set_message("OK")
    tableDataFile = data_path('test_table_update.xml')
    tableData = utils.read_file_content(tableDataFile)
    dummyResponse.set_data(method='GET',
                           context=None,
                           body=tableData,
                           headers=None)
    tableRequest = f"tables?tables={tableName}"
    connHandler.set_response(tableRequest, dummyResponse)

    with pytest.raises(Exception):
        tap.update_user_table()
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName)
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName, list_of_changes=[])
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName, list_of_changes=[[]])
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName,
                              list_of_changes=[['', '', '']])

    # Test Ra & Dec are provided
    list_of_changes = [['alpha', 'flags', 'Ra']]
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName,
                              list_of_changes=list_of_changes)
    list_of_changes = [['delta', 'flags', 'Dec']]
    with pytest.raises(Exception):
        tap.update_user_table(table_name=tableName,
                              list_of_changes=list_of_changes)

    # OK
    responseEditTable = DummyResponse()
    responseEditTable.set_status_code(200)
    responseEditTable.set_message("OK")
    dictTmp = {
        "ACTION": "edit",
        "NUMTABLES": "1",
        "TABLE0": tableName,
        "TABLE0_COL0": "table6_oid",
        "TABLE0_COL0_FLAGS": "PK",
        "TABLE0_COL0_INDEXED": "True",
        "TABLE0_COL0_UCD": "",
        "TABLE0_COL0_UTYPE": "",
        "TABLE0_COL1": "source_id",
        "TABLE0_COL1_FLAGS": "None",
        "TABLE0_COL1_INDEXED": "False",
        "TABLE0_COL1_UCD": "None",
        "TABLE0_COL1_UTYPE": "None",
        "TABLE0_COL2": "alpha",
        "TABLE0_COL2_FLAGS": "Ra",
        "TABLE0_COL2_INDEXED": "False",
        "TABLE0_COL2_UCD": "",
        "TABLE0_COL2_UTYPE": "",
        "TABLE0_COL3": "delta",
        "TABLE0_COL3_FLAGS": "Dec",
        "TABLE0_COL3_INDEXED": "False",
        "TABLE0_COL3_UCD": "",
        "TABLE0_COL3_UTYPE": "",
        "TABLE0_NUMCOLS": "4"
    }
    sortedKey = taputils.taputil_create_sorted_dict_key(dictTmp)
    req = f"tableEdit?{sortedKey}"
    connHandler.set_response(req, responseEditTable)

    list_of_changes = [['alpha', 'flags', 'Ra'], ['delta', 'flags', 'Dec']]
    tap.update_user_table(table_name=tableName,
                          list_of_changes=list_of_changes)
Exemple #39
0
    def test_query_region(self):
        connHandler = DummyConnHandler()
        tapplus = TapPlus("http://test:1111/tap", connhandler=connHandler)
        tap = JwstClass(tap_plus_handler=tapplus, show_messages=False)

        # Launch response: we use default response because the
        # query contains decimals
        responseLaunchJob = DummyResponse()
        responseLaunchJob.set_status_code(200)
        responseLaunchJob.set_message("OK")
        jobDataFile = data_path('job_1.vot')
        jobData = utils.read_file_content(jobDataFile)
        responseLaunchJob.set_data(method='POST',
                                   context=None,
                                   body=jobData,
                                   headers=None)
        # The query contains decimals: force default response
        connHandler.set_default_response(responseLaunchJob)
        sc = SkyCoord(ra=29.0,
                      dec=15.0,
                      unit=(u.degree, u.degree),
                      frame='icrs')
        with pytest.raises(ValueError) as err:
            tap.query_region(sc)
        assert "Missing required argument: 'width'" in err.value.args[0]

        width = Quantity(12, u.deg)
        height = Quantity(10, u.deg)

        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width)
        assert "Missing required argument: 'height'" in err.value.args[0]

        assert (isinstance(tap.query_region(sc, width=width, height=height),
                           Table))

        # Test observation_id argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, observation_id=1)
        assert "observation_id must be string" in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc,
                             width=width,
                             height=height,
                             observation_id="observation"), Table))
        # raise ValueError

        # Test cal_level argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, cal_level='a')
        assert "cal_level must be either 'Top' or an integer" in err.value.args[
            0]

        assert (isinstance(
            tap.query_region(sc, width=width, height=height, cal_level='Top'),
            Table))
        assert (isinstance(
            tap.query_region(sc, width=width, height=height, cal_level=1),
            Table))

        # Test only_public
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, only_public='a')
        assert "only_public must be boolean" in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc, width=width, height=height, only_public=True),
            Table))

        # Test dataproduct_type argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, prod_type=1)
        assert "prod_type must be string" in err.value.args[0]

        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, prod_type='a')
        assert "prod_type must be one of: " in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc, width=width, height=height,
                             prod_type='image'), Table))

        # Test instrument_name argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, instrument_name=1)
        assert "instrument_name must be string" in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc,
                             width=width,
                             height=height,
                             instrument_name='NIRCAM'), Table))

        with pytest.raises(ValueError) as err:
            tap.query_region(sc,
                             width=width,
                             height=height,
                             instrument_name='a')
        assert "instrument_name must be one of: " in err.value.args[0]

        # Test filter_name argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, filter_name=1)
        assert "filter_name must be string" in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc,
                             width=width,
                             height=height,
                             filter_name='filter'), Table))

        # Test proposal_id argument
        with pytest.raises(ValueError) as err:
            tap.query_region(sc, width=width, height=height, proposal_id=123)
        assert "proposal_id must be string" in err.value.args[0]

        assert (isinstance(
            tap.query_region(sc, width=width, height=height,
                             proposal_id='123'), Table))

        table = tap.query_region(sc, width=width, height=height)
        assert len(
            table
        ) == 3, f"Wrong job results (num rows). Expected: {3}, found {len(table)}"
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)
        # by radius
        radius = Quantity(1, u.deg)
        table = tap.query_region(sc, radius=radius)
        assert len(
            table
        ) == 3, f"Wrong job results (num rows). Expected: {3}, found {len(table)}"
        self.__check_results_column(table, 'alpha', 'alpha', None, np.float64)
        self.__check_results_column(table, 'delta', 'delta', None, np.float64)
        self.__check_results_column(table, 'source_id', 'source_id', None,
                                    object)
        self.__check_results_column(table, 'table1_oid', 'table1_oid', None,
                                    np.int32)