Download behavior_backend/tests/unit/utils/test_data_utils.py from urjob/test: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/spaces/urjob/test/resolve/main/behavior_backend/tests/unit/utils/test_data_utils.py
- Command line
-
hf download hf://spaces/urjob/test/behavior_backend/tests/unit/utils/test_data_utils.py
-
curl -L -o test_data_utils.py https://huggingface.co/spaces/urjob/test/resolve/main/behavior_backend/tests/unit/utils/test_data_utils.py
3.82 kB
| 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 |