|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +from pytest_cases import parametrize_with_cases |
| 4 | + |
| 5 | +from malariagen_data import af1 as _af1 |
| 6 | +from malariagen_data import ag3 as _ag3 |
| 7 | +from malariagen_data.anoph.describe import AnophelesDescribe |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def ag3_sim_api(ag3_sim_fixture): |
| 12 | + return AnophelesDescribe( |
| 13 | + url=ag3_sim_fixture.url, |
| 14 | + public_url=ag3_sim_fixture.url, |
| 15 | + config_path=_ag3.CONFIG_PATH, |
| 16 | + major_version_number=_ag3.MAJOR_VERSION_NUMBER, |
| 17 | + major_version_path=_ag3.MAJOR_VERSION_PATH, |
| 18 | + pre=True, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def af1_sim_api(af1_sim_fixture): |
| 24 | + return AnophelesDescribe( |
| 25 | + url=af1_sim_fixture.url, |
| 26 | + public_url=af1_sim_fixture.url, |
| 27 | + config_path=_af1.CONFIG_PATH, |
| 28 | + major_version_number=_af1.MAJOR_VERSION_NUMBER, |
| 29 | + major_version_path=_af1.MAJOR_VERSION_PATH, |
| 30 | + pre=False, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def case_ag3_sim(ag3_sim_fixture, ag3_sim_api): |
| 35 | + return ag3_sim_fixture, ag3_sim_api |
| 36 | + |
| 37 | + |
| 38 | +def case_af1_sim(af1_sim_fixture, af1_sim_api): |
| 39 | + return af1_sim_fixture, af1_sim_api |
| 40 | + |
| 41 | + |
| 42 | +@parametrize_with_cases("fixture,api", cases=".") |
| 43 | +def test_describe_api_returns_dataframe(fixture, api): |
| 44 | + """Test that describe_api returns a DataFrame with expected columns.""" |
| 45 | + df = api.describe_api() |
| 46 | + assert isinstance(df, pd.DataFrame) |
| 47 | + assert "method" in df.columns |
| 48 | + assert "summary" in df.columns |
| 49 | + assert "category" in df.columns |
| 50 | + assert len(df) > 0 |
| 51 | + |
| 52 | + |
| 53 | +@parametrize_with_cases("fixture,api", cases=".") |
| 54 | +def test_describe_api_no_private_methods(fixture, api): |
| 55 | + """Test that describe_api does not include private or dunder methods.""" |
| 56 | + df = api.describe_api() |
| 57 | + for method_name in df["method"]: |
| 58 | + assert not method_name.startswith( |
| 59 | + "_" |
| 60 | + ), f"Private method {method_name!r} should not appear in describe_api output" |
| 61 | + |
| 62 | + |
| 63 | +@parametrize_with_cases("fixture,api", cases=".") |
| 64 | +def test_describe_api_category_filter(fixture, api): |
| 65 | + """Test filtering by category.""" |
| 66 | + for category in ("data", "analysis", "plot"): |
| 67 | + df = api.describe_api(category=category) |
| 68 | + assert isinstance(df, pd.DataFrame) |
| 69 | + if len(df) > 0: |
| 70 | + assert all(df["category"] == category) |
| 71 | + |
| 72 | + |
| 73 | +@parametrize_with_cases("fixture,api", cases=".") |
| 74 | +def test_describe_api_invalid_category(fixture, api): |
| 75 | + """Test that an invalid category raises ValueError.""" |
| 76 | + with pytest.raises(ValueError, match="Invalid category"): |
| 77 | + api.describe_api(category="invalid") |
| 78 | + |
| 79 | + |
| 80 | +@parametrize_with_cases("fixture,api", cases=".") |
| 81 | +def test_describe_api_known_methods(fixture, api): |
| 82 | + """Test that some known methods appear in the output.""" |
| 83 | + df = api.describe_api() |
| 84 | + method_names = set(df["method"]) |
| 85 | + # These methods should exist on AnophelesDescribe (inherited from AnophelesBase). |
| 86 | + assert "describe_api" in method_names |
| 87 | + assert "sample_sets" in method_names |
| 88 | + |
| 89 | + |
| 90 | +@parametrize_with_cases("fixture,api", cases=".") |
| 91 | +def test_describe_api_summaries_not_empty(fixture, api): |
| 92 | + """Test that at least some methods have non-empty summaries.""" |
| 93 | + df = api.describe_api() |
| 94 | + non_empty = df[df["summary"] != ""] |
| 95 | + assert len(non_empty) > 0, "Expected at least some methods to have summaries" |
| 96 | + |
| 97 | + |
| 98 | +def test_categorize_method(): |
| 99 | + """Test the static _categorize_method helper.""" |
| 100 | + assert AnophelesDescribe._categorize_method("plot_pca") == "plot" |
| 101 | + assert AnophelesDescribe._categorize_method("plot_heterozygosity") == "plot" |
| 102 | + assert AnophelesDescribe._categorize_method("sample_metadata") == "data" |
| 103 | + assert AnophelesDescribe._categorize_method("snp_calls") == "data" |
| 104 | + assert AnophelesDescribe._categorize_method("genome_sequence") == "data" |
| 105 | + assert AnophelesDescribe._categorize_method("lookup_release") == "data" |
| 106 | + assert AnophelesDescribe._categorize_method("diversity_stats") == "analysis" |
| 107 | + assert AnophelesDescribe._categorize_method("cohort_diversity_stats") == "analysis" |
| 108 | + |
| 109 | + |
| 110 | +def test_extract_summary(): |
| 111 | + """Test the static _extract_summary helper.""" |
| 112 | + |
| 113 | + def dummy_func(): |
| 114 | + """This is a test summary. |
| 115 | +
|
| 116 | + More details here. |
| 117 | + """ |
| 118 | + pass |
| 119 | + |
| 120 | + summary = AnophelesDescribe._extract_summary(dummy_func) |
| 121 | + assert summary == "This is a test summary." |
| 122 | + |
| 123 | + def no_doc_func(): |
| 124 | + pass |
| 125 | + |
| 126 | + summary = AnophelesDescribe._extract_summary(no_doc_func) |
| 127 | + assert summary == "" |
0 commit comments