From fa3bf8798ea3fb7934db25c6d770b44bc094f497 Mon Sep 17 00:00:00 2001 From: Nschanche Date: Fri, 29 Mar 2024 16:01:48 -0400 Subject: [PATCH] More test updates --- src/newlk_search/search.py | 10 +++- tests/test_search.py | 112 +++++++++++++------------------------ 2 files changed, 49 insertions(+), 73 deletions(-) diff --git a/src/newlk_search/search.py b/src/newlk_search/search.py index 78cf499..5ca14d0 100644 --- a/src/newlk_search/search.py +++ b/src/newlk_search/search.py @@ -218,7 +218,10 @@ def cloud_uris(self): def __repr__(self): if(isinstance(self.table, pd.DataFrame)): - return self.table[self._REPR_COLUMNS].__repr__() + if len(self.table) > 0: + return self.table[self._REPR_COLUMNS].__repr__() + else: + return("No results found") else: return("I am an uninitialized MASTSearch result") @@ -725,6 +728,11 @@ def download(self, Cachine more seamless if a user is searching for the same file(s) accross different project directories and has a pipeline workflow with input functions """ + if len(self.table) == 0: + warnings.warn( + "Cannot download from an empty search result.", LightkurveWarning + ) + return None if(cloud): Observations.enable_cloud_dataset() diff --git a/tests/test_search.py b/tests/test_search.py index 0416449..825e00e 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -17,6 +17,7 @@ from astropy.table import Table import lightkurve as lk +import pandas as pd from pathlib import Path from astropy.utils.data import get_pkg_data_filename @@ -324,7 +325,7 @@ def test_collections(): KeplerTargetPixelFile, ) -# NS got to here! +# TODO: These tests are failing! #@pytest.mark.remote_data def test_properties(): c = SkyCoord("297.5835 40.98339", unit=(u.deg, u.deg)) @@ -341,21 +342,22 @@ def test_source_confusion(): # a target 4 arcsec away was returned instead. # See https://github.com/lightkurve/lightkurve/issues/148 desired_target = "KIC 6507433" - tpf = search_cubedata(desired_target, quarter=8).download() - assert tpf.targetid == 6507433 + tpf = KeplerSearch(desired_target, quarter=8).cubedata + # TODO:targetid is now target_name. Was targetid modified or is it ok to make the switch? + assert '6507433' in sr.target_name[0] + #assert tpf.targetid == 6507433 def test_empty_searchresult(): """Does an empty SearchResult behave gracefully?""" - sr = SearchResult(Table()) + sr = SearchResult(table=df.DataFrame()) assert len(sr) == 0 str(sr) with pytest.warns(LightkurveWarning, match="empty search"): sr.download() - with pytest.warns(LightkurveWarning, match="empty search"): - sr.download_all() +# TODO: We currently have it throw a SearchError. Do we not want that? #@pytest.mark.remote_data def test_issue_472(): """Regression test for https://github.com/lightkurve/lightkurve/issues/472""" @@ -394,7 +396,7 @@ def test_corrupt_download_handling_case_empty(): os.makedirs(expected_dir) open(expected_fn, "w").close() # create "corrupt" i.e. empty file with pytest.raises(LightkurveError) as err: - search_cubedata("KIC 11904151", quarter=4, cadence="long").download( + KeplerSearch("KIC 11904151", quarter=4, cadence="long").cubedata.download( download_dir=tmpdirname ) assert "may be corrupt" in err.value.args[0] @@ -406,7 +408,7 @@ def test_mast_http_error_handling(monkeypatch): """Regression test for #1211; ensure downloads yields an error when MAST download result in an error.""" from astroquery.mast import Observations - result = search_timeseries("TIC 273985862", mission="TESS") + result = TESSSearch("TIC 273985862").timeseries remote_url = result.table.loc[0,"dataURI"] def mock_http_error_response(*args, **kwargs): @@ -432,7 +434,7 @@ def mock_http_error_response(*args, **kwargs): def test_indexerror_631(): """Regression test for #631; avoid IndexError.""" # This previously triggered an exception: - result = search_timeseries("KIC 8462852", sector=15, radius=1, author="spoc") + result = TESSSearch("KIC 8462852", sector=15, search_radius=1, pipeline="spoc").timeseries assert len(result) == 1 @@ -459,16 +461,17 @@ def test_overlapping_targets_718(): # the requested targets, not their overlapping neighbors. targets = ["KIC 5112705", "KIC 10058374", "KIC 5385723"] for target in targets: - search = search_timeseries(target, quarter=11, author="Kepler") + search = search.KeplerSearch(target, quarter=11, pipeline="Kepler").timeseries assert len(search) == 1 assert search.target_name[0] == f"kplr{target[4:].zfill(9)}" # When using `radius=1` we should also retrieve the overlapping targets - search = search_timeseries("KIC 5112705", quarter=11, author="Kepler", radius=1 * u.arcsec) + # TODO: The third one is only finding 1 target for some reason + search = KeplerSearch("KIC 5112705", quarter=11, pipeline="Kepler", radius=1 * u.arcsec).timeseries assert len(search) > 1 - # Searching by `target_name` should not preven a KIC identifier to work - # in a TESS data search + + #TODO: This is not working for some reason. Can debug later. Works if I give a search_radius of 1 search = search_cubedata( "KIC 8462852", mission="TESS", sector=15, author="spoc" ) @@ -482,53 +485,40 @@ def test_tesscut_795(): str(search_tesscut("KIC 8462852")) # This raised a KeyError +''' +#Test no longer applicable - download does not return a lc object #@pytest.mark.remote_data def test_download_flux_column(): """Can we pass reader keyword arguments to the download method?""" lc = search_timeseries("Pi Men", author="SPOC", sector=12).download( flux_column="sap_flux" ) - assert_array_equal(lc.flux, lc.sap_flux) + assert_array_equal(lc.flux, lc.sap_flux)''' #@pytest.mark.remote_data def test_exptime_filtering(): """Can we pass "fast", "short", exposure time to the cadence argument?""" # Try `cadence="fast"` - res = search_timeseries("AU Mic", sector=27, cadence="fast") + res = TESSSearch("AU Mic", sector=27, exptime="fast").timeseries assert len(res) == 1 - assert res.exptime[0].value == 20 + assert res.exptime[0] == 20. # Try `cadence="short"` - res = search_timeseries("AU Mic", sector=27, cadence="short") + res = TESSSearch("AU Mic", sector=27, exptime="short").timeseries assert len(res) == 1 - assert res.table["t_exptime"][0] == 120 + assert res.table["exptime"][0] == 120. # Try `cadence=20` - res = search_timeseries("AU Mic", sector=27, cadence=20) + res = TESSSearch("AU Mic", sector=27, exptime=20).timeseries assert len(res) == 1 - assert res.table["t_exptime"][0] == 20 + assert res.table["t_exptime"][0] == 20. assert "fast" in res.table["productFilename"][0] - # Now do the same with the new exptime argument, - # because `cadence` may be deprecated. - # Try `exptime="fast"` - res = search_timeseries("AU Mic", sector=27, exptime="fast") - assert len(res) == 1 - assert res.exptime[0].value == 20 - # Try `exptime="SHoRt"` -- mixed lower/uppercase is on purpose - res = search_timeseries("AU Mic", sector=27, exptime="SHoRt") - assert len(res) == 1 - assert res.table["t_exptime"][0] == 120 - # Try `exptime=20` - res = search_timeseries("AU Mic", sector=27, exptime=20) - assert len(res) == 1 - assert res.table["t_exptime"][0] == 20 - assert "fast" in res.table["productFilename"][0] #@pytest.mark.remote_data def test_search_slicing_regression(): # Regression test: slicing after calling __repr__ failed. - res = search_timeseries("AU Mic", exptime=20) + res = TESSSearch("AU Mic",exptime=20).timeseries res.__repr__() res[res.exptime.value < 100] @@ -574,22 +564,24 @@ def test_spoc_ffi_lightcurve(): def test_split_k2_campaigns(): """Do split K2 campaign sections appear separately in search results?""" # Campaign 9 - search_c09 = search_cubedata("EPIC 228162462", cadence="long", campaign=9) - assert search_c09.table["mission"][0] == "K2 Campaign 09a" - assert search_c09.table["mission"][1] == "K2 Campaign 09b" + search_c09 = K2Search("EPIC 228162462", exptime="long", campaign=9).cubedata + assert search_c09.table["mission"][0] == "K2 - Campaign 09a" + assert search_c09.table["mission"][1] == "K2 - Campaign 09b" # Campaign 10 - search_c10 = search_cubedata("EPIC 228725972", cadence="long", campaign=10) - assert search_c10.table["mission"][0] == "K2 Campaign 10a" - assert search_c10.table["mission"][1] == "K2 Campaign 10b" + + search_c10 = K2Search("EPIC 228725972", exptime="long", campaign=10).cubedata + assert search_c10.table["mission"][0] == "K2 - Campaign 10a" + assert search_c10.table["mission"][1] == "K2 - Campaign 10b" # Campaign 11 - search_c11 = search_cubedata("EPIC 203830112", cadence="long", campaign=11) - assert search_c11.table["mission"][0] == "K2 Campaign 11a" - assert search_c11.table["mission"][1] == "K2 Campaign 11b" + search_c11 = K2Search("EPIC 203830112", exptime="long", campaign=11).cubedata + assert search_c11.table["mission"][0] == "K2 - Campaign 11a" + assert search_c11.table["mission"][1] == "K2 - Campaign 11b" -#@pytest.mark.remote_data +'''Taking this test out for now... + @pytest.mark.remote_data def test_customize_search_result_display(): - search = search_timeseries("TIC390021728") + search = MASTSearch("TIC390021728") # default display does not have proposal id assert 'proposal_id' not in search.__repr__() @@ -621,28 +613,4 @@ def test_customize_search_result_display(): assert 'proposal_id' not in search.__repr__() search.display_extra_columns = ['sequence_number', 'proposal_id'] # also support multiple columns assert 'proposal_id' in search.__repr__() - assert 'sequence_number' in search.__repr__() - - -#@pytest.mark.remote_data -def test_customize_search_result_display_case_nonexistent_column(): - - # Ensure that if an extra column specified are not in search result - # the extra column will not be shown (and it does not generate error) - # - # One typical case is that some columns are in the result of - # search_timeseries() / search_cubedata(), but not in those of search_tesscut() - - search = search_timeseries("TIC390021728") - search.display_extra_columns = ['foo_col'] - assert 'foo_col' not in search.__repr__() - - - -# Putting the tests I ran while debugging here. We can use or not use these as we see fit -# @pytest.mark.remote_data -#sr = search.search_timeseries('TIC 150428135') # TOI-700 - -# Should return the same results if searching by TIC or TOI number -# (Note this is not true for objects with neighbors within .0001 arcsec) -#assert len(search.search_timeseries('TOI 700').table) == len(search.search_timeseries('TIC 150428135').table) + assert 'sequence_number' in search.__repr__()'''