def test_func(): assert paths.get_cache_dir(rootname='astropy') == temp_astropy_cache # Test temporary restoration of original default with paths.set_temp_cache() as d: assert d == orig_cache_dir == paths.get_cache_dir( rootname='astropy')
def test_get_horizons_coord(tmpdir): # get_horizons_coord() depends on astroquery astroquery = pytest.importorskip("astroquery") # Validate against published values from the Astronomical Almanac (2013) with set_temp_cache(tmpdir): e1 = get_horizons_coord('Geocenter', '2013-Jan-01') assert_quantity_allclose(e1.lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e1.lat, -3.03 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e1.radius, 0.9832947 * u.AU, atol=5e-7 * u.AU) with set_temp_cache(tmpdir): e2 = get_horizons_coord('Geocenter', '2013-Sep-01') assert_quantity_allclose(e1.lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e2.lat, 7.19 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e2.radius, 1.0092561 * u.AU, atol=5e-7 * u.AU)
def test_get_horizons_coord(tmpdir): # get_horizons_coord() depends on astroquery astroquery = pytest.importorskip("astroquery") with set_temp_cache(tmpdir): # Validate against published values from the Astronomical Almanac (2013) e1 = get_horizons_coord('Geocenter', '2013-Jan-01') assert_quantity_allclose((e1.lon + 1*u.deg) % (360*u.deg), 1*u.deg, atol=5e-6*u.deg) assert_quantity_allclose(e1.lat, -3.03*u.deg, atol=5e-3*u.deg) assert_quantity_allclose(e1.radius, 0.9832947*u.AU, atol=5e-7*u.AU) with set_temp_cache(tmpdir): e2 = get_horizons_coord('Geocenter', '2013-Sep-01') assert_quantity_allclose((e2.lon + 1*u.deg) % (360*u.deg), 1*u.deg, atol=5e-6*u.deg) assert_quantity_allclose(e2.lat, 7.19*u.deg, atol=5e-3*u.deg) assert_quantity_allclose(e2.radius, 1.0092561*u.AU, atol=5e-7*u.AU)
def test_name_resolve_cache(tmpdir): from astropy.utils.data import _get_download_cache_locs, get_cached_urls import shelve target_name = "castor" temp_cache_dir = str(tmpdir.mkdir('cache')) with paths.set_temp_cache(temp_cache_dir, delete=True): download_dir, urlmapfn = _get_download_cache_locs() with shelve.open(urlmapfn) as url2hash: assert len(url2hash) == 0 icrs1 = get_icrs_coordinates(target_name, cache=True) # This is a weak test: we just check to see that a url is added to the # cache! with shelve.open(urlmapfn) as url2hash: assert len(url2hash) == 1 url = get_cached_urls()[0] assert 'http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame/' in url # Try reloading coordinates, now should just reload cached data: with no_internet(): icrs2 = get_icrs_coordinates(target_name, cache=True) with shelve.open(urlmapfn) as url2hash: assert len(url2hash) == 1 assert u.allclose(icrs1.ra, icrs2.ra) assert u.allclose(icrs1.dec, icrs2.dec)
def run_tests(self, **kwargs): # The following option will include eggs inside a .eggs folder in # sys.path when running the tests. This is possible so that when # running pytest, test dependencies installed via e.g. # tests_requires are available here. This is not an advertised option # since it is only for internal use if kwargs.pop('add_local_eggs_to_path', False): # Add each egg to sys.path individually for egg in glob.glob(os.path.join('.eggs', '*.egg')): sys.path.insert(0, egg) # We now need to force reload pkg_resources in case any pytest # plugins were added above, so that their entry points are picked up import pkg_resources importlib.reload(pkg_resources) self._has_test_dependencies() # pragma: no cover # The docstring for this method is defined as a class variable. # This allows it to be built for each subclass in __new__. # Don't import pytest until it's actually needed to run the tests import pytest # Raise error for undefined kwargs allowed_kwargs = set(self.keywords.keys()) passed_kwargs = set(kwargs.keys()) if not passed_kwargs.issubset(allowed_kwargs): wrong_kwargs = list(passed_kwargs.difference(allowed_kwargs)) raise TypeError( f"run_tests() got an unexpected keyword argument {wrong_kwargs[0]}" ) args = self._generate_args(**kwargs) if kwargs.get('plugins', None) is not None: plugins = kwargs.pop('plugins') elif self.keywords.get('plugins', None) is not None: plugins = self.keywords['plugins'] else: plugins = [] # Override the config locations to not make a new directory nor use # existing cache or config. Note that we need to do this here in # addition to in conftest.py - for users running tests interactively # in e.g. IPython, conftest.py would get read in too late, so we need # to do it here - but at the same time the code here doesn't work when # running tests in parallel mode because this uses subprocesses which # don't know about the temporary config/cache. astropy_config = tempfile.mkdtemp('astropy_config') astropy_cache = tempfile.mkdtemp('astropy_cache') # Have to use nested with statements for cross-Python support # Note, using these context managers here is superfluous if the # config_dir or cache_dir options to pytest are in use, but it's # also harmless to nest the contexts with set_temp_config(astropy_config, delete=True): with set_temp_cache(astropy_cache, delete=True): return pytest.main(args=args, plugins=plugins)
def run_tests(self, **kwargs): # The following option will include eggs inside a .eggs folder in # sys.path when running the tests. This is possible so that when # runnning python setup.py test, test dependencies installed via e.g. # tests_requires are available here. This is not an advertised option # since it is only for internal use if kwargs.pop('add_local_eggs_to_path', False): # Add each egg to sys.path individually for egg in glob.glob(os.path.join('.eggs', '*.egg')): sys.path.insert(0, egg) # We now need to force reload pkg_resources in case any pytest # plugins were added above, so that their entry points are picked up import pkg_resources importlib.reload(pkg_resources) self._has_test_dependencies() # pragma: no cover # The docstring for this method is defined as a class variable. # This allows it to be built for each subclass in __new__. # Don't import pytest until it's actually needed to run the tests import pytest # Raise error for undefined kwargs allowed_kwargs = set(self.keywords.keys()) passed_kwargs = set(kwargs.keys()) if not passed_kwargs.issubset(allowed_kwargs): wrong_kwargs = list(passed_kwargs.difference(allowed_kwargs)) raise TypeError("run_tests() got an unexpected keyword argument {}".format(wrong_kwargs[0])) args = self._generate_args(**kwargs) if kwargs.get('plugins', None) is not None: plugins = kwargs.pop('plugins') elif self.keywords.get('plugins', None) is not None: plugins = self.keywords['plugins'] else: plugins = [] # If we are running the astropy tests with the astropy plugins handle # the config stuff, otherwise ignore it. if 'astropy.tests.plugins.config' not in plugins: return pytest.main(args=args, plugins=plugins) # override the config locations to not make a new directory nor use # existing cache or config astropy_config = tempfile.mkdtemp('astropy_config') astropy_cache = tempfile.mkdtemp('astropy_cache') # Have to use nested with statements for cross-Python support # Note, using these context managers here is superfluous if the # config_dir or cache_dir options to py.test are in use, but it's # also harmless to nest the contexts with set_temp_config(astropy_config, delete=True): with set_temp_cache(astropy_cache, delete=True): return pytest.main(args=args, plugins=plugins)
def test_consistency_with_horizons(tmpdir): # get_horizons_coord() depends on astroquery astroquery = pytest.importorskip("astroquery") # Check whether the location of Earth is the same between Astropy and JPL HORIZONS e1 = get_earth() with set_temp_cache(tmpdir): e2 = get_horizons_coord('Geocenter') assert_quantity_allclose(e1.separation_3d(e2), 0 * u.km, atol=25 * u.km)
def test_consistency_with_horizons(tmpdir): # get_horizons_coord() depends on astroquery astroquery = pytest.importorskip("astroquery") # Check whether the location of Earth is the same between Astropy and JPL HORIZONS e1 = get_earth() with set_temp_cache(tmpdir): e2 = get_horizons_coord('Geocenter') assert_quantity_allclose(e1.separation_3d(e2), 0*u.km, atol=25*u.km)
def test_set_temp_cache_resets_on_exception(tmpdir): """Test for regression of bug #9704""" t = paths.get_cache_dir() a = tmpdir / 'a' with open(a, 'wt') as f: f.write("not a good cache\n") with pytest.raises(OSError): with paths.set_temp_cache(a): pass assert t == paths.get_cache_dir()
def pytest_runtest_setup(item): config_dir = item.config.getini('astropy_config_dir') cache_dir = item.config.getini('astropy_cache_dir') # Command-line options can override, however config_dir = item.config.getoption('astropy_config_dir') or config_dir cache_dir = item.config.getoption('astropy_cache_dir') or cache_dir # We can't really use context managers directly in py.test (although # py.test 2.7 adds the capability), so this may look a bit hacky if config_dir: item.set_temp_config = set_temp_config(config_dir) item.set_temp_config.__enter__() if cache_dir: item.set_temp_cache = set_temp_cache(cache_dir) item.set_temp_cache.__enter__()
def test_set_temp_cache(tmpdir, monkeypatch): monkeypatch.setattr(paths.set_temp_cache, '_temp_path', None) orig_cache_dir = paths.get_cache_dir() temp_cache_dir = str(tmpdir.mkdir('cache')) temp_astropy_cache = os.path.join(temp_cache_dir, 'astropy') # Test decorator mode @paths.set_temp_cache(temp_cache_dir) def test_func(): assert paths.get_cache_dir() == temp_astropy_cache # Test temporary restoration of original default with paths.set_temp_cache() as d: assert d == orig_cache_dir == paths.get_cache_dir() test_func() # Test context manager mode (with cleanup) with paths.set_temp_cache(temp_cache_dir, delete=True): assert paths.get_cache_dir() == temp_astropy_cache assert not os.path.exists(temp_cache_dir)
def test_name_resolve_cache(tmpdir): from astropy.utils.data import get_cached_urls target_name = "castor" temp_cache_dir = str(tmpdir.mkdir('cache')) with paths.set_temp_cache(temp_cache_dir, delete=True): assert len(get_cached_urls()) == 0 icrs1 = get_icrs_coordinates(target_name, cache=True) urls = get_cached_urls() assert len(urls) == 1 assert 'http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame/' in urls[0] # Try reloading coordinates, now should just reload cached data: with no_internet(): icrs2 = get_icrs_coordinates(target_name, cache=True) assert len(get_cached_urls()) == 1 assert u.allclose(icrs1.ra, icrs2.ra) assert u.allclose(icrs1.dec, icrs2.dec)
def test_get_horizons_coord_array_time(tmpdir): # get_horizons_coord() depends on astroquery astroquery = pytest.importorskip("astroquery") # Validate against published values from the Astronomical Almanac (2013, C8-C13) array_time = Time(['2013-05-01', '2013-06-01', '2013-04-01', '2013-03-01']) with set_temp_cache(tmpdir): e = get_horizons_coord('Geocenter', array_time) assert_quantity_allclose(e[0].lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e[0].lat, -4.17 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e[0].radius, 1.0075271 * u.AU, atol=5e-7 * u.AU) assert_quantity_allclose(e[1].lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e[1].lat, -0.66 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e[1].radius, 1.0140013 * u.AU, atol=5e-7 * u.AU) assert_quantity_allclose(e[2].lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e[2].lat, -6.54 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e[2].radius, 0.9992311 * u.AU, atol=5e-7 * u.AU) assert_quantity_allclose(e[3].lon, 0 * u.deg, atol=5e-6 * u.deg) assert_quantity_allclose(e[3].lat, -7.22 * u.deg, atol=5e-3 * u.deg) assert_quantity_allclose(e[3].radius, 0.9908173 * u.AU, atol=5e-7 * u.AU)
def test_name_resolve_cache(tmpdir): from astropy.utils.data import get_cached_urls target_name = "castor" temp_cache_dir = str(tmpdir.mkdir('cache')) with paths.set_temp_cache(temp_cache_dir, delete=True): assert len(get_cached_urls()) == 0 icrs1 = get_icrs_coordinates(target_name, cache=True) urls = get_cached_urls() assert len(urls) == 1 expected_urls = sesame_url.get() assert any([urls[0].startswith(x) for x in expected_urls]), f'{urls[0]} not in {expected_urls}' # Try reloading coordinates, now should just reload cached data: with no_internet(): icrs2 = get_icrs_coordinates(target_name, cache=True) assert len(get_cached_urls()) == 1 assert u.allclose(icrs1.ra, icrs2.ra) assert u.allclose(icrs1.dec, icrs2.dec)
def test_func(): assert paths.get_cache_dir() == temp_astropy_cache # Test temporary restoration of original default with paths.set_temp_cache() as d: assert d == orig_cache_dir == paths.get_cache_dir()