Spaces:
Sleeping
Sleeping
Download dataset/problem_20/test.py from Viraj0112/rl_code_fix_env: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/Viraj0112/rl_code_fix_env/resolve/main/dataset/problem_20/test.py
- Command line
-
hf download hf://spaces/Viraj0112/rl_code_fix_env/dataset/problem_20/test.py
-
curl -L -o test.py https://huggingface.co/spaces/Viraj0112/rl_code_fix_env/resolve/main/dataset/problem_20/test.py
1.88 kB
| import pytest | |
| from dataset.problem_20.buggy import analyze_user_activity | |
| def test_analyze_user_activity(): | |
| logs = [ | |
| {'user_id': 'u1', 'timestamp': '2023-10-01 10:00:00', 'action': 'login', 'duration_seconds': 120}, | |
| {'user_id': 'u1', 'timestamp': '2023-10-02 11:00:00', 'action': 'view', 'duration_seconds': 300}, | |
| {'user_id': 'u2', 'timestamp': '2023-09-20 09:00:00', 'action': 'login', 'duration_seconds': 60}, | |
| {'user_id': 'u2', 'timestamp': '2023-10-05 14:00:00', 'action': 'view', 'duration_seconds': 150}, | |
| {'user_id': 'u3', 'timestamp': '2023-10-06 16:00:00', 'action': 'view', 'duration_seconds': 200} | |
| ] | |
| current_date = '2023-10-07' | |
| result = analyze_user_activity(logs, current_date) | |
| # Active users last 7 days: 2023-10-01 to 2023-10-07 | |
| # u1 (Oct 1, Oct 2), u2 (Oct 5), u3 (Oct 6) | |
| # Expected: 3 | |
| assert result['active_users_last_7_days'] == 3 | |
| # Most common action | |
| # login: 2, view: 3 -> 'view' | |
| assert result['most_common_action'] == 'view' | |
| # Average session duration | |
| # Total = 120 + 300 + 60 + 150 + 200 = 830 | |
| # Count = 5 -> 830 / 5 = 166.0 | |
| assert result['average_session_duration'] == 166.0 | |
| # Engagement scores (duration / days active) | |
| # u1: duration = 420, days = 2 -> 210.0 | |
| # u2: duration = 210, days = 2 -> 105.0 | |
| # u3: duration = 200, days = 1 -> 200.0 | |
| scores = result['user_engagement_scores'] | |
| assert scores['u1'] == 210.0 | |
| assert scores['u2'] == 105.0 | |
| assert scores['u3'] == 200.0 | |
| def test_empty_logs(): | |
| result = analyze_user_activity([], '2023-10-07') | |
| assert result['active_users_last_7_days'] == 0 | |
| assert result['most_common_action'] is None | |
| assert result['average_session_duration'] == 0.0 | |
| assert result['user_engagement_scores'] == {} | |