Skip to content

Commit 0e6c08b

Browse files
Jorge Fernandez Hernandezbsipocz
authored andcommitted
EUCLIDPCR-1971 New tests
1 parent 1d8f999 commit 0e6c08b

File tree

9 files changed

+190
-10
lines changed

9 files changed

+190
-10
lines changed

astroquery/utils/tap/conn/tests/DummyConnHandler.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
1515
1616
"""
17-
from astroquery.utils.tap import taputils
18-
1917
import requests
2018

19+
from astroquery.utils.tap import taputils
20+
from astroquery.utils.tap.conn.tapconn import TapConn
21+
2122

2223
class DummyConnHandler:
2324

@@ -158,3 +159,12 @@ def execute_secure(self, subcontext=None, data=None, verbose=False):
158159

159160
def get_host_url(self):
160161
return "my fake object"
162+
163+
def encode_multipart(self, fields, files):
164+
tap = TapConn(ishttps=False, host='host')
165+
return tap.encode_multipart(fields, files)
166+
167+
def execute_upload(self, data,
168+
content_type="application/x-www-form-urlencoded", *,
169+
verbose=False):
170+
return self.defaultResponse
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source_id,ra,dec
2+
3834447128563320320,149.8678677871318,1.12773018116361
3+
3834447162923057280,149.8953864417848,1.1345712682426434
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# %ECSV 1.0
2+
# ---
3+
# delimiter: ','
4+
# datatype:
5+
# -
6+
# name: source_id
7+
# datatype: int64
8+
# description: Unique source identifier (unique within a particular Data Release)
9+
# meta:
10+
# ucd: meta.id
11+
# -
12+
# name: ra
13+
# datatype: float64
14+
# unit: deg
15+
# description: Right ascension
16+
# meta:
17+
# ucd: pos.eq.ra;meta.main
18+
# utype: stc:AstroCoords.Position3D.Value3.C1
19+
# CoosysSystem: ICRS
20+
# CoosysEpoch: J2016.0
21+
# -
22+
# name: dec
23+
# datatype: float64
24+
# unit: deg
25+
# description: Declination
26+
# meta:
27+
# ucd: pos.eq.dec;meta.main
28+
# utype: stc:AstroCoords.Position3D.Value3.C2
29+
# CoosysSystem: ICRS
30+
# CoosysEpoch: J2016.0
31+
# meta:
32+
# name: votable
33+
# QUERY_STATUS: OK
34+
# QUERY: 'SELECT TOP 2 source_id, ra, dec FROM gaiadr3.gaia_source '
35+
# CAPTION: 'How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html'
36+
# CITATION: 'How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html'
37+
# JOBID: 1744351221317O
38+
# RELEASE: Gaia DR3
39+
source_id,ra,dec
40+
3834447128563320320,149.8678677871318,1.12773018116361
41+
3834447162923057280,149.8953864417848,1.1345712682426434
Binary file not shown.

astroquery/utils/tap/tests/data/test_upload_file/1744351221317O-result.json

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<VOTABLE version="1.4" xmlns="http://www.ivoa.net/xml/VOTable/v1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivoa.net/xml/VOTable/v1.3 http://www.ivoa.net/xml/VOTable/votable-1.4.xsd">
3+
<RESOURCE type="results">
4+
<INFO name="QUERY_STATUS" value="OK"/>
5+
6+
<INFO name="QUERY" value="SELECT TOP 20 *
7+
FROM user_jferna01.my_votable_fits "><![CDATA[SELECT TOP 20 *
8+
FROM user_jferna01.my_votable_fits ]]></INFO>
9+
<INFO name="CAPTION" value="How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html"><![CDATA[How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html]]></INFO>
10+
<INFO name="CITATION" value="How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html" ucd="meta.bib"><![CDATA[How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html]]></INFO>
11+
<INFO name="PAGE" value=""/>
12+
<INFO name="PAGE_SIZE" value=""/>
13+
<INFO name="JOBID" value="1744360074103O"><![CDATA[1744360074103O]]></INFO>
14+
<INFO name="JOBNAME" value=""></INFO>
15+
16+
<TABLE>
17+
<FIELD datatype="int" name="my_votable_fits_oid">
18+
<DESCRIPTION>Object Identifier</DESCRIPTION>
19+
</FIELD>
20+
<FIELD datatype="long" name="source_id"/>
21+
<FIELD datatype="double" name="ra" unit="deg"/>
22+
<FIELD datatype="double" name="dec" unit="deg"/>
23+
<DATA>
24+
<BINARY2>
25+
<STREAM encoding='base64'>
26+
AAAAAAE1NrFZAAiGAEBiu8WSql90P/ILLs1s9TAAAAAAAjU2sWEACICAQGK8pwF3
27+
l+w/8ic0M8FVmA==
28+
</STREAM>
29+
</BINARY2>
30+
</DATA>
31+
</TABLE>
32+
</RESOURCE>
33+
</VOTABLE>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<VOTABLE version="1.4" xmlns="http://www.ivoa.net/xml/VOTable/v1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivoa.net/xml/VOTable/v1.3 http://www.ivoa.net/xml/VOTable/votable-1.4.xsd">
3+
<RESOURCE type="results">
4+
<INFO name="QUERY_STATUS" value="OK"/>
5+
6+
<INFO name="QUERY" value="SELECT TOP 20 * FROM user_jferna01.my_votable_fits "><![CDATA[SELECT TOP 20 *
7+
FROM user_jferna01.my_votable_fits ]]></INFO>
8+
<INFO name="CAPTION" value="How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html"><![CDATA[How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html]]></INFO>
9+
<INFO name="CITATION" value="How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html" ucd="meta.bib"><![CDATA[How to cite and acknowledge Gaia: https://gea.esac.esa.int/archive/documentation/credits.html]]></INFO>
10+
<INFO name="PAGE" value=""/>
11+
<INFO name="PAGE_SIZE" value=""/>
12+
<INFO name="JOBID" value="1744360074103O"><![CDATA[1744360074103O]]></INFO>
13+
<INFO name="JOBNAME" value=""/>
14+
15+
<TABLE>
16+
<FIELD datatype="int" name="my_votable_fits_oid">
17+
<DESCRIPTION>Object Identifier</DESCRIPTION>
18+
</FIELD>
19+
<FIELD datatype="long" name="source_id"/>
20+
<FIELD datatype="double" name="ra" unit="deg"/>
21+
<FIELD datatype="double" name="dec" unit="deg"/>
22+
<DATA>
23+
<TABLEDATA>
24+
<TR><TD>1</TD><TD>3834447128563320320</TD><TD>149.8678677871318</TD><TD>1.12773018116361</TD></TR>
25+
<TR><TD>2</TD><TD>3834447162923057280</TD><TD>149.8953864417848</TD><TD>1.1345712682426434</TD></TR>
26+
</TABLEDATA>
27+
</DATA>
28+
29+
</TABLE>
30+
</RESOURCE>
31+
</VOTABLE>

astroquery/utils/tap/tests/setup_package.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
1616
"""
1717

