-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodycomposition_test.go
75 lines (66 loc) · 1.67 KB
/
bodycomposition_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
package runalyze
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var bcTestCases = []struct {
name string
bc BodyComposition
status int
}{
{
name: "full body composition data",
bc: BodyComposition{
DateTime: time.Now(),
Weight: 82.5,
FatPercentage: 11.23,
WaterPercentage: 1.5,
MusclePercentage: 19.7,
BonePercentage: 98,
},
status: http.StatusCreated,
},
{
name: "partial body composition data",
bc: BodyComposition{
DateTime: time.Now(),
Weight: 82.5,
FatPercentage: 11.23,
},
status: http.StatusCreated,
},
{
name: "invalid body composition data",
bc: BodyComposition{
DateTime: time.Now(),
FatPercentage: 11.23, // Weight is required
},
status: http.StatusBadRequest,
},
}
func TestCreateBodyComposition(t *testing.T) {
for _, tc := range bcTestCases {
t.Run(tc.name, func(st *testing.T) {
testBodyComposition(st, tc.bc, tc.status)
})
}
}
func testBodyComposition(t *testing.T, bc BodyComposition, status int) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/metrics/bodyComposition", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
var reqBc = BodyComposition{}
err := json.NewDecoder(r.Body).Decode(&reqBc)
assert.NoError(t, err, "should send a request that the API can parse")
assert.ObjectsAreEqual(reqBc, bc)
w.WriteHeader(http.StatusCreated)
})
resp, err := client.CreateBodyComposition(context.Background(), bc)
assert.NoError(t, err, "should not produce an error")
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}