This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
heartrate_test.go
98 lines (89 loc) · 2.45 KB
/
heartrate_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
package oura
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
var heartrateCases = []struct {
name string
startDate string
endDate string
nextToken string
expectedURL string
mock string
}{
{
name: "get heartrates without specific dates",
startDate: "",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/heartrate",
mock: `{
"data": [
{
"bpm": 79,
"source": "awake",
"timestamp": "2022-04-02T12:38:38+00:00"
},
{
"bpm": 79,
"source": "awake",
"timestamp": "2022-04-02T12:38:43+00:00"
},
{
"bpm": 79,
"source": "awake",
"timestamp": "2022-04-02T12:38:47+00:00"
}],
"nextToken": "123456"
}`,
},
{
name: "get heartrates with only start date",
startDate: "2020-01-20T00:00:00+00:00",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/heartrate?start_datetime=2020-01-20T00%3A00%3A00%2B00%3A00",
mock: `{}`, // we don't care about the response
},
{
name: "get heartrates with start and end dates",
startDate: "2020-01-20T00:00:00+00:00",
endDate: "2020-01-22T00:00:00+00:00",
nextToken: "",
expectedURL: "/v2/usercollection/heartrate?end_datetime=2020-01-22T00%3A00%3A00%2B00%3A00&start_datetime=2020-01-20T00%3A00%3A00%2B00%3A00",
mock: `{}`,
},
{
name: "get heartrates with next token",
startDate: "",
endDate: "",
nextToken: "thisisbase64encodedjson",
expectedURL: "/v2/usercollection/heartrate?next_token=thisisbase64encodedjson",
mock: `{}`, // we don't care about the response
},
}
func TestHeartrates(t *testing.T) {
for _, tc := range heartrateCases {
t.Run(tc.name, func(t *testing.T) {
testHeartrates(t, tc.startDate, tc.endDate, tc.nextToken, tc.expectedURL, tc.mock)
})
}
}
func testHeartrates(t *testing.T, startDate, endDate, nextToken, expectedURL, mock string) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/v2/usercollection/heartrate", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, expectedURL, r.URL.String())
fmt.Fprint(w, mock)
})
got, _, err := client.Heartrates(context.Background(), startDate, endDate, nextToken)
assert.NoError(t, err, "should not return an error")
want := &Heartrate{}
json.Unmarshal([]byte(mock), want)
assert.ObjectsAreEqual(want, got)
}