-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmental_test.go
76 lines (67 loc) · 1.54 KB
/
mental_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
package runalyze
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var msTestCases = []struct {
name string
ms MentalState
status int
}{
{
name: "full mental state data",
ms: MentalState{
DateTime: time.Now(),
Fatigue: 8,
Stress: 5,
Mood: 2,
},
status: http.StatusCreated,
},
{
name: "invalid (fatigue too low) mental state data",
ms: MentalState{
DateTime: time.Now(),
Fatigue: -1,
Stress: 5,
Mood: 2,
},
status: http.StatusBadRequest,
},
{
name: "invalid (Stress too high) mental state data",
ms: MentalState{
DateTime: time.Now(),
Fatigue: -1,
Stress: 11,
Mood: 2,
},
status: http.StatusBadRequest,
},
}
func TestCreateMental(t *testing.T) {
for _, tc := range msTestCases {
t.Run(tc.name, func(st *testing.T) {
testCreateMental(st, tc.ms, tc.status)
})
}
}
func testCreateMental(t *testing.T, ms MentalState, status int) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/metrics/mental", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
var reqMs = MentalState{}
err := json.NewDecoder(r.Body).Decode(&reqMs)
assert.NoError(t, err, "should send a request that the API can parse")
assert.ObjectsAreEqual(reqMs, ms)
w.WriteHeader(http.StatusCreated)
})
resp, err := client.CreateMental(context.Background(), ms)
assert.NoError(t, err, "should not produce an error")
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}