-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheartraterest_test.go
70 lines (61 loc) · 1.51 KB
/
heartraterest_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 hrrTestCases = []struct {
name string
hrr HeartRateRest
status int
}{
{
name: "valid heart rate rest data",
hrr: HeartRateRest{
DateTime: time.Now(),
HeartRate: 100,
},
status: http.StatusCreated,
},
{
name: "invalid (too high) heart rate rest data",
hrr: HeartRateRest{
DateTime: time.Now(),
HeartRate: 999,
},
status: http.StatusBadRequest,
},
{
name: "invalid (too low) heart rate rest data",
hrr: HeartRateRest{
DateTime: time.Now(),
HeartRate: 2,
},
status: http.StatusBadRequest,
},
}
func TestCreateHeartRateRest(t *testing.T) {
for _, tc := range hrrTestCases {
t.Run(tc.name, func(st *testing.T) {
testCreateHeartRateRest(st, tc.hrr, tc.status)
})
}
}
func testCreateHeartRateRest(t *testing.T, hrr HeartRateRest, status int) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/metrics/heartRateRest", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
var reqHrr = HeartRateRest{}
err := json.NewDecoder(r.Body).Decode(&reqHrr)
assert.NoError(t, err, "should send a request that the API can parse")
assert.ObjectsAreEqual(reqHrr, hrr)
w.WriteHeader(http.StatusCreated)
})
resp, err := client.CreateHeartRateRest(context.Background(), hrr)
assert.NoError(t, err, "should not produce an error")
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}