-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplehealth_test.go
194 lines (172 loc) · 4.97 KB
/
applehealth_test.go
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
package applehealth
import (
"encoding/xml"
"fmt"
"io"
"log"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/AlekSi/applehealth/healthkit"
)
func TestUnmarshaler(t *testing.T) {
for _, ext := range []string{"xml", "zip"} {
t.Run(ext, func(t *testing.T) {
t.Parallel()
f := filepath.Join("testdata", "testdata."+ext)
if _, err := os.Stat(f); ext == "zip" && os.IsNotExist(err) {
t.Skipf("Generate ZIP archive by running `make %s`.", f)
}
u, err := NewUnmarshaler(f)
require.NoError(t, err)
defer func() {
assert.NoError(t, u.Close())
}()
u.DisallowUnhandledElements = true
expectedData := []healthkit.Data{
&healthkit.Record{
XMLName: xml.Name{Local: "Record"},
Type: "HKQuantityTypeIdentifierHeight",
Unit: "cm",
Value: "168",
SourceName: "My Processing Tool",
SourceVersion: "80",
CreationDate: "2017-10-29 21:29:16 +0300",
StartDate: "2015-12-21 13:46:53 +0300",
EndDate: "2015-12-21 13:46:53 +0300",
MetadataEntry: []*healthkit.MetadataEntry{{
XMLName: xml.Name{Local: "MetadataEntry"},
Key: "Original Creation Date",
Value: "2015-12-21 13:46:53 +0300",
}, {
XMLName: xml.Name{Local: "MetadataEntry"},
Key: "Original Source",
Value: "MyWatch",
}, {
XMLName: xml.Name{Local: "MetadataEntry"},
Key: "Source File Export Date",
Value: "2017-10-29 21:15:43 +0300",
}},
},
&healthkit.Record{
XMLName: xml.Name{Local: "Record"},
Type: "HKQuantityTypeIdentifierBodyMass",
Unit: "kg",
Value: "65",
SourceName: "Здоровье",
SourceVersion: "10.3.3",
CreationDate: "2017-07-25 08:18:55 +0300",
StartDate: "2017-07-25 08:18:00 +0300",
EndDate: "2017-07-25 08:18:00 +0300",
MetadataEntry: []*healthkit.MetadataEntry{{
XMLName: xml.Name{Local: "MetadataEntry"},
Key: "HKWasUserEntered",
Value: "1",
}},
},
}
for i, expected := range expectedData {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
actual, err := u.Next()
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
}
actual, err := u.Next()
assert.Equal(t, io.EOF, err)
assert.Nil(t, actual)
expectedMeta := &healthkit.Meta{
Locale: "ru_RU",
ExportDate: healthkit.ExportDate{
XMLName: xml.Name{Local: "ExportDate"},
Value: "2019-12-26 08:20:53 +0300",
},
Me: healthkit.Me{
XMLName: xml.Name{Local: "Me"},
DateOfBirth: "1987-10-02",
BiologicalSex: healthkit.BiologicalSexFemale,
BloodType: healthkit.BloodTypeABPositive,
FitzpatrickSkinType: "HKFitzpatrickSkinTypeVI",
},
}
assert.Equal(t, expectedMeta, u.Meta())
assert.Equal(t, time.Date(1987, 10, 2, 0, 0, 0, 0, time.UTC), expectedMeta.Me.DateOfBirthTime())
})
}
}
func Example() {
file := filepath.Join("testdata", "testdata.zip")
u, err := NewUnmarshaler(file)
if err != nil {
log.Fatal(err)
}
defer u.Close()
// call Next() once to be able to read metadata
var data healthkit.Data
if data, err = u.Next(); err != nil {
log.Fatal(err)
}
fmt.Printf("Meta: %s\n", u.Meta())
for {
fmt.Printf("Got %T:\n", data)
switch data := data.(type) {
case *healthkit.Record:
fmt.Printf("\t%s\n", data.Type)
}
if data, err = u.Next(); err != nil {
break
}
}
if err != io.EOF {
log.Fatal(err)
}
// Output:
// Meta: {Locale:ru_RU ExportDate:{XMLName:{Space: Local:ExportDate} Value:2019-12-26 08:20:53 +0300} Me:{XMLName:{Space: Local:Me} DateOfBirth:1987-10-02 BiologicalSex:HKBiologicalSexFemale BloodType:HKBloodTypeABPositive FitzpatrickSkinType:HKFitzpatrickSkinTypeVI}}
// Got *healthkit.Record:
// HKQuantityTypeIdentifierHeight
// Got *healthkit.Record:
// HKQuantityTypeIdentifierBodyMass
}
func BenchmarkUnmarshaler(b *testing.B) {
xml, err := filepath.Glob(filepath.FromSlash("benchdata/*.xml"))
if err != nil {
b.Fatal(err)
}
zip, err := filepath.Glob(filepath.FromSlash("benchdata/*.zip"))
if err != nil {
b.Fatal(err)
}
matches := make([]string, 0, len(xml)+len(zip))
matches = append(matches, xml...)
matches = append(matches, zip...)
for _, file := range matches {
file := file
b.Run(file, func(b *testing.B) {
if _, err := os.Stat(file); os.IsNotExist(err) {
b.Skipf("%s is absent.", file)
}
b.ReportAllocs()
u, err := NewUnmarshaler(file)
require.NoError(b, err)
defer u.Close()
u.DisallowUnhandledElements = true
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = u.Next()
if err != nil {
if err == io.EOF {
b.Skipf("%s is too small for N = %d.", file, b.N)
}
b.Fatal(err)
}
}
b.StopTimer()
offset := u.d.InputOffset()
b.SetBytes(offset / int64(b.N))
b.ReportMetric(float64(offset)/1024/1024, "MB")
})
}
}