@@ -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