Skip to content

Commit 040f676

Browse files
committed
Simplify setting DummyResponse data in TAP+ tests
`DummyResponse` is used in `esa/jwst` and `gaia` tests for mimicking server responses. Now the response data can be specified by only setting the parameters that have non-default values.
1 parent 7b2310c commit 040f676

File tree

6 files changed

+51
-198
lines changed

6 files changed

+51
-198
lines changed

astroquery/esa/jwst/tests/test_jwsttap.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@ def test_query_region(self):
236236
responseLaunchJob = DummyResponse(200)
237237
jobDataFile = data_path('job_1.vot')
238238
jobData = utils.read_file_content(jobDataFile)
239-
responseLaunchJob.set_data(method='POST',
240-
context=None,
241-
body=jobData,
242-
headers=None)
239+
responseLaunchJob.set_data(method='POST', body=jobData)
243240
# The query contains decimals: force default response
244241
connHandler.set_default_response(responseLaunchJob)
245242
sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree),
@@ -373,27 +370,18 @@ def test_query_region_async(self):
373370
responseLaunchJob = DummyResponse(303)
374371
# list of list (httplib implementation for headers in response)
375372
launchResponseHeaders = [['location', 'http://test:1111/tap/async/' + jobid]]
376-
responseLaunchJob.set_data(method='POST',
377-
context=None,
378-
body=None,
379-
headers=launchResponseHeaders)
373+
responseLaunchJob.set_data(method='POST', headers=launchResponseHeaders)
380374
connHandler.set_default_response(responseLaunchJob)
381375
# Phase response
382376
responsePhase = DummyResponse(200)
383-
responsePhase.set_data(method='GET',
384-
context=None,
385-
body="COMPLETED",
386-
headers=None)
377+
responsePhase.set_data(method='GET', body="COMPLETED")
387378
req = "async/" + jobid + "/phase"
388379
connHandler.set_response(req, responsePhase)
389380
# Results response
390381
responseResultsJob = DummyResponse(200)
391382
jobDataFile = data_path('job_1.vot')
392383
jobData = utils.read_file_content(jobDataFile)
393-
responseResultsJob.set_data(method='GET',
394-
context=None,
395-
body=jobData,
396-
headers=None)
384+
responseResultsJob.set_data(method='GET', body=jobData)
397385
req = "async/" + jobid + "/results/result"
398386
connHandler.set_response(req, responseResultsJob)
399387
sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree),
@@ -456,10 +444,7 @@ def test_cone_search_sync(self):
456444
responseLaunchJob = DummyResponse(200)
457445
jobDataFile = data_path('job_1.vot')
458446
jobData = utils.read_file_content(jobDataFile)
459-
responseLaunchJob.set_data(method='POST',
460-
context=None,
461-
body=jobData,
462-
headers=None)
447+
responseLaunchJob.set_data(method='POST', body=jobData)
463448
ra = 19.0
464449
dec = 20.0
465450
sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
@@ -546,31 +531,22 @@ def test_cone_search_async(self):
546531
responseLaunchJob = DummyResponse(303)
547532
# list of list (httplib implementation for headers in response)
548533
launchResponseHeaders = [['location', 'http://test:1111/tap/async/' + jobid]]
549-
responseLaunchJob.set_data(method='POST',
550-
context=None,
551-
body=None,
552-
headers=launchResponseHeaders)
534+
responseLaunchJob.set_data(method='POST', headers=launchResponseHeaders)
553535
ra = 19
554536
dec = 20
555537
sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
556538
radius = Quantity(1.0, u.deg)
557539
connHandler.set_default_response(responseLaunchJob)
558540
# Phase response
559541
responsePhase = DummyResponse(200)
560-
responsePhase.set_data(method='GET',
561-
context=None,
562-
body="COMPLETED",
563-
headers=None)
542+
responsePhase.set_data(method='GET', body="COMPLETED")
564543
req = "async/" + jobid + "/phase"
565544
connHandler.set_response(req, responsePhase)
566545
# Results response
567546
responseResultsJob = DummyResponse(200)
568547
jobDataFile = data_path('job_1.vot')
569548
jobData = utils.read_file_content(jobDataFile)
570-
responseResultsJob.set_data(method='GET',
571-
context=None,
572-
body=jobData,
573-
headers=None)
549+
responseResultsJob.set_data(method='GET', body=jobData)
574550
req = "async/" + jobid + "/results/result"
575551
connHandler.set_response(req, responseResultsJob)
576552
job = tap.cone_search(sc, radius, async_job=True)

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ def test_show_message(self):
4848

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

