File size: 1,939 Bytes
0a476ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import pandas as pd
from unittest.mock import patch, MagicMock
from app.boxplot import plot_boxplot

# Fixture for valid test data
@pytest.fixture
def valid_data():
    """Fixture to provide valid intensity values for all conditions."""
    return pd.DataFrame({
        "SampleId": [1, 2, 3, 4],
        "condition": ["Healthy", "VEDOSS", "SSC_low", "SSC_high"],
        "Intensity": [10, 20, 15, 25]
    })

# Fixture for invalid test data
@pytest.fixture
def invalid_data():
    """Fixture to provide invalid intensity values (zero or negative)."""
    return pd.DataFrame({
        "SampleId": [1, 2, 3, 4],
        "condition": ["Healthy", "VEDOSS", "SSC_low", "SSC_high"],
        "Intensity": [-10, 0, 15, 25]
    })

@pytest.fixture
def protein_name():
    """Fixture for test protein name."""
    return "Test Protein"

@patch("matplotlib.pyplot.show")
@patch("matplotlib.pyplot.subplots")
def test_plot_boxplot(mock_subplots, mock_show, valid_data, protein_name):
    """Test that the plot_boxplot function initializes the plot and creates a boxplot."""
    # Mock figure and axes
    mock_fig = MagicMock()
    mock_ax = MagicMock()
    mock_subplots.return_value = (mock_fig, mock_ax)

    # Call the function
    plot_boxplot(valid_data, protein_name)

    # Verify that plt.subplots is called with correct parameters
    mock_subplots.assert_called_once_with(figsize=(12, 8))

    # Verify that the boxplot method is called on the axes
    mock_ax.boxplot.assert_called_once()

    # Verify that plt.show() is called to display the plot
    mock_show.assert_called_once()

def test_invalid_data_raises_error(invalid_data, protein_name):
    """Test that plot_boxplot raises a ValueError for zero or negative intensity values."""
    with pytest.raises(ValueError, match="contains zero or negative values"):
        plot_boxplot(invalid_data, protein_name)