@@ -11,68 +11,89 @@ import (
1111)
1212
1313func TestGetScreenboard (t * testing.T ) {
14- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
15- response , err := ioutil .ReadFile ("./testdata/fixtures/screenboard_response.json" )
16- if err != nil {
17- // mustn't call t.Fatal from a different Goroutine
18- http .Error (w , err .Error (), http .StatusInternalServerError )
19- return
20- }
21- w .Write (response )
22- }))
23- defer ts .Close ()
24-
25- datadogClient := Client {
26- baseUrl : ts .URL ,
27- HttpClient : ts .Client (),
28- RetryTimeout : 5 * time .Second ,
29- }
30-
31- got , err := datadogClient .GetScreenboard (6334 )
32- if err != nil {
33- t .Fatal (err )
34- }
35-
36- want := & Screenboard {
37- Id : Int (6334 ),
38- Title : String ("dogapi test" ),
39- Height : Int (768 ),
40- Width : Int (1024 ),
41- ReadOnly : Bool (false ),
42- Widgets : []Widget {
43- Widget {
44- Type : String ("image" ),
45- Height : Int (20 ),
46- Width : Int (32 ),
47- X : Int (32 ),
48- Y : Int (7 ),
49- URL : String ("http://path/to/image.jpg" ),
50- },
51- Widget {
52- Type : String ("timeseries" ),
53- TileDef : & TileDef {
54- Precision : Strint ("42" ),
14+ tests := []struct {
15+ file string
16+ want * Screenboard
17+ }{
18+ {
19+ file : "screenboard_response" ,
20+ want : & Screenboard {
21+ Id : Int (6334 ),
22+ Title : String ("dogapi test" ),
23+ Height : Strint ("768" ),
24+ Width : Strint ("1024" ),
25+ ReadOnly : Bool (false ),
26+ Widgets : []Widget {
27+ Widget {
28+ Type : String ("image" ),
29+ Height : Int (20 ),
30+ Width : Int (32 ),
31+ X : Int (32 ),
32+ Y : Int (7 ),
33+ URL : String ("http://path/to/image.jpg" ),
34+ },
35+ Widget {
36+ Type : String ("timeseries" ),
37+ TileDef : & TileDef {
38+ Precision : Strint ("42" ),
39+ },
40+ },
41+ Widget {
42+ Type : String ("timeseries" ),
43+ TileDef : & TileDef {
44+ Precision : Strint ("*" ),
45+ },
46+ },
5547 },
5648 },
57- Widget {
58- Type : String ("timeseries" ),
59- TileDef : & TileDef {
60- Precision : Strint ("*" ),
61- },
49+ },
50+ {
51+ file : "screenboard_response_fullscreen" ,
52+ want : & Screenboard {
53+ Id : Int (6335 ),
54+ Title : String ("dogapi fullscreen test" ),
55+ Height : Strint ("100%" ),
56+ Width : Strint ("100%" ),
57+ ReadOnly : Bool (false ),
6258 },
6359 },
6460 }
61+ for _ , tt := range tests {
62+ t .Run (tt .file , func (t * testing.T ) {
63+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
64+ response , err := ioutil .ReadFile ("./testdata/fixtures/" + tt .file + ".json" )
65+ if err != nil {
66+ // mustn't call t.Fatal from a different Goroutine
67+ http .Error (w , err .Error (), http .StatusInternalServerError )
68+ return
69+ }
70+ w .Write (response )
71+ }))
72+ defer ts .Close ()
73+
74+ datadogClient := Client {
75+ baseUrl : ts .URL ,
76+ HttpClient : ts .Client (),
77+ RetryTimeout : 5 * time .Second ,
78+ }
79+
80+ got , err := datadogClient .GetScreenboard (6334 )
81+ if err != nil {
82+ t .Fatal (err )
83+ }
6584
66- if ! reflect .DeepEqual (got , want ) {
67- // Go formatting doesn't handle pointers gracefully, so we'll display the diff as JSON
68- gotJSON , err := json .MarshalIndent (got , "" , "\t " )
69- if err != nil {
70- t .Fatal (err )
71- }
72- wantJSON , err := json .MarshalIndent (want , "" , "\t " )
73- if err != nil {
74- t .Fatal (err )
75- }
76- t .Errorf ("got:\n %s\n want:\n %s" , gotJSON , wantJSON )
85+ if ! reflect .DeepEqual (got , tt .want ) {
86+ // Go formatting doesn't handle pointers gracefully, so we'll display the diff as JSON
87+ gotJSON , err := json .MarshalIndent (got , "" , "\t " )
88+ if err != nil {
89+ t .Fatal (err )
90+ }
91+ wantJSON , err := json .MarshalIndent (tt .want , "" , "\t " )
92+ if err != nil {
93+ t .Fatal (err )
94+ }
95+ t .Errorf ("got:\n %s\n want:\n %s" , gotJSON , wantJSON )
96+ }
97+ })
7798 }
7899}
0 commit comments