File size: 3,326 Bytes
16e4ad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""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)