18-
1918
import os
2019

2120

@@ -24,7 +23,14 @@
2423
def get_package_data():
2524
paths = [os.path.join('data', '*.vot'),
2625
os.path.join('data', '*.xml'),
26+
os.path.join('data', '*.csv'),
27+
os.path.join('data', '*.ecsv'),
28+
os.path.join('data', '*.fits'),
2729
os.path.join('data', '*.fits.gz'),
30+
os.path.join('data/test_upload_file', '*.vot'),
31+
os.path.join('data/test_upload_file', '*.csv'),
32+
os.path.join('data/test_upload_file', '*.ecsv'),
33+
os.path.join('data/test_upload_file', '*.fits'),
2834
] # etc, add other extensions
2935
# you can also enlist files individually by names
3036
# finally construct and return a dict for the sub module

astroquery/utils/tap/tests/test_tap.py

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
TAP plus
55
=============
66
7-
@author: Juan Carlos Segovia
8-
9-
107
European Space Astronomy Centre (ESAC)
118
European Space Agency (ESA)
12-
13-
Created on 30 jun. 2016
149
"""
1510
import gzip
11+
import os
12+
1613
from pathlib import Path
1714
from unittest.mock import patch
1815
from urllib.parse import quote_plus, urlencode
1916

2017
import numpy as np
2118
import pytest
19+
from astropy.io.registry import IORegistryError
2220
from astropy.table import Table
21+
from astropy.utils.data import get_pkg_data_filename
22+
2323
from requests import HTTPError
2424

2525
from astroquery.utils.tap import taputils
@@ -37,7 +37,7 @@ def read_file(filename):
3737
return filename.read_text()
3838

3939

40-
TEST_DATA = {f.name: read_file(f) for f in Path(__file__).with_name("data").iterdir()}
40+
TEST_DATA = {f.name: read_file(f) for f in Path(__file__).with_name("data").iterdir() if os.path.isfile(f)}
4141

4242

4343
def test_load_tables():
@@ -938,7 +938,7 @@ def test_logout(mock_logout):
938938
assert (mock_logout.call_count == 2)
939939

940940

941-
def test_upload_table():
941+
def test_upload_table_exception():
942942
conn_handler = DummyConnHandler()
943943
tap = TapPlus(url="http://test:1111/tap", connhandler=conn_handler)
944944
a = [1, 2, 3]
@@ -980,3 +980,59 @@ def test___findCookieInHeader():
980980
result = tap._Tap__findCookieInHeader(headers)
981981

982982
assert (result == "JSESSIONID=E677B51BA5C4837347D1E17D4E36647E")
983+
984+
985+
def test_upload_table():
986+
conn_handler = DummyConnHandler()
987+
tap = TapPlus(url="http://test:1111/tap", connhandler=conn_handler)
988+
989+
jobid = '12345'
990+
dummyResponse = DummyResponse(303)
991+
conn_handler.set_default_response(dummyResponse)
992+
launchResponseHeaders = [
993+
['location', f'http://test:1111/tap/async/{jobid}']
994+
]
995+
dummyResponse.set_data(method='POST', headers=launchResponseHeaders)
996+
997+
package = "astroquery.utils.tap.tests"
998+
999+
table_name = 'my_table'
1000+
file_csv = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result.csv'),
1001+
package=package)
1002+
job = tap.upload_table(upload_resource=file_csv, table_name=table_name)
1003+
1004+
assert (job.jobid == jobid)
1005+
1006+
file_ecsv = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result.ecsv'),
1007+
package=package)
1008+
job = tap.upload_table(upload_resource=file_ecsv, table_name=table_name)
1009+
1010+
assert (job.jobid == jobid)
1011+
1012+
file_fits = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result.fits'),
1013+
package=package)
1014+
job = tap.upload_table(upload_resource=file_fits, table_name=table_name)
1015+
1016+
assert (job.jobid == jobid)
1017+
1018+
file_vot = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result.vot'),
1019+
package=package)
1020+
job = tap.upload_table(upload_resource=file_vot, table_name=table_name)
1021+
1022+
assert (job.jobid == jobid)
1023+
1024+
file_plain_vot = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result_plain.vot'),
1025+
package=package)
1026+
job = tap.upload_table(upload_resource=file_plain_vot, table_name=table_name)
1027+
1028+
assert (job.jobid == jobid)
1029+
1030+
file_json = get_pkg_data_filename(os.path.join("data", 'test_upload_file', '1744351221317O-result.json'),
1031+
package=package)
1032+
with pytest.raises(IORegistryError) as exc_info:
1033+
job = tap.upload_table(upload_resource=file_json, table_name=table_name)
1034+
1035+
assert (
1036+
"Format could not be identified based on the file name or contents, please provide a 'format' "
1037+
"argument." in str(
1038+
exc_info.value))

0 commit comments

Comments
 (0)