diff --git a/tests/test_backed_hdf5.py b/tests/test_backed_hdf5.py index 34571793c..3bf387cbc 100644 --- a/tests/test_backed_hdf5.py +++ b/tests/test_backed_hdf5.py @@ -318,6 +318,32 @@ def test_return_to_memory_mode(adata: ad.AnnData, backing_h5ad: Path): bdata.filename = None +@pytest.mark.parametrize( + "x", + [ + pytest.param(np.arange(12).reshape(3, 4), id="dense"), + pytest.param(sparse.csr_matrix(np.arange(12).reshape(3, 4)), id="csr"), + pytest.param(sparse.csc_matrix(np.arange(12).reshape(3, 4)), id="csc"), + ], +) +def test_return_to_memory_mode_after_read_h5ad( + tmp_path: Path, x: np.ndarray | sparse.spmatrix +): + pth = tmp_path / "tmp.h5ad" + expected = np.arange(12).reshape(3, 4) + ad.AnnData(X=x).write_h5ad(pth) + + backed = ad.read_h5ad(pth, backed="r+") + assert backed.isbacked + + backed.filename = None + assert not backed.isbacked + assert backed.X is not None + + got = backed.X.toarray() if sparse.issparse(backed.X) else backed.X + np.testing.assert_array_equal(got, expected) + + def test_backed_modification(adata: ad.AnnData, backing_h5ad: Path): adata.X[:, 1] = 0 # Make it a little sparse adata.X = sparse.csr_matrix(adata.X)