Skip to content

Commit 7b2310c

Browse files
committed
Simplify handling DummyResponse in TAP+ tests
`DummyResponse` is used in `esa/jwst` and `gaia` tests for mimicking server responses. Now the `message` attribute of an instance is automatically determined by its `status`, whereas previously it had to be set manually in a separate step. Furthermore, the `status` of a `DummyResponse` can now be set through `__init__()`.
1 parent a750c43 commit 7b2310c

File tree

5 files changed

+57
-176
lines changed

5 files changed

+57
-176
lines changed

astroquery/esa/jwst/tests/test_jwsttap.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ def test_query_region(self):
233233

234234
# Launch response: we use default response because the
235235
# query contains decimals
236-
responseLaunchJob = DummyResponse()
237-
responseLaunchJob.set_status_code(200)
238-
responseLaunchJob.set_message("OK")
236+
responseLaunchJob = DummyResponse(200)
239237
jobDataFile = data_path('job_1.vot')
240238
jobData = utils.read_file_content(jobDataFile)
241239
responseLaunchJob.set_data(method='POST',
@@ -372,9 +370,7 @@ def test_query_region_async(self):
372370
tap = JwstClass(tap_plus_handler=tapplus, show_messages=False)
373371
jobid = '12345'
374372
# Launch response
375-
responseLaunchJob = DummyResponse()
376-
responseLaunchJob.set_status_code(303)
377-
responseLaunchJob.set_message("OK")
373+
responseLaunchJob = DummyResponse(303)
378374
# list of list (httplib implementation for headers in response)
379375
launchResponseHeaders = [['location', 'http://test:1111/tap/async/' + jobid]]
380376
responseLaunchJob.set_data(method='POST',
@@ -383,19 +379,15 @@ def test_query_region_async(self):
383379
headers=launchResponseHeaders)
384380
connHandler.set_default_response(responseLaunchJob)
385381
# Phase response
386-
responsePhase = DummyResponse()
387-
responsePhase.set_status_code(200)
388-
responsePhase.set_message("OK")
382+
responsePhase = DummyResponse(200)
389383
responsePhase.set_data(method='GET',
390384
context=None,
391385
body="COMPLETED",
392386
headers=None)
393387
req = "async/" + jobid + "/phase"
394388
connHandler.set_response(req, responsePhase)
395389
# Results response
396-
responseResultsJob = DummyResponse()
397-
responseResultsJob.set_status_code(200)
398-
responseResultsJob.set_message("OK")
390+
responseResultsJob = DummyResponse(200)
399391
jobDataFile = data_path('job_1.vot')
400392
jobData = utils.read_file_content(jobDataFile)
401393
responseResultsJob.set_data(method='GET',
@@ -461,9 +453,7 @@ def test_cone_search_sync(self):
461453
tap = JwstClass(tap_plus_handler=tapplus, show_messages=False)
462454
# Launch response: we use default response because the
463455
# query contains decimals
464-
responseLaunchJob = DummyResponse()
465-
responseLaunchJob.set_status_code(200)
466-
responseLaunchJob.set_message("OK")
456+
responseLaunchJob = DummyResponse(200)
467457
jobDataFile = data_path('job_1.vot')
468458
jobData = utils.read_file_content(jobDataFile)
469459
responseLaunchJob.set_data(method='POST',
@@ -553,9 +543,7 @@ def test_cone_search_async(self):
553543
tap = JwstClass(tap_plus_handler=tapplus, show_messages=False)
554544
jobid = '12345'
555545
# Launch response
556-
responseLaunchJob = DummyResponse()
557-
responseLaunchJob.set_status_code(303)
558-
responseLaunchJob.set_message("OK")
546+
responseLaunchJob = DummyResponse(303)
559547
# list of list (httplib implementation for headers in response)
560548
launchResponseHeaders = [['location', 'http://test:1111/tap/async/' + jobid]]
561549
responseLaunchJob.set_data(method='POST',
@@ -568,19 +556,15 @@ def test_cone_search_async(self):
568556
radius = Quantity(1.0, u.deg)
569557
connHandler.set_default_response(responseLaunchJob)
570558
# Phase response
571-
responsePhase = DummyResponse()
572-
responsePhase.set_status_code(200)
573-
responsePhase.set_message("OK")
559+
responsePhase = DummyResponse(200)
574560
responsePhase.set_data(method='GET',
575561
context=None,
576562
body="COMPLETED",
577563
headers=None)
578564
req = "async/" + jobid + "/phase"
579565
connHandler.set_response(req, responsePhase)
580566
# Results response
581-
responseResultsJob = DummyResponse()
582-
responseResultsJob.set_status_code(200)
583-
responseResultsJob.set_message("OK")
567+
responseResultsJob = DummyResponse(200)
584568
jobDataFile = data_path('job_1.vot')
585569
jobData = utils.read_file_content(jobDataFile)
586570
responseResultsJob.set_data(method='GET',

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ class TestTap:
4444
def test_show_message(self):
4545
connHandler = DummyConnHandler()
4646

47-
dummy_response = DummyResponse()
48-
dummy_response.set_status_code(200)
49-
dummy_response.set_message("OK")
47+
dummy_response = DummyResponse(200)
5048

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

@@ -67,9 +65,7 @@ def test_query_object(self):
6765
conn_handler = DummyConnHandler()
6866
# Launch response: we use default response because the query contains
6967
# decimals
70-
dummy_response = DummyResponse()
71-
dummy_response.set_status_code(200)
72-
dummy_response.set_message("OK")
68+
dummy_response = DummyResponse(200)
7369

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

@@ -87,9 +83,7 @@ def test_query_object(self):
8783
tap = GaiaClass(conn_handler, tapplus, show_server_messages=True)
8884
# Launch response: we use default response because the query contains
8985
# decimals
90-
response_launch_job = DummyResponse()
91-
response_launch_job.set_status_code(200)
92-
response_launch_job.set_message("OK")
86+
response_launch_job = DummyResponse(200)
9387
job_data_file = data_path('job_1.vot')
9488
job_data = utils.read_file_content(job_data_file)
9589
response_launch_job.set_data(method='POST',
@@ -168,9 +162,7 @@ def test_query_object_async(self):
168162
tap = GaiaClass(conn_handler, tapplus, show_server_messages=False)
169163
jobid = '12345'
170164
# Launch response
171-
response_launch_job = DummyResponse()
172-
response_launch_job.set_status_code(303)
173-
response_launch_job.set_message("OK")
165+
response_launch_job = DummyResponse(303)
174166
# list of list (httplib implementation for headers in response)
175167
launch_response_headers = [
176168
['location', 'http://test:1111/tap/async/' + jobid]
@@ -181,19 +173,15 @@ def test_query_object_async(self):
181173
headers=launch_response_headers)
182174
conn_handler.set_default_response(response_launch_job)
183175
# Phase response
184-
response_phase = DummyResponse()
185-
response_phase.set_status_code(200)
186-
response_phase.set_message("OK")
176+
response_phase = DummyResponse(200)
187177
response_phase.set_data(method='GET',
188178
context=None,
189179
body="COMPLETED",
190180
headers=None)
191181
req = "async/" + jobid + "/phase"
192182
conn_handler.set_response(req, response_phase)
193183
# Results response
194-
response_results_job = DummyResponse()
195-
response_results_job.set_status_code(200)
196-
response_results_job.set_message("OK")
184+
response_results_job = DummyResponse(200)
197185
job_data_file = data_path('job_1.vot')
198186
job_data = utils.read_file_content(job_data_file)
199187
response_results_job.set_data(method='GET',
@@ -263,9 +251,7 @@ def test_cone_search_sync(self):
263251
tap = GaiaClass(conn_handler, tapplus, show_server_messages=False)
264252
# Launch response: we use default response because the query contains
265253
# decimals
266-
response_launch_job = DummyResponse()
267-
response_launch_job.set_status_code(200)
268-
response_launch_job.set_message("OK")
254+
response_launch_job = DummyResponse(200)
269255
job_data_file = data_path('job_1.vot')
270256
job_data = utils.read_file_content(job_data_file)
271257
response_launch_job.set_data(method='POST',
@@ -316,9 +302,7 @@ def test_cone_search_async(self):
316302
tap = GaiaClass(conn_handler, tapplus, show_server_messages=False)
317303
jobid = '12345'
318304
# Launch response
319-
response_launch_job = DummyResponse()
320-
response_launch_job.set_status_code(303)
321-
response_launch_job.set_message("OK")
305+
response_launch_job = DummyResponse(303)
322306
# list of list (httplib implementation for headers in response)
323307
launch_response_headers = [
324308
['location', 'http://test:1111/tap/async/' + jobid]
@@ -333,19 +317,15 @@ def test_cone_search_async(self):
333317
radius = Quantity(1.0, u.deg)
334318
conn_handler.set_default_response(response_launch_job)
335319
# Phase response
336-
response_phase = DummyResponse()
337-
response_phase.set_status_code(200)
338-
response_phase.set_message("OK")
320+
response_phase = DummyResponse(200)
339321
response_phase.set_data(method='GET',
340322
context=None,
341323
body="COMPLETED",
342324
headers=None)
343325
req = "async/" + jobid + "/phase"
344326
conn_handler.set_response(req, response_phase)
345327
# Results response
346-
response_results_job = DummyResponse()
347-
response_results_job.set_status_code(200)
348-
response_results_job.set_message("OK")
328+
response_results_job = DummyResponse(200)
349329
job_data_file = data_path('job_1.vot')
350330
job_data = utils.read_file_content(job_data_file)
351331
response_results_job.set_data(method='GET',
@@ -474,9 +454,7 @@ def test_xmatch(self):
474454
tap = GaiaClass(conn_handler, tapplus, show_server_messages=False)
475455
jobid = '12345'
476456
# Launch response
477-
response_launch_job = DummyResponse()
478-
response_launch_job.set_status_code(303)
479-
response_launch_job.set_message("OK")
457+
response_launch_job = DummyResponse(303)
480458
# list of list (httplib implementation for headers in response)
481459
launch_response_headers = [
482460
['location', 'http://test:1111/tap/async/' + jobid]
@@ -487,19 +465,15 @@ def test_xmatch(self):
487465
headers=launch_response_headers)
488466
conn_handler.set_default_response(response_launch_job)
489467
# Phase response
490-
response_phase = DummyResponse()
491-
response_phase.set_status_code(200)
492-
response_phase.set_message("OK")
468+
response_phase = DummyResponse(200)
493469
response_phase.set_data(method='GET',
494470
context=None,
495471
body="COMPLETED",
496472
headers=None)
497473
req = "async/" + jobid + "/phase"
498474
conn_handler.set_response(req, response_phase)
499475
# Results response
500-
response_results_job = DummyResponse()
501-
response_results_job.set_status_code(200)
502-
response_results_job.set_message("OK")
476+
response_results_job = DummyResponse(200)
503477
job_data_file = data_path('job_1.vot')
504478
job_data = utils.read_file_content(job_data_file)
505479
response_results_job.set_data(method='GET',

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ class DummyResponse:
2121
classdocs
2222
'''
2323

24-
def __init__(self):
24+
STATUS_MESSAGES = {200: "OK", 303: "OK", 500: "ERROR"}
25+
26+
def __init__(self, status_code=None):
2527
self.reason = ""
26-
self.status = 0
28+
self.set_status_code(status_code)
2729
self.index = 0
2830
self.set_data(None, None, None, None)
2931

30-
def set_status_code(self, status):
31-
self.status = status
32-
33-
def set_message(self, reason):
34-
self.reason = reason
32+
def set_status_code(self, status_code):
33+
self.status = status_code
34+
self.reason = self.STATUS_MESSAGES.get(status_code)
3535

3636
def set_data(self, method, context, body, headers):
3737
self.method = method

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def test_job_get_results(capsys, tmpdir):
4141
outputFormat = "votable"
4242
job.jobid = jobid
4343
job.parameters['format'] = outputFormat
44-
responseCheckPhase = DummyResponse()
45-
responseCheckPhase.set_status_code(500)
46-
responseCheckPhase.set_message("ERROR")
44+
responseCheckPhase = DummyResponse(500)
4745
responseCheckPhase.set_data(method='GET',
4846
context=None,
4947
body='FINISHED',
@@ -57,10 +55,7 @@ def test_job_get_results(capsys, tmpdir):
5755
job.get_results()
5856

5957
responseCheckPhase.set_status_code(200)
60-
responseCheckPhase.set_message("OK")
61-
responseGetData = DummyResponse()
62-
responseGetData.set_status_code(500)
63-
responseGetData.set_message("ERROR")
58+
responseGetData = DummyResponse(500)
6459
jobContentFileName = data_path('result_1.vot')
6560
jobContent = utils.read_file_content(jobContentFileName)
6661
responseGetData.set_data(method='GET',
@@ -74,7 +69,6 @@ def test_job_get_results(capsys, tmpdir):
7469
job.get_results()
7570

7671
responseGetData.set_status_code(200)
77-
responseGetData.set_message("OK")
7872
res = job.get_results()
7973
assert len(res) == 3
8074
assert len(res.columns) == 4

0 commit comments

Comments
 (0)