This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
folder_test.go
178 lines (153 loc) · 3.78 KB
/
folder_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
package gapi
import (
"strings"
"testing"
"github.com/gobs/pretty"
)
const (
getFoldersJSON = `{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}`
getFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
createdFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet ABC",
"url": "/dashboards/f/nErXDvCkzz/department-abc",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
updatedFolderJSON = `
{
"id":1,
"uid": "nErXDvCkzz",
"title": "Departmenet DEF",
"url": "/dashboards/f/nErXDvCkzz/department-def",
"hasAcl": false,
"canSave": true,
"canEdit": true,
"canAdmin": true,
"createdBy": "admin",
"created": "2018-01-31T17:43:12+01:00",
"updatedBy": "admin",
"updated": "2018-01-31T17:43:12+01:00",
"version": 1
}
`
deletedFolderJSON = `
{
"message":"Folder deleted"
}
`
)
func TestFolders(t *testing.T) {
mockData := strings.Repeat(getFoldersJSON+",", 1000) // make 1000 folders.
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of folders.
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, mockData},
{200, mockData},
{200, "[" + getFolderJSON + "]"},
})
const dashCount = 2001
folders, err := client.Folders()
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(folders))
if len(folders) != dashCount {
t.Errorf("Length of returned folders should be %d", dashCount)
}
if folders[0].ID != 1 || folders[0].Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folders.")
}
if folders[dashCount-1].ID != 1 || folders[dashCount-1].Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folders.")
}
}
func TestFolder(t *testing.T) {
client := gapiTestTools(t, 200, getFolderJSON)
folder := int64(1)
resp, err := client.Folder(folder)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(resp))
if resp.ID != folder || resp.Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folder.")
}
}
func TestFolderByUid(t *testing.T) {
client := gapiTestTools(t, 200, getFolderJSON)
folder := "nErXDvCkzz"
resp, err := client.FolderByUID(folder)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(resp))
if resp.UID != folder || resp.Title != "Departmenet ABC" {
t.Error("Not correctly parsing returned folder.")
}
}
func TestNewFolder(t *testing.T) {
client := gapiTestTools(t, 200, createdFolderJSON)
resp, err := client.NewFolder("test-folder")
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(resp))
if resp.UID != "nErXDvCkzz" {
t.Error("Not correctly parsing returned creation message.")
}
}
func TestUpdateFolder(t *testing.T) {
client := gapiTestTools(t, 200, updatedFolderJSON)
err := client.UpdateFolder("nErXDvCkzz", "test-folder")
if err != nil {
t.Fatal(err)
}
}
func TestDeleteFolder(t *testing.T) {
client := gapiTestTools(t, 200, deletedFolderJSON)
err := client.DeleteFolder("nErXDvCkzz")
if err != nil {
t.Fatal(err)
}
}