Skip to content

Commit

Permalink
Fix/unit tests (#1159)
Browse files Browse the repository at this point in the history
* Update `TestCleanup::test_cleanup_all`

Replace hypothesis-based testing with Pytest's parametrize. Instead of
loading randomly generated AnnData objects, versions of the pancreas and
dentate gyrus are used.

* Update `test_datasets.py`

Update Python version for which dataset download is tests.

* Update `TestCleanup`

Increase deadling for `test_cleanup_default_clean_w_random_adata`.
  • Loading branch information
WeilerP authored Dec 2, 2023
1 parent d23edc8 commit 632c60b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions tests/core/test_anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import hypothesis.strategies as st
import pytest
from hypothesis import given
from hypothesis import given, settings

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -112,8 +112,23 @@ def test_different_obs_id_length(


class TestCleanup(TestBase):
@given(adata=get_adata(max_obs=5, max_vars=5), inplace=st.booleans())
def test_cleanup_all(self, adata: AnnData, inplace: bool):
@pytest.mark.parametrize("dataset", ["pancreas", "dentategyrus"])
@pytest.mark.parametrize("n_obs", [50, 100])
@pytest.mark.parametrize("layer", [None, "unspliced", "spliced"])
@pytest.mark.parametrize("dense", [True, False])
@pytest.mark.parametrize("inplace", [True, False])
def test_cleanup_all(
self, adata, dataset: str, n_obs: int, layer: bool, dense: bool, inplace: bool
):
adata = adata(dataset=dataset, n_obs=n_obs, raw=False, preprocessed=True)
adata.layers["dummy_layer"] = csr_matrix(np.eye(adata.n_obs, adata.n_vars))
adata.uns["dummy_entry"] = {"key": csr_matrix(np.eye(5, 7))}

if dense:
if layer is None:
adata.X = adata.X.A
else:
adata.layers[layer] = adata.layers[layer].A
returned_adata = cleanup(adata=adata, clean="all", inplace=inplace)

if not inplace:
Expand All @@ -122,12 +137,13 @@ def test_cleanup_all(self, adata: AnnData, inplace: bool):
else:
assert returned_adata is None

assert len(adata.layers) == 0
assert len(adata.uns) == 0
assert list(adata.layers.keys()) == ["Ms", "Mu", "spliced", "unspliced"]
assert list(adata.uns.keys()) == ["neighbors"]
assert len(adata.obs.columns) == 0
assert len(adata.var.columns) == 0

@given(adata=get_adata(max_obs=5, max_vars=5), inplace=st.booleans())
@settings(max_examples=10, deadline=1000)
def test_cleanup_default_clean_w_random_adata(self, adata: AnnData, inplace: bool):
n_obs_cols = len(adata.obs.columns)
n_var_cols = len(adata.var.columns)
Expand Down
2 changes: 1 addition & 1 deletion tests/datasets/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def dentategyrus_adata(tmpdir_factory):


@pytest.mark.skipif(
sys.version_info[:2] != (3, 8) or sys.platform != "linux",
sys.version_info[:2] != (3, 10) or sys.platform != "linux",
reason="Limit number of downloads to speed up testing.",
)
class TestDataSets:
Expand Down

0 comments on commit 632c60b

Please sign in to comment.