| | 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.""" |
| | |
| | data = {} |
| | |
| | |
| | df = json_to_dataframe(data) |
| | |
| | |
| | assert df.empty |
| | |
| | def test_json_to_dataframe(self): |
| | """Test converting JSON to DataFrame.""" |
| | |
| | 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 |
| | } |
| | } |
| | ] |
| | } |
| | ] |
| | } |
| | |
| | |
| | df = json_to_dataframe(data) |
| | |
| | |
| | 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.""" |
| | |
| | df = pd.DataFrame() |
| | |
| | |
| | percentages = calculate_emotion_percentages(df) |
| | |
| | |
| | assert percentages == { |
| | "positive": 0, |
| | "negative": 0, |
| | "neutral": 0 |
| | } |
| | |
| | def test_calculate_emotion_percentages(self): |
| | """Test calculating emotion percentages.""" |
| | |
| | data = { |
| | "dominant_emotion": ["happy", "sad", "neutral", "happy", "surprise"] |
| | } |
| | df = pd.DataFrame(data) |
| | |
| | |
| | percentages = calculate_emotion_percentages(df) |
| | |
| | |
| | assert percentages["positive"] == 60.0 |
| | assert percentages["negative"] == 20.0 |
| | assert percentages["neutral"] == 20.0 |
| | |
| | def test_format_results_for_api_empty(self): |
| | """Test formatting empty results for API.""" |
| | |
| | emotion_df = None |
| | transcript = "" |
| | analysis = {} |
| | |
| | |
| | result = format_results_for_api(emotion_df, transcript, analysis) |
| | |
| | |
| | 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.""" |
| | |
| | data = { |
| | "dominant_emotion": ["happy", "sad", "neutral", "happy", "surprise"] |
| | } |
| | emotion_df = pd.DataFrame(data) |
| | transcript = "Test transcript" |
| | analysis = {"test": "data"} |
| | |
| | |
| | result = format_results_for_api(emotion_df, transcript, analysis) |
| | |
| | |
| | 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 |