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
/
session_test.go
87 lines (78 loc) · 2.18 KB
/
session_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
package oura
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var sessionCases = []struct {
name string
startDate string
endDate string
nextToken string
expectedURL string
mock string
}{
{
name: "get sessions without specific dates",
startDate: "",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/session",
mock: `testdata/v2/session.json`,
},
{
name: "get sessions with only start date",
startDate: "2020-01-20",
endDate: "",
nextToken: "",
expectedURL: "/v2/usercollection/session?start_date=2020-01-20",
mock: `{}`, // we don't care about the response
},
{
name: "get sessions with start and end dates",
startDate: "2020-01-20",
endDate: "2020-01-22",
nextToken: "",
expectedURL: "/v2/usercollection/session?end_date=2020-01-22&start_date=2020-01-20",
mock: `{}`, // we don't care about the response
},
{
name: "get sessions with next token",
startDate: "",
endDate: "",
nextToken: "thisisbase64encodedjson",
expectedURL: "/v2/usercollection/session?next_token=thisisbase64encodedjson",
mock: `{}`, // we don't care about the response
},
}
func TestSessions(t *testing.T) {
for _, tc := range sessionCases {
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)
}
testSessions(t, tc.startDate, tc.endDate, tc.nextToken, tc.expectedURL, mock)
})
}
}
func testSessions(t *testing.T, startDate, endDate, nextToken, expectedURL, mock string) {
client, mux, teardown := setup()
defer teardown()
mux.HandleFunc("/v2/usercollection/session", 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.Sessions(context.Background(), startDate, endDate, nextToken)
assert.NoError(t, err, "should not return an error")
want := &Sessions{}
json.Unmarshal([]byte(mock), want)
assert.ObjectsAreEqual(want, got)
}