def test_file_manager_refcounts() -> None: mock_file = mock.Mock() opener = mock.Mock(spec=open, return_value=mock_file) cache: dict = {} ref_counts: dict = {} manager = CachingFileManager(opener, "filename", cache=cache, ref_counts=ref_counts) assert ref_counts[manager._key] == 1 manager.acquire() assert cache manager2 = CachingFileManager( opener, "filename", cache=cache, ref_counts=ref_counts ) assert cache assert manager._key == manager2._key assert ref_counts[manager._key] == 2 with set_options(warn_for_unclosed_files=False): del manager gc.collect() assert cache assert ref_counts[manager2._key] == 1 mock_file.close.assert_not_called() with set_options(warn_for_unclosed_files=False): del manager2 gc.collect() assert not ref_counts assert not cache
def test_file_manager_refcounts(): mock_file = mock.Mock() opener = mock.Mock(spec=open, return_value=mock_file) cache = {} ref_counts = {} manager = CachingFileManager( opener, 'filename', cache=cache, ref_counts=ref_counts) assert ref_counts[manager._key] == 1 manager.acquire() assert cache manager2 = CachingFileManager( opener, 'filename', cache=cache, ref_counts=ref_counts) assert cache assert manager._key == manager2._key assert ref_counts[manager._key] == 2 with set_options(warn_for_unclosed_files=False): del manager gc.collect() assert cache assert ref_counts[manager2._key] == 1 mock_file.close.assert_not_called() with set_options(warn_for_unclosed_files=False): del manager2 gc.collect() assert not ref_counts assert not cache
def test_safe_cast_to_index_datetime_datetime(enable_cftimeindex): dates = [datetime(1, 1, day) for day in range(1, 20)] expected = pd.Index(dates) with set_options(enable_cftimeindex=enable_cftimeindex): actual = utils.safe_cast_to_index(np.array(dates)) assert_array_equal(expected, actual) assert isinstance(actual, pd.Index)
def test_safe_cast_to_index_cftimeindex(enable_cftimeindex): date_types = _all_cftime_date_types() for date_type in date_types.values(): dates = [date_type(1, 1, day) for day in range(1, 20)] if enable_cftimeindex and has_cftime: expected = CFTimeIndex(dates) else: expected = pd.Index(dates) with set_options(enable_cftimeindex=enable_cftimeindex): actual = utils.safe_cast_to_index(np.array(dates)) assert_array_equal(expected, actual) assert expected.dtype == actual.dtype assert isinstance(actual, type(expected))
def test_file_manager_autoclose(expected_warning) -> None: mock_file = mock.Mock() opener = mock.Mock(return_value=mock_file) cache: dict = {} manager = CachingFileManager(opener, "filename", cache=cache) manager.acquire() assert cache with set_options(warn_for_unclosed_files=expected_warning is not None): with pytest.warns(expected_warning): del manager gc.collect() assert not cache mock_file.close.assert_called_once_with()
def test_file_manager_autoclose(expected_warning): mock_file = mock.Mock() opener = mock.Mock(return_value=mock_file) cache = {} manager = CachingFileManager(opener, 'filename', cache=cache) manager.acquire() assert cache with set_options(warn_for_unclosed_files=expected_warning is not None): with pytest.warns(expected_warning): del manager gc.collect() assert not cache mock_file.close.assert_called_once_with()
def test_file_manager_autoclose_while_locked() -> None: opener = mock.Mock() lock = threading.Lock() cache: dict = {} manager = CachingFileManager(opener, "filename", lock=lock, cache=cache) manager.acquire() assert cache lock.acquire() with set_options(warn_for_unclosed_files=False): del manager gc.collect() # can't clear the cache while locked, but also don't block in __del__ assert cache
def test_file_manager_autoclose_while_locked(): opener = mock.Mock() lock = threading.Lock() cache = {} manager = CachingFileManager(opener, 'filename', lock=lock, cache=cache) manager.acquire() assert cache lock.acquire() with set_options(warn_for_unclosed_files=False): del manager gc.collect() # can't clear the cache while locked, but also don't block in __del__ assert cache
not has_scipy_or_netCDF4, reason='requires scipy or netCDF4') has_cftime_or_netCDF4 = has_cftime or has_netCDF4 requires_cftime_or_netCDF4 = pytest.mark.skipif( not has_cftime_or_netCDF4, reason='requires cftime or netCDF4') if not has_pathlib: has_pathlib, requires_pathlib = _importorskip('pathlib2') try: import_seaborn() has_seaborn = True except ImportError: has_seaborn = False requires_seaborn = pytest.mark.skipif(not has_seaborn, reason='requires seaborn') # change some global options for tests set_options(warn_for_unclosed_files=True) if has_dask: import dask if LooseVersion(dask.__version__) < '0.18': dask.set_options(get=dask.get) else: dask.config.set(scheduler='single-threaded') # pytest config try: _SKIP_FLAKY = not pytest.config.getoption("--run-flaky") _SKIP_NETWORK_TESTS = not pytest.config.getoption("--run-network-tests") except (ValueError, AttributeError): # Can't get config from pytest, e.g., because xarray is installed instead # of being run from a development version (and hence conftests.py is not