def test_no_time_error(): query = (a.Instrument('EVE'), a.Level(0)) with pytest.raises(ValueError) as excinfo: Fido.search(*query) assert all(str(a) in str(excinfo.value) for a in query) query1 = (a.Instrument('EVE') & a.Level(0)) query2 = (a.Time("2012/1/1", "2012/1/2") & a.Instrument("AIA")) with pytest.raises(ValueError) as excinfo: Fido.search(query1 | query2) assert all(str(a) in str(excinfo.value) for a in query1.attrs) assert all(str(a) not in str(excinfo.value) for a in query2.attrs)
def test_no_time_error(): # TODO: work out why this is failing sometimes. from sunpy.net import attrs as a query = (a.Instrument('EVE'), a.Level(0)) with pytest.raises(ValueError) as excinfo: Fido.search(*query) assert all(str(a) in str(excinfo.value) for a in query) query1 = (a.Instrument('EVE') & a.Level(0)) query2 = (a.Time("2012/1/1", "2012/1/2") & a.Instrument("AIA")) with pytest.raises(ValueError) as excinfo: Fido.search(query1 | query2) assert all(str(a) in str(excinfo.value) for a in query1.attrs) assert all(str(a) not in str(excinfo.value) for a in query2.attrs)
def test_unified_response(): start = parse_time("2012/1/1") end = parse_time("2012/1/2") qr = Fido.search(a.Instrument('EVE'), a.Level(0), a.Time(start, end)) assert qr.file_num == 2 strings = ['eve', 'SDO', start.strftime(TIMEFORMAT), end.strftime(TIMEFORMAT)] assert all(s in qr._repr_html_() for s in strings)
def test_fetch_working(suvi_client): """ Tests if the online server for goes_suvi is working. This also checks if the mock is working well. """ start = '2019/05/25 00:50' end = '2019/05/25 00:52' wave = 94 * u.Angstrom goes_sat = a.goes.SatelliteNumber.sixteen tr = a.Time(start, end) qr1 = suvi_client.search(tr, a.Instrument.suvi, a.Wavelength(wave), goes_sat, a.Level(2)) # Mock QueryResponse object mock_qr = mock_query_object(suvi_client) # Compare if two objects have the same attribute mock_qr = mock_qr[0] qr = qr1[0] assert mock_qr['Source'] == qr['Source'] assert mock_qr['Provider'] == qr['Provider'] assert mock_qr['Physobs'] == qr['Physobs'] assert mock_qr['Instrument'] == qr['Instrument'] assert mock_qr['url'] == qr['url'] assert qr1['Start Time'] == Time("2019-05-25T00:52:00.000") assert qr1['End Time'] == Time("2019-05-25T00:56:00.000") with tempfile.TemporaryDirectory() as tmpdirname: download_list = suvi_client.fetch(qr1, path=tmpdirname) assert len(download_list) == len(qr1)
def test_fido_waverange_level1b(start, end, wave1, wave2, expected_num_files): """check that we get all wavelengths if no wavelength is given""" goes_sat = a.goes.SatelliteNumber.sixteen result = Fido.search(a.Time(start, end), a.Instrument.suvi, goes_sat, a.Wavelength(wave1 * u.Angstrom, wave2 * u.Angstrom), a.Level('1b')) assert result.file_num == expected_num_files
def test_levels(time): """ Test the correct handling of level 0 / 1. The default should be level 1 from VSO, level 0 comes from EVEClient. """ eve_a = a.Instrument('EVE') qr = Fido.search(time, eve_a) client = qr.get_response(0).client assert isinstance(client, VSOClient) qr = Fido.search(time, eve_a, a.Level(0)) client = qr.get_response(0).client assert isinstance(client, eve.EVEClient) qr = Fido.search(time, eve_a, a.Level(0) | a.Level(1)) clients = {type(a.client) for a in qr.responses} assert clients.symmetric_difference({VSOClient, eve.EVEClient}) == set()
def test_get_url_for_time_range_level2_allwave(suvi_client, start, end, expected_num_files): """check that we get all wavelengths if no wavelength is given""" goes_sat = a.goes.SatelliteNumber.sixteen qresponse = suvi_client.search(a.Time(start, end), goes_sat, a.Level(2)) urls = [i['url'] for i in qresponse] assert isinstance(urls, list) assert len(urls) == expected_num_files
def test_save_path(tmpdir): qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) # Test when path is str files = Fido.fetch(qr, path=str(tmpdir / "{instrument}" / "{level}")) for f in files: assert str(tmpdir) in f assert "eve{}0".format(os.path.sep) in f
def online_query(draw, instrument=online_instruments()): query = draw(instrument) if isinstance(query, a.Instrument) and query.value == 'eve': query &= a.Level(0) if isinstance(query, a.Instrument) and query.value == 'norh': query &= a.Wavelength(17 * u.GHz) return query
def test_get_url_for_time_range_level2(suvi_client, start, end, wave, expected_num_files): goes_sat = a.goes.SatelliteNumber.sixteen qresponse = suvi_client.search(a.Time(start, end), a.Wavelength(wave * u.Angstrom), goes_sat, a.Level(2)) urls = [i['url'] for i in qresponse] assert isinstance(urls, list) assert len(urls) == expected_num_files
def test_save_path_pathlib(tmpdir): qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) # Test when path is pathlib.Path target_dir = tmpdir.mkdir("down") path = pathlib.Path(target_dir, "{instrument}", "{level}") files = Fido.fetch(qr, path=path) for f in files: assert target_dir.strpath in f assert "eve{}0".format(os.path.sep) in f
def offline_instruments(): """ Returns a strategy for any instrument that does not need the internet to do a query """ offline_instr = ['lyra', 'norh', 'noaa-indices', 'noaa-predict', 'goes'] offline_instr = st.builds(a.Instrument, st.sampled_from(offline_instr)) eve = st.just(a.Instrument('eve') & a.Level(0)) return st.one_of(offline_instr, eve)
def test_deprecated_identifier(): id = a.Instrument('EUI') time = a.Time('2021-02-01', '2021-02-02') level = a.Level(1) with pytest.warns(SunpyDeprecationWarning): identifier = a.soar.Identifier('EUI-FSI174-IMAGE') product = a.soar.Product('EUI-FSI174-IMAGE') res1 = Fido.search(id, time, level, identifier) res2 = Fido.search(id, time, level, product) assert res1.__str__() == res2.__str__()
def fido_search_result(): # A search query with responses from all instruments # No JSOC query return Fido.search( net_attrs.Time("2012/1/1", "2012/1/2"), net_attrs.Instrument('lyra') | net_attrs.Instrument('eve') | net_attrs.Instrument('XRS') | net_attrs.Instrument('noaa-indices') | net_attrs.Instrument('noaa-predict') | (net_attrs.Instrument('norh') & net_attrs.Wavelength(17 * u.GHz)) | net_attrs.Instrument('rhessi') | (net_attrs.Instrument('EVE') & net_attrs.Level(0)))
def test_save_path(): with tempfile.TemporaryDirectory() as target_dir: qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) files = Fido.fetch(qr, path=os.path.join( target_dir, "{instrument}" + os.path.sep + "{level}")) for f in files: assert target_dir in f assert "eve{}0".format(os.path.sep) in f
def test_save_path_pathlib(): pathlib = pytest.importorskip('pathlib') qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) # Test when path is pathlib.Path with tempfile.TemporaryDirectory() as target_dir: path = pathlib.Path(target_dir, "{instrument}", "{level}") files = Fido.fetch(qr, path=path) for f in files: assert target_dir in f assert "eve{}0".format(os.path.sep) in f
def test_levels(time): """ Test the correct handling of level Level 0 comes from EVEClient, other levels from EVE. """ eve_a = a.Instrument('EVE') qr = Fido.search(time, eve_a) clients = {type(a.client) for a in qr.responses} assert clients == {VSOClient} qr = Fido.search(time, eve_a, a.Level(0)) clients = {type(a.client) for a in qr.responses} assert clients == {eve.EVEClient}
def test_can_handle_query(suvi_client, time): ans1 = suvi_client._can_handle_query(time, a.Instrument('suvi')) assert ans1 is True ans2 = suvi_client._can_handle_query(time, a.Instrument('suvi'), a.Wavelength(131 * u.Angstrom)) assert ans2 is True ans3 = suvi_client._can_handle_query(time, a.Instrument('suvi'), a.Wavelength(131 * u.Angstrom), a.Level(2)) assert ans3 is True ans4 = suvi_client._can_handle_query(time) assert ans4 is False ans5 = suvi_client._can_handle_query(time, a.Instrument('aia')) assert ans5 is False ans6 = suvi_client._can_handle_query(time, a.Instrument('suvi'), a.goes.SatelliteNumber(16)) assert ans6 is True
def test_search(): id = a.Instrument('EUI') time = a.Time('2021-02-01', '2021-02-02') level = a.Level(1) product = a.soar.Product('EUI-FSI174-IMAGE') res = Fido.search(id, time, level, product) assert len(res) == 1 assert len(res[0]) == 43 assert u.allclose(res[0, 0]['Filesize'], 18.896 * u.Mbyte) files = Fido.fetch(res[0, 0]) assert len(files) == 1 fname = files[0] assert u.allclose(os.path.getsize(fname) * u.byte, res[0, 0]['Filesize'], atol=1e-3 * u.Mbyte) # Smoke test that we can read this into a map eui_map = sunpy.map.Map(fname)
def test_fido_waverange_level1b(start, end, wave1, wave2, expected_num_files): """check that we get all wavelengths if no wavelength is given""" result = Fido.search(a.Time(start, end), a.Instrument('suvi'), a.Wavelength(wave1 * u.Angstrom, wave2 * u.Angstrom), a.Level('1b')) assert result.file_num == expected_num_files
@pytest.mark.remote_data @pytest.mark.parametrize("time,instrument", [ (Time('2012/11/27', '2012/11/27'), Instrument('eve')), ]) def test_get(time, instrument): qr1 = LCClient.search(time, instrument) res = LCClient.fetch(qr1) assert len(res) == len(qr1) @pytest.mark.remote_data @pytest.mark.parametrize( 'query', [(a.Time('2012/10/4', '2012/10/6') & a.Instrument('eve') & a.Level(0))]) def test_fido(query): qr = Fido.search(query) client = qr.get_response(0).client assert isinstance(qr, UnifiedResponse) assert isinstance(client, eve.EVEClient) response = Fido.fetch(qr) assert len(response) == qr._numfile @pytest.mark.remote_data @pytest.mark.parametrize( 'time', [(a.Time('2012/10/4', '2012/10/6')), (a.Time('2012/11/27', '2012/11/27'))]) def test_levels(time): """
def test_fido_onewave_level1b(start, end, wave, expected_num_files): goes_sat = a.goes.SatelliteNumber.sixteen result = Fido.search(a.Time(start, end), a.Instrument.suvi, goes_sat, a.Wavelength(wave * u.Angstrom), a.Level('1b')) assert result.file_num == expected_num_files
def test_fetch(): qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) res = Fido.fetch(qr) assert isinstance(res, Results)
def test_attr_reg(): assert a.Instrument.lyra == a.Instrument('LYRA') assert a.Level.one == a.Level('1') assert a.Level.two == a.Level('2') assert a.Level.three == a.Level('3')
return query @settings(deadline=50000) @given(offline_query()) def test_offline_fido(query): unifiedresp = Fido.search(query) check_response(query, unifiedresp) @pytest.mark.remote_data @pytest.mark.flaky(reruns=5) # Until we get more mocked, we can't really do this to online clients. # TODO: Hypothesis this again @pytest.mark.parametrize("query", [ (a.Instrument('eve') & a.Time('2014/7/7', '2014/7/14') & a.Level(0)), (a.Instrument('rhessi') & a.Time('2014/7/7', '2014/7/14')), (a.Instrument('norh') & a.Time('2014/7/7', '2014/7/14') & a.Wavelength(17 * u.GHz)), ]) def test_online_fido(query): unifiedresp = Fido.search(query) check_response(query, unifiedresp) def check_response(query, unifiedresp): """ Common test for online or offline query """ query_tr = None query_instr = None
def test_no_wait_fetch(): qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0)) res = Fido.fetch(qr, wait=False) assert isinstance(res, DownloadResponse) assert isinstance(res.wait(), list)
def test_attr_reg(): assert a.Instrument.eve == a.Instrument('EVE') assert a.Level.zero == a.Level('0')
def test_fido_onewave_level1b(start, end, wave, expected_num_files): result = Fido.search(a.Time(start, end), a.Instrument.suvi, a.Wavelength(wave * u.Angstrom), a.Level('1b')) assert result.file_num == expected_num_files
HealthCheck.too_slow, HealthCheck.function_scoped_fixture ]) @given(dst.query_and()) def test_can_handle_query(client, query): # Can handle query never gets passed an AttrOr # It also never passes an AttrAnd, just the components of it if isinstance(query, attr.AttrAnd): assert client._can_handle_query(*query.attrs) else: assert client._can_handle_query(query) @pytest.mark.parametrize("query", ( a.Instrument("bob"), a.Physobs("who's got the button"), a.Level(2), (a.Instrument("VBI"), a.Level(0)), (a.Instrument("VBI"), a.Detector("test")), tuple(), )) def test_cant_handle_query(client, query): """Some examples of invalid queries.""" assert not client._can_handle_query(query) @no_vso @settings(suppress_health_check=[ HealthCheck.too_slow, HealthCheck.function_scoped_fixture ], deadline=None) @given(st.one_of(dst.query_and(), dst.query_or(), dst.query_or_composite()))