File size: 3,816 Bytes
8ae78b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pytest
import pandas as pd
from typing import Dict, List, Any

from app.utils.data_utils import json_to_dataframe, calculate_emotion_percentages, format_results_for_api

class TestDataUtils:
    """Tests for the data utilities."""
    
    def test_json_to_dataframe_empty(self):
        """Test converting empty JSON to DataFrame."""
        # Arrange
        data = {}
        
        # Act
        df = json_to_dataframe(data)
        
        # Assert
        assert df.empty
    
    def test_json_to_dataframe(self):
        """Test converting JSON to DataFrame."""
        # Arrange
        data = {
            "backend1": [
                {
                    "frame_index": 0,
                    "faces": [
                        {
                            "face_box": [0, 0, 100, 100],
                            "emotion": {
                                "angry": 0.1,
                                "disgust": 0.1,
                                "fear": 0.1,
                                "happy": 0.5,
                                "sad": 0.1,
                                "surprise": 0.1,
                                "neutral": 0.0
                            }
                        }
                    ]
                }
            ]
        }
        
        # Act
        df = json_to_dataframe(data)
        
        # Assert
        assert not df.empty
        assert len(df) == 1
        assert "dominant_emotion" in df.columns
        assert "frame_index" in df.columns
        assert "backend" in df.columns
    
    def test_calculate_emotion_percentages_empty(self):
        """Test calculating emotion percentages with empty DataFrame."""
        # Arrange
        df = pd.DataFrame()
        
        # Act
        percentages = calculate_emotion_percentages(df)
        
        # Assert
        assert percentages == {
            "positive": 0,
            "negative": 0,
            "neutral": 0
        }
    
    def test_calculate_emotion_percentages(self):
        """Test calculating emotion percentages."""
        # Arrange
        data = {
            "dominant_emotion": ["happy", "sad", "neutral", "happy", "surprise"]
        }
        df = pd.DataFrame(data)
        
        # Act
        percentages = calculate_emotion_percentages(df)
        
        # Assert
        assert percentages["positive"] == 60.0  # 3/5 * 100
        assert percentages["negative"] == 20.0  # 1/5 * 100
        assert percentages["neutral"] == 20.0   # 1/5 * 100
    
    def test_format_results_for_api_empty(self):
        """Test formatting empty results for API."""
        # Arrange
        emotion_df = None
        transcript = ""
        analysis = {}
        
        # Act
        result = format_results_for_api(emotion_df, transcript, analysis)
        
        # Assert
        assert result["transcript"] == ""
        assert result["emotion_percentages"] == {
            "positive": 0,
            "negative": 0,
            "neutral": 0
        }
        assert result["analysis"] == {}
    
    def test_format_results_for_api(self):
        """Test formatting results for API."""
        # Arrange
        data = {
            "dominant_emotion": ["happy", "sad", "neutral", "happy", "surprise"]
        }
        emotion_df = pd.DataFrame(data)
        transcript = "Test transcript"
        analysis = {"test": "data"}
        
        # Act
        result = format_results_for_api(emotion_df, transcript, analysis)
        
        # Assert
        assert result["transcript"] == transcript
        assert result["emotion_percentages"]["positive"] == 60.0
        assert result["emotion_percentages"]["negative"] == 20.0
        assert result["emotion_percentages"]["neutral"] == 20.0
        assert result["analysis"] == analysis