51-
dummy_response.set_data(method='GET',
52-
context=None,
53-
body=message_text,
54-
headers=None)
51+
dummy_response.set_data(method='GET', body=message_text)
5552
connHandler.set_default_response(dummy_response)
5653

5754
# show_server_messages
@@ -69,10 +66,7 @@ def test_query_object(self):
6966

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

72-
dummy_response.set_data(method='GET',
73-
context=None,
74-
body=message_text,
75-
headers=None)
69+
dummy_response.set_data(method='GET', body=message_text)
7670
conn_handler.set_default_response(dummy_response)
7771

7872
# show_server_messages
@@ -86,10 +80,7 @@ def test_query_object(self):
8680
response_launch_job = DummyResponse(200)
8781
job_data_file = data_path('job_1.vot')
8882
job_data = utils.read_file_content(job_data_file)
89-
response_launch_job.set_data(method='POST',
90-
context=None,
91-
body=job_data,
92-
headers=None)
83+
response_launch_job.set_data(method='POST', body=job_data)
9384
# The query contains decimals: force default response
9485
conn_handler.set_default_response(response_launch_job)
9586
sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree),
@@ -167,27 +158,18 @@ def test_query_object_async(self):
167158
launch_response_headers = [
168159
['location', 'http://test:1111/tap/async/' + jobid]
169160
]
170-
response_launch_job.set_data(method='POST',
171-
context=None,
172-
body=None,
173-
headers=launch_response_headers)
161+
response_launch_job.set_data(method='POST', headers=launch_response_headers)
174162
conn_handler.set_default_response(response_launch_job)
175163
# Phase response
176164
response_phase = DummyResponse(200)
177-
response_phase.set_data(method='GET',
178-
context=None,
179-
body="COMPLETED",
180-
headers=None)
165+
response_phase.set_data(method='GET', body="COMPLETED")
181166
req = "async/" + jobid + "/phase"
182167
conn_handler.set_response(req, response_phase)
183168
# Results response
184169
response_results_job = DummyResponse(200)
185170
job_data_file = data_path('job_1.vot')
186171
job_data = utils.read_file_content(job_data_file)
187-
response_results_job.set_data(method='GET',
188-
context=None,
189-
body=job_data,
190-
headers=None)
172+
response_results_job.set_data(method='GET', body=job_data)
191173
req = "async/" + jobid + "/results/result"
192174
conn_handler.set_response(req, response_results_job)
193175
sc = SkyCoord(ra=29.0, dec=15.0, unit=(u.degree, u.degree),
@@ -254,10 +236,7 @@ def test_cone_search_sync(self):
254236
response_launch_job = DummyResponse(200)
255237
job_data_file = data_path('job_1.vot')
256238
job_data = utils.read_file_content(job_data_file)
257-
response_launch_job.set_data(method='POST',
258-
context=None,
259-
body=job_data,
260-
headers=None)
239+
response_launch_job.set_data(method='POST', body=job_data)
261240
ra = 19.0
262241
dec = 20.0
263242
sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
@@ -307,31 +286,22 @@ def test_cone_search_async(self):
307286
launch_response_headers = [
308287
['location', 'http://test:1111/tap/async/' + jobid]
309288
]
310-
response_launch_job.set_data(method='POST',
311-
context=None,
312-
body=None,
313-
headers=launch_response_headers)
289+
response_launch_job.set_data(method='POST', headers=launch_response_headers)
314290
ra = 19
315291
dec = 20
316292
sc = SkyCoord(ra=ra, dec=dec, unit=(u.degree, u.degree), frame='icrs')
317293
radius = Quantity(1.0, u.deg)
318294
conn_handler.set_default_response(response_launch_job)
319295
# Phase response
320296
response_phase = DummyResponse(200)
321-
response_phase.set_data(method='GET',
322-
context=None,
323-
body="COMPLETED",
324-
headers=None)
297+
response_phase.set_data(method='GET', body="COMPLETED")
325298
req = "async/" + jobid + "/phase"
326299
conn_handler.set_response(req, response_phase)
327300
# Results response
328301
response_results_job = DummyResponse(200)
329302
job_data_file = data_path('job_1.vot')
330303
job_data = utils.read_file_content(job_data_file)
331-
response_results_job.set_data(method='GET',
332-
context=None,
333-
body=job_data,
334-
headers=None)
304+
response_results_job.set_data(method='GET', body=job_data)
335305
req = "async/" + jobid + "/results/result"
336306
conn_handler.set_response(req, response_results_job)
337307
job = tap.cone_search_async(sc, radius)
@@ -459,27 +429,18 @@ def test_xmatch(self):
459429
launch_response_headers = [
460430
['location', 'http://test:1111/tap/async/' + jobid]
461431
]
462-
response_launch_job.set_data(method='POST',
463-
context=None,
464-
body=None,
465-
headers=launch_response_headers)
432+
response_launch_job.set_data(method='POST', headers=launch_response_headers)
466433
conn_handler.set_default_response(response_launch_job)
467434
# Phase response
468435
response_phase = DummyResponse(200)
469-
response_phase.set_data(method='GET',
470-
context=None,
471-
body="COMPLETED",
472-
headers=None)
436+
response_phase.set_data(method='GET', body="COMPLETED")
473437
req = "async/" + jobid + "/phase"
474438
conn_handler.set_response(req, response_phase)
475439
# Results response
476440
response_results_job = DummyResponse(200)
477441
job_data_file = data_path('job_1.vot')
478442
job_data = utils.read_file_content(job_data_file)
479-
response_results_job.set_data(method='GET',
480-
context=None,
481-
body=job_data,
482-
headers=None)
443+
response_results_job.set_data(method='GET', body=job_data)
483444
req = "async/" + jobid + "/results/result"
484445
conn_handler.set_response(req, response_results_job)
485446
query = ("SELECT crossmatch_positional(",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, name=None):
2929
self.cookie = None
3030
self.ishttps = False
3131

32-
def request(self, method, context, body, headers):
32+
def request(self, method, context=None, body=None, headers=None):
3333
self.response.set_data(method, context, body, headers)
3434

3535
def getresponse(self):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def set_status_code(self, status_code):
3333
self.status = status_code
3434
self.reason = self.STATUS_MESSAGES.get(status_code)
3535

36-
def set_data(self, method, context, body, headers):
36+
def set_data(self, method, context=None, body=None, headers=None):
3737
self.method = method
3838
self.context = context
3939
self.body = body

astroquery/utils/tap/model/tests/test_job.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ def test_job_get_results(capsys, tmpdir):
4242
job.jobid = jobid
4343
job.parameters['format'] = outputFormat
4444
responseCheckPhase = DummyResponse(500)
45-
responseCheckPhase.set_data(method='GET',
46-
context=None,
47-
body='FINISHED',
48-
headers=None)
45+
responseCheckPhase.set_data(method='GET', body='FINISHED')
4946
waitRequest = f"async/{jobid}/phase"
5047
connHandler = DummyConnHandler()
5148
connHandler.set_response(waitRequest, responseCheckPhase)
@@ -58,10 +55,7 @@ def test_job_get_results(capsys, tmpdir):
5855
responseGetData = DummyResponse(500)
5956
jobContentFileName = data_path('result_1.vot')
6057
jobContent = utils.read_file_content(jobContentFileName)
61-
responseGetData.set_data(method='GET',
62-
context=None,
63-
body=jobContent,
64-
headers=None)
58+
responseGetData.set_data(method='GET', body=jobContent)
6559
dataRequest = f"async/{jobid}/results/result"
6660
connHandler.set_response(dataRequest, responseGetData)
6761

0 commit comments

Comments
 (0)