-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep_test.go
78 lines (69 loc) · 1.6 KB
/
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
package runalyze
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var sleepTestCases = []struct {
name string
sleep Sleep
status int
}{
{
name: "full sleep data",
sleep: Sleep{
DateTime: time.Now(),
Duration: 460,
RemDuration: 80,
LightSleepDuration: 70,
DeepSleepDuration: 70,
AwakeDuration: 70,
HrAverage: 89,
HrLowest: 60,
Quality: 8,
},
status: http.StatusCreated,
},
{
name: "partial sleep data",
sleep: Sleep{
DateTime: time.Now(),
Duration: 460,
Quality: 2,
},
status: http.StatusCreated,
},
{
name: "invalid (too high) sleep data",
sleep: Sleep{
DateTime: time.Now(),
Duration: 1460,
},
status: http.StatusBadRequest,
},
}
func TestSleep(t *testing.T) {
for _, tc := range sleepTestCases {
t.Run(tc.name, func(st *testing.T) {
testSleep(st, tc.sleep, tc.status)
})
}
}
func testSleep(t *testing.T, sleep Sleep, status int) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/metrics/sleep", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
var reqSleep = Sleep{}
err := json.NewDecoder(r.Body).Decode(&reqSleep)
assert.NoError(t, err, "should send a request that the API can parse")
assert.ObjectsAreEqual(reqSleep, sleep)
w.WriteHeader(http.StatusCreated)
})
resp, err := client.CreateSleep(context.Background(), sleep)
assert.NoError(t, err, "should not produce an error")
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}