Skip to content

Commit 2c0b762

Browse files
committed
Make many gaia tests smaller and parametrized
The newly refactored tests achieve the same test coverage with less code and allow more tests to run is one of the `assert` statements fails.
1 parent e556a24 commit 2c0b762

File tree

1 file changed

+73
-116
lines changed

1 file changed

+73
-116
lines changed

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 73 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
GAIA_QUERIER = GaiaClass(show_server_messages=False)
3535
JOB_DATA = (Path(__file__).with_name("data") / "job_1.vot").read_text()
36+
RADIUS = 1 * u.deg
3637
SKYCOORD = SkyCoord(ra=19 * u.deg, dec=20 * u.deg, frame="icrs")
3738

3839

@@ -83,6 +84,13 @@ def mock_querier_async():
8384
return GaiaClass(tap_plus_conn_handler=conn_handler, datalink_handler=tapplus, show_server_messages=False)
8485

8586

87+
@pytest.fixture
88+
def cross_match_kwargs():
89+
return {"full_qualified_table_name_a": "schemaA.tableA",
90+
"full_qualified_table_name_b": "schemaB.tableB",
91+
"results_table_name": "results"}
92+
93+
8694
def test_show_message():
8795
connHandler = DummyConnHandler()
8896

@@ -101,44 +109,33 @@ def test_show_message():
101109
GaiaClass(tap_plus_conn_handler=connHandler, datalink_handler=tapplus, show_server_messages=True)
102110

103111

104-
def test_query_object(column_attrs, mock_querier):
105-
with pytest.raises(ValueError) as err:
106-
mock_querier.query_object(SKYCOORD)
107-
assert "Missing required argument: width" in err.value.args[0]
108-
109-
width = 12 * u.deg
110-
111-
with pytest.raises(ValueError) as err:
112-
mock_querier.query_object(SKYCOORD, width=width)
113-
assert "Missing required argument: height" in err.value.args[0]
114-
115-
table = mock_querier.query_object(SKYCOORD, width=width, height=10 * u.deg)
116-
assert len(table) == 3
117-
for colname, attrs in column_attrs.items():
118-
assert table[colname].attrs_equal(attrs)
119-
# by radius
120-
table = mock_querier.query_object(SKYCOORD, radius=1 * u.deg)
112+
@pytest.mark.parametrize(
113+
"kwargs", [{"width": 12 * u.deg, "height": 10 * u.deg}, {"radius": RADIUS}])
114+
def test_query_object(column_attrs, mock_querier, kwargs):
115+
table = mock_querier.query_object(SKYCOORD, **kwargs)
121116
assert len(table) == 3
122117
for colname, attrs in column_attrs.items():
123118
assert table[colname].attrs_equal(attrs)
124119

125120

126-
def test_query_object_async(column_attrs, mock_querier_async):
127-
table = mock_querier_async.query_object_async(
128-
SKYCOORD, width=12 * u.deg, height=10 * u.deg
129-
)
130-
assert len(table) == 3
131-
for colname, attrs in column_attrs.items():
132-
assert table[colname].attrs_equal(attrs)
133-
# by radius
134-
table = mock_querier_async.query_object_async(SKYCOORD, radius=1 * u.deg)
121+
@pytest.mark.parametrize(
122+
"kwargs,reported_missing", [({}, "width"), ({"width": 12 * u.deg}, "height")])
123+
def test_query_object_missing_argument(kwargs, reported_missing):
124+
with pytest.raises(ValueError, match=f"^Missing required argument: {reported_missing}$"):
125+
GAIA_QUERIER.query_object(SKYCOORD, **kwargs)
126+
127+
128+
@pytest.mark.parametrize(
129+
"kwargs", ({"width": 12 * u.deg, "height": 10 * u.deg}, {"radius": RADIUS}))
130+
def test_query_object_async(column_attrs, mock_querier_async, kwargs):
131+
table = mock_querier_async.query_object_async(SKYCOORD, **kwargs)
135132
assert len(table) == 3
136133
for colname, attrs in column_attrs.items():
137134
assert table[colname].attrs_equal(attrs)
138135

139136

