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
/
daily_activity_test.go
97 lines (88 loc) · 2.65 KB
/
daily_activity_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
package oura
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var dailyActivityCases = []struct {
name string
startDate string
endDate string
nextToken string
expectedURL string
mock string
}{
{
name: "get daily activity without specific dates",
startDate: "",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/daily_activity",
mock: `testdata/v2/daily_activity.json`,
},
{
name: "get daily activity with only start date",
startDate: "2020-01-20",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/daily_activity?start_date=2020-01-20",
mock: `{}`, // we don't care about the response here
},
{
name: "get daily activity with start and end dates",
startDate: "2020-01-20",
endDate: "2020-01-22",
nextToken: "",
expectedURL: "/v2/usercollection/daily_activity?end_date=2020-01-22&start_date=2020-01-20",
mock: `{}`, // we don't care about the response here
},
{
name: "get daily activity with next token",
startDate: "",
endDate: "",
nextToken: "thisisbase64encodedjson",
expectedURL: "/v2/usercollection/daily_activity?next_token=thisisbase64encodedjson",
mock: `{}`, // We don't care about the response here
},
{
name: "get error with dates the wrong way round",
startDate: "2020-01-25",
endDate: "2020-01-22",
nextToken: "",
expectedURL: "/v2/usercollection/daily_activity?end_date=2020-01-22&start_date=2020-01-25",
mock: `{
"detail": "Start date is greater than end date: [start_date: 2020-01-25; end_date: 2020-01-22]"
}`,
},
}
func TestDailyActivities(t *testing.T) {
for _, tc := range dailyActivityCases {
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)
}
testDailyActivities(t, tc.startDate, tc.endDate, tc.nextToken, tc.expectedURL, mock)
})
}
}
func testDailyActivities(t *testing.T, startDate, endDate, nextToken, expectedURL, mock string) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/v2/usercollection/daily_activity", 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.DailyActivities(context.Background(), startDate, endDate, nextToken)
assert.NoError(t, err, "should not return an error")
want := &DailyActivities{}
json.Unmarshal([]byte(mock), want)
assert.ObjectsAreEqual(want, got)
}