PanGenomeWatchAI / tests /test_callbacks.py
Ashkan Taghipour (The University of Western Australia)
Initial deploy: Pigeon Pea Pangenome Atlas
16e4ad5
"""Tests for src/callbacks.py."""
import pytest
import plotly.graph_objects as go
from src.state import AppState
from src.callbacks import (
on_line_selected, build_umap_plot,
build_donut_chart, build_frequency_histogram, build_treasure_table,
on_pin_gene, build_hotspot_heatmap, get_protein_stats_html,
build_backpack_comparison,
)
class TestQuest0:
def test_on_line_selected_returns(self, synthetic_data):
state = AppState()
line_id = synthetic_data["line_stats"].iloc[0]["line_id"]
total, unique, neighbor, new_state = on_line_selected(line_id, state, synthetic_data)
assert total != "--"
assert int(total) > 0
def test_on_line_selected_updates_state(self, synthetic_data):
state = AppState()
line_id = synthetic_data["line_stats"].iloc[0]["line_id"]
_, _, _, new_state = on_line_selected(line_id, state, synthetic_data)
assert new_state.selected_line == line_id
class TestQuest1:
def test_umap_plot_returns_figure(self, synthetic_data):
state = AppState()
state.selected_line = synthetic_data["line_stats"].iloc[0]["line_id"]
fig = build_umap_plot("Country", state, synthetic_data)
assert isinstance(fig, go.Figure)
assert len(fig.data) >= 1
class TestQuest2:
def test_donut_three_segments(self, synthetic_data):
fig = build_donut_chart(95, 15, synthetic_data)
assert isinstance(fig, go.Figure)
assert len(fig.data) == 1
assert len(fig.data[0].values) == 3
def test_histogram_returns_figure(self, synthetic_data):
fig = build_frequency_histogram(95, 15, synthetic_data)
assert isinstance(fig, go.Figure)
def test_treasure_table_columns(self, synthetic_data):
state = AppState()
state.selected_line = synthetic_data["line_stats"].iloc[0]["line_id"]
df = build_treasure_table(state, 95, 15, "All", synthetic_data)
assert "gene_id" in df.columns
assert "freq_count" in df.columns
assert "core_class" in df.columns
def test_pin_gene(self):
state = AppState()
backpack_text, new_state = on_pin_gene("g00001", state)
assert "g00001" in new_state.backpack_genes
def test_pin_gene_idempotent(self):
state = AppState()
on_pin_gene("g00001", state)
backpack_text, new_state = on_pin_gene("g00001", state)
assert state.backpack_genes.count("g00001") == 1
def test_backpack_limit(self):
state = AppState()
for i in range(55):
state.add_to_backpack(f"g{i:05d}")
assert len(state.backpack_genes) <= 50
class TestQuest3:
def test_hotspot_heatmap(self, synthetic_data):
fig = build_hotspot_heatmap(synthetic_data)
assert isinstance(fig, go.Figure)
class TestQuest4:
def test_protein_stats_html(self, synthetic_data):
gene_id = synthetic_data["protein"].iloc[0]["gene_id"]
html = get_protein_stats_html(gene_id, synthetic_data)
assert "aa" in html or "protein" in html.lower()
def test_backpack_comparison_needs_2(self, synthetic_data):
state = AppState()
state.backpack_genes = ["g00001"]
fig = build_backpack_comparison(state, synthetic_data)
assert isinstance(fig, go.Figure)