140137
def test_cone_search_sync(column_attrs, mock_querier):
141-
job = mock_querier.cone_search(SKYCOORD, radius=1 * u.deg)
138+
job = mock_querier.cone_search(SKYCOORD, radius=RADIUS)
142139
assert job.async_ is False
143140
assert job.get_phase() == "COMPLETED"
144141
assert job.failed is False
@@ -150,8 +147,7 @@ def test_cone_search_sync(column_attrs, mock_querier):
150147

151148

152149
def test_cone_search_async(column_attrs, mock_querier_async):
153-
radius = 1.0 * u.deg
154-
job = mock_querier_async.cone_search_async(SKYCOORD, radius=radius)
150+
job = mock_querier_async.cone_search_async(SKYCOORD, radius=RADIUS)
155151
assert job.async_ is True
156152
assert job.get_phase() == "COMPLETED"
157153
assert job.failed is False
@@ -161,16 +157,18 @@ def test_cone_search_async(column_attrs, mock_querier_async):
161157
for colname, attrs in column_attrs.items():
162158
assert results[colname].attrs_equal(attrs)
163159

160+
161+
def test_cone_search_and_changing_MAIN_GAIA_TABLE(mock_querier_async):
164162
# Regression test for #2093 and #2099 - changing the MAIN_GAIA_TABLE
165163
# had no effect.
166-
# The preceding tests should have used the default value.
164+
job = mock_querier_async.cone_search_async(SKYCOORD, radius=RADIUS)
167165
assert 'gaiadr3.gaia_source' in job.parameters['query']
168166
with conf.set_temp("MAIN_GAIA_TABLE", "name_from_conf"):
169-
job = mock_querier_async.cone_search_async(SKYCOORD, radius=radius)
167+
job = mock_querier_async.cone_search_async(SKYCOORD, radius=RADIUS)
170168
assert "name_from_conf" in job.parameters["query"]
171169
# Changing the value through the class should overrule conf.
172170
mock_querier_async.MAIN_GAIA_TABLE = "name_from_class"
173-
job = mock_querier_async.cone_search_async(SKYCOORD, radius=radius)
171+
job = mock_querier_async.cone_search_async(SKYCOORD, radius=RADIUS)
174172
assert "name_from_class" in job.parameters["query"]
175173

176174

@@ -210,94 +208,53 @@ def get_datalinks_monkeypatched(self, ids, verbose):
210208
assert isinstance(result, Table)
211209

212210

