This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_data_loader.py
More file actions
159 lines (138 loc) · 7.02 KB
/
Copy pathtest_user_data_loader.py
File metadata and controls
159 lines (138 loc) · 7.02 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import unittest
from unittest.mock import patch, MagicMock
from user_data_loader import UserDataLoader
import pandas as pd
from datetime import datetime
from io import BytesIO
class TestUserDataLoader(unittest.TestCase):
def setUp(self):
# Example of user profile data as it would be structured in an Excel file
self.profile_data = {
'User Profile Aspect': ['Unique ID', 'First Name', 'Last Name', 'Date Of Birth', 'Gender'],
'Details': ['123456', 'Alex', 'Johnson', '1990-04-15', 'Male']
}
self.profile_df = pd.DataFrame(self.profile_data)
# Example of physiological data as it would be structured in an Excel file
self.physiological_data = {
'heart-rate-bpm': [85.3447, 60.7059],
'breathing-rate-breaths-min': [18.5971, 12.2553],
'hrv-ms': [58.3913, 62.8153],
'skin-temp-c': [33.7697, 30.4123],
'emg-mv': [0.4739, 0.1398],
'bvp-unit': [0.8963, 0.8710],
'predicted-emotion': ['Surprised', 'Joyful']
}
self.physiological_df = pd.DataFrame(self.physiological_data)
def test_parse_user_profile(self):
data_loader = UserDataLoader(None)
profile = data_loader._parse_user_profile(self.profile_df)
self.assertIsInstance(profile, dict)
self.assertEqual(profile['unique-id'], 123456)
self.assertEqual(profile['first-name'], 'Alex')
self.assertEqual(profile['last-name'], 'Johnson')
expected_date_of_birth = '1990-04-15' # Expected date as a string
actual_date_of_birth = profile['date-of-birth']
self.assertEqual(actual_date_of_birth, expected_date_of_birth)
def test_parse_physiological_data(self):
# Correctly use self.physiological_df
data_loader = UserDataLoader(None)
physiological_entries = data_loader._parse_physiological_data(self.physiological_df)
self.assertIsInstance(physiological_entries, list)
self.assertEqual(len(physiological_entries), 2)
self.assertEqual(physiological_entries[0]['heart-rate-bpm'], 85.3447)
self.assertEqual(physiological_entries[1]['predicted-emotion'], 'Joyful')
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=['user1.xlsx', 'user2.xlsx'])
def test_load_users_with_valid_files(self, mock_listdir, mock_exists):
# Test loading of users with valid Excel files
mock_exists.return_value = True
mock_listdir.return_value = ['user1.xlsx', 'user2.xlsx']
with patch('pandas.read_excel') as mock_read_excel:
mock_read_excel.side_effect = [
{'user-profile': self.profile_df, 'data': self.physiological_df},
{'user-profile': self.profile_df, 'data': self.physiological_df}
]
data_loader = UserDataLoader('some/directory')
users = data_loader.load_users()
self.assertEqual(len(users), 2)
# More assertions to check the content of the users...
@patch('os.path.exists', return_value=False)
def test_directory_does_not_exist(self, mock_exists):
# Test when the directory does not exist
mock_exists.return_value = False
data_loader = UserDataLoader('non/existent/directory')
with self.assertRaises(FileNotFoundError):
data_loader.load_users()
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=[])
def test_no_files_in_directory(self, mock_listdir, mock_exists):
# Test when the directory is empty
mock_exists.return_value = True
mock_listdir.return_value = []
data_loader = UserDataLoader('empty/directory')
users = data_loader.load_users()
self.assertEqual(len(users), 0)
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=['user1.xlsx'])
@patch('pandas.read_excel')
def test_invalid_excel_file_format(self, mock_read_excel, mock_listdir, mock_exists):
# Simulate read_excel raising an EmptyDataError
mock_read_excel.side_effect = pd.errors.EmptyDataError
data_loader = UserDataLoader('some/directory')
with self.assertRaises(pd.errors.EmptyDataError):
data_loader.load_users()
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=['user1.xlsx'])
@patch('pandas.read_excel', return_value={})
def test_missing_expected_sheets(self, mock_read_excel, mock_listdir, mock_exists):
# Simulate read_excel returning an empty dict, as if the sheets are missing
data_loader = UserDataLoader('some/directory')
users = data_loader.load_users()
self.assertEqual(len(users), 0)
# Correct the patch decorators for the following tests
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=['user1.xlsx'])
@patch('pandas.read_excel')
def test_partial_data_in_profile(self, mock_read_excel, mock_listdir, mock_exists):
# Test when some user profile data is missing
# Make sure all arrays are of the same length
incomplete_profile_data = {
'User Profile Aspect': ['Unique ID', 'First Name', 'Last Name', 'Date Of Birth'],
'Details': ['123456', 'Alex', None, None] # Use None for missing values
}
incomplete_profile_df = pd.DataFrame(incomplete_profile_data)
mock_read_excel.return_value = {
'user-profile': incomplete_profile_df,
'data': self.physiological_df
}
data_loader = UserDataLoader('some/directory')
users = data_loader.load_users()
self.assertEqual(len(users), 1)
self.assertIn('unique-id', users[0].profile) # Access profile attribute of User
self.assertEqual(users[0].profile['unique-id'], 123456)
self.assertEqual(users[0].profile['first-name'], 'Alex')
self.assertIn('last-name', users[0].profile)
self.assertIsNone(users[0].profile['last-name']) # Use 'last-name'
@patch('os.path.exists', return_value=True)
@patch('os.listdir', return_value=['user1.xlsx'])
@patch('pandas.read_excel')
def test_missing_data_in_physiological_data(self, mock_read_excel, mock_listdir, mock_exists):
# Set up the mock to return the correct structure for the DataFrame
incomplete_physiological_df = pd.DataFrame({
'heart-rate-bpm': [None, 60.7059],
'breathing-rate-breaths-min': [18.5971, 12.2553],
'hrv-ms': [58.3913, 62.8153],
'skin-temp-c': [33.7697, 30.4123],
'emg-mv': [0.4739, 0.1398],
'bvp-unit': [0.8963, 0.8710],
'predicted-emotion': ['Surprised', 'Joyful']
})
# Mock the pandas.read_excel method to return a dictionary with the DataFrame
mock_read_excel.return_value = {
'user-profile': self.profile_df,
'data': incomplete_physiological_df
}
data_loader = UserDataLoader('some/directory')
users = data_loader.load_users()
if __name__ == '__main__':
unittest.main()