@@ -2,6 +2,9 @@ package datadog
2
2
3
3
import (
4
4
"encoding/json"
5
+ "io/ioutil"
6
+ "net/http"
7
+ "net/http/httptest"
5
8
"testing"
6
9
7
10
"github.com/stretchr/testify/assert"
@@ -75,3 +78,154 @@ func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalAutoMinMax() {
75
78
func TestYAxisTestSuite (t * testing.T ) {
76
79
suite .Run (t , new (YAxisTestSuite ))
77
80
}
81
+
82
+ func TestDashboardGetters (t * testing.T ) {
83
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
84
+ response , err := ioutil .ReadFile ("./tests/fixtures/dashboards_response.json" )
85
+ if err != nil {
86
+ t .Fatal (err )
87
+ }
88
+ w .Write (response )
89
+ }))
90
+ defer ts .Close ()
91
+
92
+ datadogClient := Client {
93
+ baseUrl : ts .URL ,
94
+ HttpClient : http .DefaultClient ,
95
+ }
96
+
97
+ dashboards , err := datadogClient .GetDashboards ()
98
+ if err != nil {
99
+ t .Fatal (err )
100
+ }
101
+
102
+ if len (dashboards ) != 2 {
103
+ t .Fatalf ("expect response with 2 dashboards. Got %d" , len (dashboards ))
104
+ }
105
+
106
+ d1 := dashboards [0 ]
107
+
108
+ expectedID := 123
109
+ if id := d1 .GetId (); id != expectedID {
110
+ t .Fatalf ("expect ID %d. Got %d" , expectedID , id )
111
+ }
112
+
113
+ expectedTitle := "Dashboard 1"
114
+ if title := d1 .GetTitle (); title != expectedTitle {
115
+ t .Fatalf ("expect title %s. Got %s" , expectedTitle , title )
116
+ }
117
+
118
+ expectedDescription := "created by user1"
119
+ if description := d1 .GetDescription (); description != expectedDescription {
120
+ t .Fatalf ("expect description %s. Got %s" , expectedDescription , description )
121
+ }
122
+
123
+ expectedCreateTime := "2018-10-05T12:32:01.000000+00:00"
124
+ createTime , ok := d1 .GetCreatedOk ()
125
+ if ! ok {
126
+ t .Fatalf ("expect to have a created field" )
127
+ }
128
+
129
+ if createTime != expectedCreateTime {
130
+ t .Fatalf ("expect create time %s. Got %s" , expectedCreateTime , createTime )
131
+ }
132
+
133
+ expectedReadOnly := false
134
+ readOnly , ok := d1 .GetReadOnlyOk ()
135
+ if ! ok {
136
+ t .Fatalf ("expect to have a read_only field" )
137
+ }
138
+
139
+ if readOnly != expectedReadOnly {
140
+ t .Fatalf ("expect read_only %v. Got %v" , expectedReadOnly , readOnly )
141
+ }
142
+
143
+ expectedModified := "2018-09-11T06:38:09.000000+00:00"
144
+ modified , ok := d1 .GetModifiedOk ()
145
+ if ! ok {
146
+ t .Fatalf ("expect to have a modified field" )
147
+ }
148
+
149
+ if modified != expectedModified {
150
+ t .Fatalf ("expect modified %s. Got %s" , expectedModified , modified )
151
+ }
152
+
153
+ createdBy , ok := d1 .GetCreatedByOk ()
154
+ if ! ok {
155
+ t .Fatal ("expect created_by field to exist" )
156
+ }
157
+
158
+ validateCreatedBy (t , & createdBy )
159
+ }
160
+
161
+ func validateCreatedBy (t * testing.T , cb * CreatedBy ) {
162
+ expectedAccessRole := "adm"
163
+ accessRole , ok := cb .GetAccessRoleOk ()
164
+ if ! ok {
165
+ t .Fatal ("expect to have access_role field" )
166
+ }
167
+
168
+ if accessRole != expectedAccessRole {
169
+ t .Fatalf ("expect access_role %s. Got %s" , expectedAccessRole , accessRole )
170
+ }
171
+
172
+ expectedDisabled := true
173
+ disabled , ok := cb .GetDisabledOk ()
174
+ if ! ok {
175
+ t .Fatal ("expect to have disabled field" )
176
+ }
177
+
178
+ if disabled != expectedDisabled {
179
+ t .Fatalf ("expect disabled %v. Got %v" , expectedDisabled , disabled )
180
+ }
181
+
182
+ expectedEmail := "[email protected] "
183
+ email , ok := cb .GetEmailOk ()
184
+ if ! ok {
185
+ t .Fatal ("expect to have email field" )
186
+ }
187
+
188
+ if email != expectedEmail {
189
+ t .Fatalf ("expect email %s. Got %s" , expectedEmail , email )
190
+ }
191
+
192
+ expectedHandle := "[email protected] "
193
+ handle , ok := cb .GetHandleOk ()
194
+ if ! ok {
195
+ t .Fatal ("expect to have handle field" )
196
+ }
197
+
198
+ if handle != expectedHandle {
199
+ t .Fatalf ("expect handle %s. Got %s" , expectedHandle , handle )
200
+ }
201
+
202
+ expectedIcon := "https://pics.site.com/123.jpg"
203
+ icon , ok := cb .GetIconOk ()
204
+ if ! ok {
205
+ t .Fatal ("expect to have icon field" )
206
+ }
207
+
208
+ if icon != expectedIcon {
209
+ t .Fatalf ("expect icon %s. Got %s" , expectedIcon , icon )
210
+ }
211
+
212
+ expectedName := "John Doe"
213
+ name , ok := cb .GetNameOk ()
214
+ if ! ok {
215
+ t .Fatal ("expect to have name field" )
216
+ }
217
+
218
+ if name != expectedName {
219
+ t .Fatalf ("expect name %s. Got %s" , expectedName , name )
220
+ }
221
+
222
+ expectedVerified := true
223
+ verified , ok := cb .GetVerifiedOk ()
224
+ if ! ok {
225
+ t .Fatal ("expect to have verified field" )
226
+ }
227
+
228
+ if verified != expectedVerified {
229
+ t .Fatalf ("expect verify %v. Got %v" , expectedVerified , verified )
230
+ }
231
+ }
0 commit comments