-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartratemax_test.go
70 lines (61 loc) · 1.49 KB
/
heartratemax_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
package runalyze
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var hrmTestCase = []struct {
name string
hrm HeartRateMax
status int
}{
{
name: "valid heart rate max data",
hrm: HeartRateMax{
DateTime: time.Now(),
HeartRate: 192,
},
status: http.StatusCreated,
},
{
name: "invalid (too high) heart rate max data",
hrm: HeartRateMax{
DateTime: time.Now(),
HeartRate: 999,
},
status: http.StatusBadRequest,
},
{
name: "invalid (too low) heart rate max data",
hrm: HeartRateMax{
DateTime: time.Now(),
HeartRate: 2,
},
status: http.StatusBadRequest,
},
}
func TestCreateHeartRateMax(t *testing.T) {
for _, tc := range hrmTestCase {
t.Run(tc.name, func(st *testing.T) {
testCreateHeartRateMax(st, tc.hrm, tc.status)
})
}
}
func testCreateHeartRateMax(t *testing.T, hrm HeartRateMax, status int) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/metrics/heartRateMax", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
var reqHrm = HeartRateMax{}
err := json.NewDecoder(r.Body).Decode(&reqHrm)
assert.NoError(t, err, "should send a request that the API can parse")
assert.ObjectsAreEqual(reqHrm, hrm)
w.WriteHeader(http.StatusCreated)
})
resp, err := client.CreateHeartRateMax(context.Background(), hrm)
assert.NoError(t, err, "should not produce an error")
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}