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
/
Copy pathdaily_sleep_test.go
105 lines (96 loc) Β· 2.98 KB
/
daily_sleep_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
package oura
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var dailySleepCases = []struct {
name string
startDate string
endDate string
nextToken string
expectedURL string
mock string
}{
{
name: "get daily sleep without specific dates",
startDate: "",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/daily_sleep",
mock: `testdata/v2/daily_sleep.json`,
},
{
name: "get daily sleep with only start date",
startDate: "2020-01-20",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/daily_sleep?start_date=2020-01-20",
mock: `{}`, // we don't care about the response here
},
{
name: "get daily sleep with start and end dates",
startDate: "2020-01-20",
endDate: "2020-01-22",
nextToken: "",
expectedURL: "/v2/usercollection/daily_sleep?end_date=2020-01-22&start_date=2020-01-20",
mock: `{}`, // we don't care about the response here
},
{
name: "get daily sleep with next token",
startDate: "",
endDate: "",
nextToken: "thisisbase64encodedjson",
expectedURL: "/v2/usercollection/daily_sleep?next_token=thisisbase64encodedjson",
mock: `{}`, // We don't care about the response here
},
{
name: "get daily sleep with start and end dates and next token",
startDate: "2020-01-20",
endDate: "2020-01-22",
nextToken: "thisisbase64encodedjson",
expectedURL: "/v2/usercollection/daily_sleep?end_date=2020-01-22&next_token=thisisbase64encodedjson&start_date=2020-01-20",
mock: `{}`, // We don't care about the response here
},
{
name: "get error with dates the wrong way round",
startDate: "2021-10-01",
endDate: "2021-01-01",
nextToken: "",
expectedURL: "/v2/usercollection/daily_sleep?end_date=2021-01-01&start_date=2021-10-01",
mock: `{
"detail": "Start time is greater than end time: [start_time: 2021-10-01 01:02:03+00:00; end_date: 2021-01-01 01:02:03+00:00"
}`,
},
}
func TestDailySleep(t *testing.T) {
for _, tc := range dailySleepCases {
t.Run(tc.name, func(t *testing.T) {
mock := tc.mock
if strings.HasPrefix(tc.mock, "testdata/") {
resp, _ := os.ReadFile(tc.mock)
mock = string(resp)
}
testDailySleeps(t, tc.startDate, tc.endDate, tc.nextToken, tc.expectedURL, mock)
})
}
}
func testDailySleeps(t *testing.T, startDate, endDate, nextToken, expectedURL, mock string) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/v2/usercollection/daily_sleep", 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.DailySleeps(context.Background(), startDate, endDate, nextToken)
assert.NoError(t, err, "should not return an error")
want := &DailySleeps{}
json.Unmarshal([]byte(mock), want)
assert.ObjectsAreEqual(want, got)
}