213-
def test_xmatch(mock_querier_async):
214-
# missing table A
215-
with pytest.raises(ValueError) as err:
216-
mock_querier_async.cross_match(
217-
full_qualified_table_name_b='schemaB.tableB',
218-
results_table_name='results',
219-
)
220-
assert "Table name A argument is mandatory" in err.value.args[0]
221-
# missing schema A
222-
with pytest.raises(ValueError) as err:
223-
mock_querier_async.cross_match(
224-
full_qualified_table_name_a='tableA',
225-
full_qualified_table_name_b='schemaB.tableB',
226-
results_table_name='results',
227-
)
228-
assert "Not found schema name in full qualified table A: 'tableA'" \
229-
in err.value.args[0]
230-
# missing table B
231-
with pytest.raises(ValueError) as err:
232-
mock_querier_async.cross_match(
233-
full_qualified_table_name_a='schemaA.tableA',
234-
results_table_name='results',
235-
)
236-
assert "Table name B argument is mandatory" in err.value.args[0]
237-
# missing schema B
238-
with pytest.raises(ValueError) as err:
239-
mock_querier_async.cross_match(
240-
full_qualified_table_name_a='schemaA.tableA',
241-
full_qualified_table_name_b='tableB',
242-
results_table_name='results',
243-
)
244-
assert "Not found schema name in full qualified table B: 'tableB'" \
245-
in err.value.args[0]
246-
# missing results table
247-
with pytest.raises(ValueError) as err:
248-
mock_querier_async.cross_match(
249-
full_qualified_table_name_a='schemaA.tableA',
250-
full_qualified_table_name_b='schemaB.tableB',
251-
)
252-
assert "Results table name argument is mandatory" in err.value.args[0]
253-
# wrong results table (with schema)
254-
with pytest.raises(ValueError) as err:
255-
mock_querier_async.cross_match(
256-
full_qualified_table_name_a='schemaA.tableA',
257-
full_qualified_table_name_b='schemaB.tableB',
258-
results_table_name='schema.results',
259-
)
260-
assert "Please, do not specify schema for 'results_table_name'" \
261-
in err.value.args[0]
262-
# radius < 0.1
263-
with pytest.raises(ValueError) as err:
264-
mock_querier_async.cross_match(
265-
full_qualified_table_name_a='schemaA.tableA',
266-
full_qualified_table_name_b='schemaB.tableB',
267-
results_table_name='results',
268-
radius=0.01,
269-
)
270-
assert "Invalid radius value. Found 0.01, valid range is: 0.1 to 10.0" \
271-
in err.value.args[0]
272-
# radius > 10.0
273-
with pytest.raises(ValueError) as err:
274-
mock_querier_async.cross_match(
275-
full_qualified_table_name_a='schemaA.tableA',
276-
full_qualified_table_name_b='schemaB.tableB',
277-
results_table_name='results',
278-
radius=10.1
279-
)
280-
assert "Invalid radius value. Found 10.1, valid range is: 0.1 to 10.0" \
281-
in err.value.args[0]
282-
job = mock_querier_async.cross_match(
283-
full_qualified_table_name_a='schemaA.tableA',
284-
full_qualified_table_name_b='schemaB.tableB',
285-
results_table_name='results',
286-
)
211+
@pytest.mark.parametrize("background", [False, True])
212+
def test_cross_match(background, cross_match_kwargs, mock_querier_async):
213+
job = mock_querier_async.cross_match(**cross_match_kwargs, background=background)
287214
assert job.async_ is True
288-
assert job.get_phase() == "COMPLETED"
289-
assert job.failed is False
290-
job = mock_querier_async.cross_match(
291-
full_qualified_table_name_a='schemaA.tableA',
292-
full_qualified_table_name_b='schemaB.tableB',
293-
results_table_name='results',
294-
background=True,
295-
)
296-
assert job.async_ is True
297-
assert job.get_phase() == "EXECUTING"
215+
assert job.get_phase() == "EXECUTING" if background else "COMPLETED"
298216
assert job.failed is False
299217

300218

219+
@pytest.mark.parametrize(
220+
"kwarg,invalid_value,error_message",
221+
[("full_qualified_table_name_a",
222+
"tableA",
223+
"^Not found schema name in full qualified table A: 'tableA'$"),
224+
("full_qualified_table_name_b",
225+
"tableB",
226+
"^Not found schema name in full qualified table B: 'tableB'$"),
227+
("results_table_name",
228+
"schema.results",
229+
"^Please, do not specify schema for 'results_table_name'$")])
230+
def test_cross_match_invalid_mandatory_kwarg(
231+
cross_match_kwargs, kwarg, invalid_value, error_message
232+
):
233+
cross_match_kwargs[kwarg] = invalid_value
234+
with pytest.raises(ValueError, match=error_message):
235+
GAIA_QUERIER.cross_match(**cross_match_kwargs)
236+
237+
238+
@pytest.mark.parametrize("radius", [0.01, 10.1])
239+
def test_cross_match_invalid_radius(cross_match_kwargs, radius):
240+
with pytest.raises(
241+
ValueError,
242+
match=rf"^Invalid radius value. Found {radius}, valid range is: 0.1 to 10.0$",
243+
):
244+
GAIA_QUERIER.cross_match(**cross_match_kwargs, radius=radius)
245+
246+
247+
@pytest.mark.parametrize(
248+
"missing_kwarg,msg",
249+
[("full_qualified_table_name_a", "Table name A"),
250+
("full_qualified_table_name_b", "Table name B"),
251+
("results_table_name", "Results table name")])
252+
def test_cross_match_missing_mandatory_kwarg(cross_match_kwargs, missing_kwarg, msg):
253+
del cross_match_kwargs[missing_kwarg]
254+
with pytest.raises(ValueError, match=rf"^{msg} argument is mandatory$"):
255+
GAIA_QUERIER.cross_match(**cross_match_kwargs)
256+
257+
301258
@patch.object(TapPlus, 'login')
302259
def test_login(mock_login):
303260
conn_handler = DummyConnHandler()

0 commit comments

Comments
 (0)