forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout_test.go
186 lines (173 loc) · 4.13 KB
/
layout_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package server_test
import (
"context"
"encoding/json"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/mocks"
"github.com/influxdata/chronograf/server"
)
func Test_Layouts(t *testing.T) {
layoutTests := []struct {
name string
expected chronograf.Layout
allLayouts []chronograf.Layout
focusedApp string // should filter all layouts to this app only
shouldErr bool
}{
{
"empty layout",
chronograf.Layout{},
[]chronograf.Layout{},
"",
false,
},
{
"several layouts",
chronograf.Layout{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
},
[]chronograf.Layout{
{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
},
},
"",
false,
},
{
"filtered app",
chronograf.Layout{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
},
[]chronograf.Layout{
{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
},
{
ID: "b020101b-ea6b-4c8c-9f0e-db0ba501f4ef",
Application: "chronograf",
Measurement: "chronograf",
},
},
"influxdb",
false,
},
{
"axis zero values",
chronograf.Layout{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
Cells: []chronograf.Cell{
{
X: 0,
Y: 0,
W: 4,
H: 4,
I: "3b0e646b-2ca3-4df2-95a5-fd80915459dd",
Name: "A Graph",
CellColors: []chronograf.CellColor{},
Axes: map[string]chronograf.Axis{
"x": {
Bounds: []string{},
},
"y": {
Bounds: []string{},
},
"y2": {
Bounds: []string{},
},
},
},
},
},
[]chronograf.Layout{
{
ID: "d20a21c8-69f1-4780-90fe-e69f5e4d138c",
Application: "influxdb",
Measurement: "influxdb",
Cells: []chronograf.Cell{
{
X: 0,
Y: 0,
W: 4,
H: 4,
I: "3b0e646b-2ca3-4df2-95a5-fd80915459dd",
CellColors: []chronograf.CellColor{},
Name: "A Graph",
},
},
},
},
"",
false,
},
}
for _, test := range layoutTests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// setup mock chronograf.Service and mock logger
lg := &mocks.TestLogger{}
svc := server.Service{
Store: &mocks.Store{LayoutsStore: &mocks.LayoutsStore{
AllF: func(ctx context.Context) ([]chronograf.Layout, error) {
if len(test.allLayouts) == 0 {
return []chronograf.Layout{
test.expected,
}, nil
} else {
return test.allLayouts, nil
}
},
},
},
Logger: lg,
}
// setup mock request and response
rr := httptest.NewRecorder()
reqURL := url.URL{
Path: "/chronograf/v1/layouts",
}
params := reqURL.Query()
// add query params required by test
if test.focusedApp != "" {
params.Add("app", test.focusedApp)
}
// re-inject query params
reqURL.RawQuery = params.Encode()
req := httptest.NewRequest("GET", reqURL.RequestURI(), strings.NewReader(""))
// invoke handler for layouts endpoint
svc.Layouts(rr, req)
// create a throwaway frame to unwrap Layouts
respFrame := struct {
Layouts []struct {
chronograf.Layout
Link interface{} `json:"-"`
} `json:"layouts"`
}{}
// decode resp into respFrame
resp := rr.Result()
if err := json.NewDecoder(resp.Body).Decode(&respFrame); err != nil {
t.Fatalf("%q - Error unmarshaling JSON: err: %s", test.name, err.Error())
}
// compare actual and expected
if !cmp.Equal(test.expected, respFrame.Layouts[0].Layout) {
t.Fatalf("%q - Expected layouts to be equal: diff:\n\t%s", test.name, cmp.Diff(test.expected, respFrame.Layouts[0].Layout))
}
})
}
}