Skip to content

Commit 316d1e3

Browse files
Laura SandovalLaura Sandoval
authored andcommitted
add tests
1 parent 8dece3f commit 316d1e3

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

imap_data_access/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,13 @@ def add_query_args(subparser: ArgumentParser) -> None:
450450
"--type",
451451
type=str,
452452
required=False,
453-
help="SPICE kernel type (e.g. ephemeris_predicted). Only used with --table spice.",
453+
help="SPICE kernel type. Only used with --table spice. "
454+
"Valid types: attitude_history, attitude_predict, spin, repoint, "
455+
"ephemeris_reconstructed, ephemeris_nominal, ephemeris_predicted, "
456+
"ephemeris_90days, ephemeris_long, ephemeris_launch, planetary_ephemeris, "
457+
"planetary_constants, leapseconds, pointing_attitude, spacecraft_clock, "
458+
"imap_frames, science_frames, metakernel, thruster, lagrange_point, "
459+
"earth_attitude.",
454460
)
455461
subparser.set_defaults(func=_query_parser)
456462

tests/test_cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ def test_cli_works():
1616
cli.main()
1717

1818

19+
def test_cli_spice_query(capsys):
20+
"""Test the CLI SPICE query command."""
21+
with mock.patch.object(
22+
sys,
23+
"argv",
24+
[
25+
"imap-data-access",
26+
"query",
27+
"--table",
28+
"spice",
29+
"--type",
30+
"ephemeris_predicted",
31+
],
32+
):
33+
with mock.patch(
34+
"imap_data_access.cli.spice_query", return_value=[]
35+
) as mock_spice_query:
36+
cli.main()
37+
captured = capsys.readouterr()
38+
assert "Found [0] matching files" in captured.out
39+
mock_spice_query.assert_called_once_with(type="ephemeris_predicted")
40+
41+
1942
def test_cli_error_message(capsys):
2043
"""Test the CLI error message when no arguments are passed."""
2144
with mock.patch.object(

tests/test_io.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,91 @@ def test_query_bad_params(mock_send_request):
283283
assert mock_send_request.call_count == 0
284284

285285

286+
@pytest.mark.parametrize(
287+
"query_params",
288+
[
289+
# All parameters should send full query
290+
{
291+
"start_date": "20100101",
292+
"end_date": "20100102",
293+
"ingestion_start_date": "20100101",
294+
"ingestion_end_date": "20100102",
295+
"type": "ephemeris_predicted",
296+
"version": "v000",
297+
},
298+
# Make sure not all query params are sent if they are missing
299+
{"type": "ephemeris_predicted"},
300+
# Ingestion dates are remapped to /spice-query naming convention
301+
{"ingestion_start_date": "20100101", "ingestion_end_date": "20100102"},
302+
# version=latest maps to latest=true
303+
{"type": "ephemeris_predicted", "version": "latest"},
304+
],
305+
)
306+
def test_spice_query(mock_send_request, query_params: dict):
307+
"""Test a basic call to the SPICE Query API.
308+
309+
Parameters
310+
----------
311+
mock_send_request : unittest.mock.MagicMock
312+
Mock object for requests.Session
313+
query_params : dict
314+
Dictionary of key/value pairs that set the query parameters
315+
"""
316+
mock_response = MagicMock()
317+
mock_response.json.return_value = []
318+
mock_send_request.return_value = mock_response
319+
320+
response = imap_data_access.spice_query(**query_params)
321+
# No data found, and JSON decoding works as expected
322+
assert response == list()
323+
324+
# Should have only been one call to send
325+
mock_send_request.assert_called_once()
326+
# Assert that the correct URL was used for the query
327+
sent_request = mock_send_request.call_args[0][0]
328+
called_url = sent_request.url
329+
# Mirror the remapping logic in spice_query
330+
fixed_query = query_params.copy()
331+
if fixed_query.get("version") == "latest":
332+
del fixed_query["version"]
333+
fixed_query["latest"] = "true"
334+
if "ingestion_start_date" in fixed_query:
335+
fixed_query["start_ingest_date"] = fixed_query.pop("ingestion_start_date")
336+
if "ingestion_end_date" in fixed_query:
337+
fixed_query["end_ingest_date"] = fixed_query.pop("ingestion_end_date")
338+
str_params = "&".join(f"{k}={v}" for k, v in fixed_query.items())
339+
expected_url_encoded = f"https://api.test.com/spice-query?{str_params}"
340+
assert called_url == expected_url_encoded
341+
342+
343+
def test_spice_query_no_params(mock_send_request):
344+
"""Test a call to the SPICE Query API that has no parameters.
345+
346+
Parameters
347+
----------
348+
mock_send_request : unittest.mock.MagicMock
349+
Mock object for ``requests.session``
350+
"""
351+
with pytest.raises(ValueError, match="At least one query"):
352+
imap_data_access.spice_query()
353+
# Should not have made any calls to urlopen
354+
assert mock_send_request.call_count == 0
355+
356+
357+
def test_spice_query_bad_params(mock_send_request):
358+
"""Test a call to the SPICE Query API that has invalid parameters.
359+
360+
Parameters
361+
----------
362+
mock_send_request : unittest.mock.MagicMock
363+
Mock object for ``requests.session``
364+
"""
365+
with pytest.raises(TypeError, match="got an unexpected"):
366+
imap_data_access.spice_query(bad_param="test")
367+
# Should not have made any calls to urlopen
368+
assert mock_send_request.call_count == 0
369+
370+
286371
@pytest.mark.parametrize(
287372
("query_flag", "query_input", "expected_output"),
288373
[

0 commit comments

Comments
 (0)