forked from tdda/applehealthdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapplehealthdata.py
254 lines (217 loc) · 7.85 KB
/
testapplehealthdata.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
testapplehealthdata.py: tests for the applehealthdata.py
Copyright (c) 2016 Nicholas J. Radcliffe
Licence: MIT
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import re
import shutil
import sys
import unittest
from collections import Counter
from applehealthdata import (HealthDataExtractor,
format_freqs, format_value,
abbreviate, encode)
CLEAN_UP = True
VERBOSE = False
def get_base_dir():
"""
Return the directory containing this test file,
which will (normally) be the applyhealthdata directory
also containing the testdata dir.
"""
return os.path.split(os.path.abspath(__file__))[0]
def get_testdata_dir():
"""Return the full path to the testdata directory"""
return os.path.join(get_base_dir(), 'testdata')
def get_tmp_dir():
"""Return the full path to the tmp directory"""
return os.path.join(get_base_dir(), 'tmp')
def remove_any_tmp_dir():
"""
Remove the temporary directory if it exists.
Returns its location either way.
"""
tmp_dir = get_tmp_dir()
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
return tmp_dir
def make_tmp_dir():
"""
Remove any existing tmp directory.
Create empty tmp direcory.
Return the location of the tmp dir.
"""
tmp_dir = remove_any_tmp_dir()
os.mkdir(tmp_dir)
return tmp_dir
def copy_test_data():
"""
Copy the test data export6s3sample.xml from testdata directory
to tmp directory.
"""
tmp_dir = make_tmp_dir()
name = 'export6s3sample.xml'
in_xml_file = os.path.join(get_testdata_dir(), name)
out_xml_file = os.path.join(get_tmp_dir(), name)
shutil.copyfile(in_xml_file, out_xml_file)
return out_xml_file
class TestAppleHealthDataExtractor(unittest.TestCase):
@classmethod
def tearDownClass(cls):
"""Clean up by removing the tmp directory, if it exists."""
if CLEAN_UP:
remove_any_tmp_dir()
def check_file(self, filename):
expected_output = os.path.join(get_testdata_dir(), filename)
actual_output = os.path.join(get_tmp_dir(), filename)
with open(expected_output) as f:
expected = f.read()
with open(actual_output) as f:
actual = f.read()
self.assertEqual((filename, expected), (filename, actual))
def test_tiny_reference_extraction(self):
path = copy_test_data()
data = HealthDataExtractor(path, verbose=VERBOSE)
data.extract()
for kind in ('StepCount', 'DistanceWalkingRunning',
'Workout', 'ActivitySummary'):
self.check_file('%s.csv' % kind)
def test_format_freqs(self):
counts = Counter()
self.assertEqual(format_freqs(counts), '')
counts['one'] += 1
self.assertEqual(format_freqs(counts), 'one: 1')
counts['one'] += 1
self.assertEqual(format_freqs(counts), 'one: 2')
counts['two'] += 1
counts['three'] += 1
self.assertEqual(format_freqs(counts),
'''one: 2
three: 1
two: 1''')
def test_format_null_values(self):
for dt in ('s', 'n', 'd', 'z'):
# Note: even an illegal type, z, produces correct output for
# null values.
# Questionable, but we'll leave as a feature
self.assertEqual(format_value(None, dt), '')
def test_format_numeric_values(self):
cases = {
'0': '0',
'3': '3',
'-1': '-1',
'2.5': '2.5',
}
for (k, v) in cases.items():
self.assertEqual((k, format_value(k, 'n')), (k, v))
def test_format_date_values(self):
hearts = 'any string not need escaping or quoting; even this: ♥♥'
cases = {
'01/02/2000 12:34:56': '01/02/2000 12:34:56',
hearts: hearts,
}
for (k, v) in cases.items():
self.assertEqual((k, format_value(k, 'd')), (k, v))
def test_format_string_values(self):
cases = {
'a': '"a"',
'': '""',
'one "2" three': r'"one \"2\" three"',
r'1\2\3': r'"1\\2\\3"',
}
for (k, v) in cases.items():
self.assertEqual((k, format_value(k, 's')), (k, v))
def test_abbreviate(self):
changed = {
'HKQuantityTypeIdentifierHeight': 'Height',
'HKQuantityTypeIdentifierStepCount': 'StepCount',
'HK*TypeIdentifierStepCount': 'StepCount',
'HKCharacteristicTypeIdentifierDateOfBirth': 'DateOfBirth',
'HKCharacteristicTypeIdentifierBiologicalSex': 'BiologicalSex',
'HKCharacteristicTypeIdentifierBloodType': 'BloodType',
'HKCharacteristicTypeIdentifierFitzpatrickSkinType':
'FitzpatrickSkinType',
}
unchanged = [
'',
'a',
'aHKQuantityTypeIdentifierHeight',
'HKQuantityTypeIdentityHeight',
]
for (k, v) in changed.items():
self.assertEqual((k, abbreviate(k)), (k, v))
self.assertEqual((k, abbreviate(k, False)), (k, k))
for k in unchanged:
self.assertEqual((k, abbreviate(k)), (k, k))
def test_encode(self):
# This test looks strange, but because of the import statments
# from __future__ import unicode_literals
# in Python 2, type('a') is unicode, and the point of the encode
# function is to ensure that it has been converted to a UTF-8 string
# before writing to file.
self.assertEqual(type(encode('a')), str)
def test_extracted_reference_stats(self):
path = copy_test_data()
data = HealthDataExtractor(path, verbose=VERBOSE)
self.assertEqual(data.n_nodes, 20)
expectedRecordCounts = [
('DistanceWalkingRunning', 5),
('StepCount', 10),
]
self.assertEqual(sorted(data.record_types.items()),
expectedRecordCounts)
self.assertEqual(data.n_nodes, 20)
expectedOtherCounts = [
('ActivitySummary', 2),
('Workout', 1),
]
self.assertEqual(sorted(data.other_types.items()),
expectedOtherCounts)
expectedTagCounts = [
('ActivitySummary', 2),
('ExportDate', 1),
('Me', 1),
('Record', 15),
('Workout', 1),
]
self.assertEqual(sorted(data.tags.items()),
expectedTagCounts)
expectedFieldCounts = [
('HKCharacteristicTypeIdentifierBiologicalSex', 1),
('HKCharacteristicTypeIdentifierBloodType', 1),
('HKCharacteristicTypeIdentifierDateOfBirth', 1),
('HKCharacteristicTypeIdentifierFitzpatrickSkinType', 1),
('activeEnergyBurned', 2),
('activeEnergyBurnedGoal', 2),
('activeEnergyBurnedUnit', 2),
('appleExerciseTime', 2),
('appleExerciseTimeGoal', 2),
('appleStandHours', 2),
('appleStandHoursGoal', 2),
('creationDate', 16),
('dateComponents', 2),
('duration', 1),
('durationUnit', 1),
('endDate', 16),
('sourceName', 16),
('sourceVersion', 1),
('startDate', 16),
('totalDistance', 1),
('totalDistanceUnit', 1),
('totalEnergyBurned', 1),
('totalEnergyBurnedUnit', 1),
('type', 15),
('unit', 15),
('value', 16),
('workoutActivityType', 1)
]
self.assertEqual(sorted(data.fields.items()),
expectedFieldCounts)
if __name__ == '__main__':
unittest.main()