forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation_test.go
186 lines (149 loc) · 3.7 KB
/
annotation_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 gapi
import (
"net/url"
"testing"
"github.com/gobs/pretty"
)
const (
annotationsJSON = `[{
"id": 1124,
"alertId": 0,
"dashboardId": 468,
"panelId": 2,
"userId": 1,
"userName": "",
"newState": "",
"prevState": "",
"time": 1507266395000,
"text": "test",
"metric": "",
"regionId": 1123,
"type": "event",
"tags": [
"tag1",
"tag2"
],
"data": {}
}]`
newAnnotationJSON = `{
"message":"Annotation added",
"id": 1,
"endId": 2
}`
newGraphiteAnnotationJSON = `{
"message":"Annotation added",
"id": 1
}`
updateAnnotationJSON = `{"message":"Annotation updated"}`
patchAnnotationJSON = `{"message":"Annotation patched"}`
deleteAnnotationJSON = `{"message":"Annotation deleted"}`
)
func TestAnnotations(t *testing.T) {
server, client := gapiTestTools(200, annotationsJSON)
defer server.Close()
params := url.Values{}
params.Add("from", "1506676478816")
params.Add("to", "1507281278816")
params.Add("limit", "100")
as, err := client.Annotations(params)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(as))
if as[0].ID != 1124 {
t.Error("annotations response should contain annotations with an ID")
}
}
func TestNewAnnotation(t *testing.T) {
server, client := gapiTestTools(200, newAnnotationJSON)
defer server.Close()
a := Annotation{
DashboardID: 123,
PanelID: 456,
Time: 1507037197339,
IsRegion: true,
TimeEnd: 1507180805056,
Tags: []string{"tag1", "tag2"},
Text: "text description",
}
res, err := client.NewAnnotation(&a)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != 1 {
t.Error("new annotation response should contain the ID of the new annotation")
}
}
func TestUpdateAnnotation(t *testing.T) {
server, client := gapiTestTools(200, updateAnnotationJSON)
defer server.Close()
a := Annotation{
Text: "new text description",
}
res, err := client.UpdateAnnotation(1, &a)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != "Annotation updated" {
t.Error("update annotation response should contain the correct response message")
}
}
func TestPatchAnnotation(t *testing.T) {
server, client := gapiTestTools(200, patchAnnotationJSON)
defer server.Close()
a := Annotation{
Text: "new text description",
}
res, err := client.PatchAnnotation(1, &a)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != "Annotation patched" {
t.Error("patch annotation response should contain the correct response message")
}
}
func TestNewGraphiteAnnotation(t *testing.T) {
server, client := gapiTestTools(200, newGraphiteAnnotationJSON)
defer server.Close()
a := GraphiteAnnotation{
What: "what",
When: 1507180805056,
Tags: []string{"tag1", "tag2"},
Data: "data",
}
res, err := client.NewGraphiteAnnotation(&a)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != 1 {
t.Error("new annotation response should contain the ID of the new annotation")
}
}
func TestDeleteAnnotation(t *testing.T) {
server, client := gapiTestTools(200, deleteAnnotationJSON)
defer server.Close()
res, err := client.DeleteAnnotation(1)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != "Annotation deleted" {
t.Error("delete annotation response should contain the correct response message")
}
}
func TestDeleteAnnotationByRegionID(t *testing.T) {
server, client := gapiTestTools(200, deleteAnnotationJSON)
defer server.Close()
res, err := client.DeleteAnnotationByRegionID(1)
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(res))
if res != "Annotation deleted" {
t.Error("delete annotation by region ID response should contain the correct response message")
}
}