This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendpoints_query_test.go
131 lines (120 loc) · 3.09 KB
/
endpoints_query_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
package simplejson_test
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestServer_Query(t *testing.T) {
testCases := []struct {
name string
request string
code int
pass bool
}{
{
name: "timeseries",
request: `{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z", "to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "A", "type": "timeserie" },{ "target": "B" }]
}`,
code: http.StatusOK,
pass: true,
},
{
name: "table",
request: `{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z","to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "C", "type": "table" }]
}`,
code: http.StatusOK,
pass: true,
},
{
name: "adhoc",
request: `{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z","to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "B" }],
"adhocFilters": [{"value":"B","operator":"<","condition":"","key":"100"}]
}`,
code: http.StatusOK,
pass: true,
},
{
name: "bad_target",
request: `{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z","to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "D", "type": "timeserie"}]
}`,
code: http.StatusInternalServerError,
pass: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "", bytes.NewBufferString(tt.request))
s.Query(w, req)
require.Equal(t, tt.code, w.Code)
if !tt.pass {
return
}
body, err := io.ReadAll(w.Body)
require.NoError(t, err)
gp := strings.ToLower(filepath.Join("testdata", t.Name()+".golden"))
if *update {
t.Logf("updating golden file for %s", t.Name())
err = os.WriteFile(gp, body, 0644)
require.NoError(t, err, "failed to update golden file")
}
var golden []byte
golden, err = os.ReadFile(gp)
require.NoError(t, err)
assert.Equal(t, string(golden), string(body))
})
}
}
func BenchmarkServer_Query(b *testing.B) {
for i := 0; i < b.N; i++ {
req, _ := http.NewRequest(http.MethodPost, "", bytes.NewBufferString(`{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z","to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "A" }]
}`))
w := httptest.NewRecorder()
s.Query(w, req)
if w.Code != http.StatusOK {
b.Fatalf("unexpected http code: %d", w.Code)
}
}
}
func BenchmarkServer_TableQuery(b *testing.B) {
for i := 0; i < b.N; i++ {
req, _ := http.NewRequest(http.MethodPost, "", bytes.NewBufferString(`{
"maxDataPoints": 100,
"interval": "1y",
"range": {"from": "2020-01-01T00:00:00.000Z","to": "2020-12-31T00:00:00.000Z"},
"targets": [{ "target": "C" }]
}`))
w := httptest.NewRecorder()
s.Query(w, req)
if w.Code != http.StatusOK {
b.Fatalf("unexpected http code: %d", w.Code)
}
}
}