Skip to content

Commit

Permalink
Accepts ints and strings for Screenboard size.
Browse files Browse the repository at this point in the history
Besides integer sizes in pixel (e.g. 768) the API can also return percentages
(e.g. "100%").

Fixes #192
  • Loading branch information
mrwonko committed Mar 1, 2019
1 parent 5e411c0 commit 088d2e4
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 74 deletions.
20 changes: 10 additions & 10 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6400,18 +6400,18 @@ func (r *Rule) SetTimeframe(v string) {
}

// GetHeight returns the Height field if non-nil, zero value otherwise.
func (s *Screenboard) GetHeight() int {
func (s *Screenboard) GetHeight() StrintD {
if s == nil || s.Height == nil {
return 0
return ""
}
return *s.Height
}

// GetHeightOk returns a tuple with the Height field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (s *Screenboard) GetHeightOk() (int, bool) {
func (s *Screenboard) GetHeightOk() (StrintD, bool) {
if s == nil || s.Height == nil {
return 0, false
return "", false
}
return *s.Height, true
}
Expand All @@ -6426,7 +6426,7 @@ func (s *Screenboard) HasHeight() bool {
}

// SetHeight allocates a new s.Height and returns the pointer to it.
func (s *Screenboard) SetHeight(v int) {
func (s *Screenboard) SetHeight(v StrintD) {
s.Height = &v
}

Expand Down Expand Up @@ -6555,18 +6555,18 @@ func (s *Screenboard) SetTitle(v string) {
}

// GetWidth returns the Width field if non-nil, zero value otherwise.
func (s *Screenboard) GetWidth() int {
func (s *Screenboard) GetWidth() StrintD {
if s == nil || s.Width == nil {
return 0
return ""
}
return *s.Width
}

// GetWidthOk returns a tuple with the Width field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (s *Screenboard) GetWidthOk() (int, bool) {
func (s *Screenboard) GetWidthOk() (StrintD, bool) {
if s == nil || s.Width == nil {
return 0, false
return "", false
}
return *s.Width, true
}
Expand All @@ -6581,7 +6581,7 @@ func (s *Screenboard) HasWidth() bool {
}

// SetWidth allocates a new s.Width and returns the pointer to it.
func (s *Screenboard) SetWidth(v int) {
func (s *Screenboard) SetWidth(v StrintD) {
s.Width = &v
}

Expand Down
10 changes: 5 additions & 5 deletions integration/screenboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package integration
import (
"testing"

"github.com/zorkian/go-datadog-api"
datadog "github.com/zorkian/go-datadog-api"
)

func TestScreenboardCreateAndDelete(t *testing.T) {
Expand Down Expand Up @@ -93,8 +93,8 @@ func TestScreenboardGet(t *testing.T) {
func getTestScreenboard() *datadog.Screenboard {
return &datadog.Screenboard{
Title: datadog.String("___Test-Board___"),
Height: datadog.Int(600),
Width: datadog.Int(800),
Height: datadog.Strint("600"),
Width: datadog.Strint("800"),
Widgets: []datadog.Widget{},
}
}
Expand Down Expand Up @@ -129,10 +129,10 @@ func assertScreenboardEquals(t *testing.T, actual, expected *datadog.Screenboard
t.Errorf("Screenboard title does not match: %s != %s", *actual.Title, *expected.Title)
}
if *actual.Width != *expected.Width {
t.Errorf("Screenboard width does not match: %d != %d", *actual.Width, *expected.Width)
t.Errorf("Screenboard width does not match: %s != %s", *actual.Width, *expected.Width)
}
if *actual.Height != *expected.Height {
t.Errorf("Screenboard width does not match: %d != %d", *actual.Height, *expected.Height)
t.Errorf("Screenboard width does not match: %s != %s", *actual.Height, *expected.Height)
}
if len(actual.Widgets) != len(expected.Widgets) {
t.Errorf("Number of Screenboard widgets does not match: %d != %d", len(actual.Widgets), len(expected.Widgets))
Expand Down
4 changes: 2 additions & 2 deletions screenboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
type Screenboard struct {
Id *int `json:"id,omitempty"`
Title *string `json:"board_title,omitempty"`
Height *int `json:"height,omitempty"`
Width *int `json:"width,omitempty"`
Height *StrintD `json:"height,omitempty"`
Width *StrintD `json:"width,omitempty"`
Shared *bool `json:"shared,omitempty"`
TemplateVariables []TemplateVariable `json:"template_variables,omitempty"`
Widgets []Widget `json:"widgets"`
Expand Down
135 changes: 78 additions & 57 deletions screenboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,89 @@ import (
)

func TestGetScreenboard(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := ioutil.ReadFile("./testdata/fixtures/screenboard_response.json")
if err != nil {
// mustn't call t.Fatal from a different Goroutine
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}))
defer ts.Close()

datadogClient := Client{
baseUrl: ts.URL,
HttpClient: ts.Client(),
RetryTimeout: 5 * time.Second,
}

got, err := datadogClient.GetScreenboard(6334)
if err != nil {
t.Fatal(err)
}

want := &Screenboard{
Id: Int(6334),
Title: String("dogapi test"),
Height: Int(768),
Width: Int(1024),
ReadOnly: Bool(false),
Widgets: []Widget{
Widget{
Type: String("image"),
Height: Int(20),
Width: Int(32),
X: Int(32),
Y: Int(7),
URL: String("http://path/to/image.jpg"),
},
Widget{
Type: String("timeseries"),
TileDef: &TileDef{
Precision: Strint("42"),
tests := []struct {
file string
want *Screenboard
}{
{
file: "screenboard_response",
want: &Screenboard{
Id: Int(6334),
Title: String("dogapi test"),
Height: Strint("768"),
Width: Strint("1024"),
ReadOnly: Bool(false),
Widgets: []Widget{
Widget{
Type: String("image"),
Height: Int(20),
Width: Int(32),
X: Int(32),
Y: Int(7),
URL: String("http://path/to/image.jpg"),
},
Widget{
Type: String("timeseries"),
TileDef: &TileDef{
Precision: Strint("42"),
},
},
Widget{
Type: String("timeseries"),
TileDef: &TileDef{
Precision: Strint("*"),
},
},
},
},
Widget{
Type: String("timeseries"),
TileDef: &TileDef{
Precision: Strint("*"),
},
},
{
file: "screenboard_response_fullscreen",
want: &Screenboard{
Id: Int(6335),
Title: String("dogapi fullscreen test"),
Height: Strint("100%"),
Width: Strint("100%"),
ReadOnly: Bool(false),
},
},
}
for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := ioutil.ReadFile("./testdata/fixtures/" + tt.file + ".json")
if err != nil {
// mustn't call t.Fatal from a different Goroutine
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(response)
}))
defer ts.Close()

datadogClient := Client{
baseUrl: ts.URL,
HttpClient: ts.Client(),
RetryTimeout: 5 * time.Second,
}

got, err := datadogClient.GetScreenboard(6334)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(got, want) {
// Go formatting doesn't handle pointers gracefully, so we'll display the diff as JSON
gotJSON, err := json.MarshalIndent(got, "", "\t")
if err != nil {
t.Fatal(err)
}
wantJSON, err := json.MarshalIndent(want, "", "\t")
if err != nil {
t.Fatal(err)
}
t.Errorf("got:\n%s\nwant:\n%s", gotJSON, wantJSON)
if !reflect.DeepEqual(got, tt.want) {
// Go formatting doesn't handle pointers gracefully, so we'll display the diff as JSON
gotJSON, err := json.MarshalIndent(got, "", "\t")
if err != nil {
t.Fatal(err)
}
wantJSON, err := json.MarshalIndent(tt.want, "", "\t")
if err != nil {
t.Fatal(err)
}
t.Errorf("got:\n%s\nwant:\n%s", gotJSON, wantJSON)
}
})
}
}
9 changes: 9 additions & 0 deletions testdata/fixtures/screenboard_response_fullscreen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"board_title": "dogapi fullscreen test",
"id": 6335,
"height": "100%",
"width": "100%",
"created": "2019-03-01T13:37:00.420042+01:00",
"modified": "2019-03-01T15:25:26.726234+01:00",
"read_only": false
}

0 comments on commit 088d2e4

Please sign in to comment.