Skip to content

Commit

Permalink
Add get a screenboard api test
Browse files Browse the repository at this point in the history
  • Loading branch information
teraken0509 committed Oct 31, 2018
1 parent dd6f675 commit 643a7df
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
99 changes: 99 additions & 0 deletions screenboards_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package datadog

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)

func TestGetScreenboard(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := ioutil.ReadFile("./tests/fixtures/screenboard_response.json")
if err != nil {
t.Fatal(err)
}
w.Write(response)
}))
defer ts.Close()

datadogClient := Client{
baseUrl: ts.URL,
HttpClient: http.DefaultClient,
}

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

expectedID := 6334
if id := screenboard.GetId(); id != expectedID {
t.Fatalf("expect ID %d. Got %d", expectedID, id)
}

expectedTitle := "dogapi test"
if title := screenboard.GetTitle(); title != expectedTitle {
t.Fatalf("expect title %s. Got %s", expectedTitle, title)
}

expectedHeight := 768
if height := screenboard.GetHeight(); height != expectedHeight {
t.Fatalf("expect height %d. Got %d", expectedHeight, height)
}

expectedWidth := 1024
if width := screenboard.GetWidth(); width != expectedWidth {
t.Fatalf("expect width %d. Got %d", expectedWidth, width)
}

expectedReadOnly := false
readOnly, ok := screenboard.GetReadOnlyOk()
if !ok {
t.Fatalf("expect to have a read_only field")
}

if readOnly != expectedReadOnly {
t.Fatalf("expect read_only %v. Got %v", expectedReadOnly, readOnly)
}

widgets, ok := screenboard.GetWidgetsByOk()
if !ok {
t.Fatal("expect widgets field to exist")
}
for _, widget := range widgets {
validateWidget(t, widget)
}
}

func validateWidget(t *testing.T, wd Widget) {
expectedType := "image"
if widgetType := wd.GetType(); widgetType != expectedType {
t.Fatalf("expect type %s. Got %s", expectedType, widgetType)
}

expectedHeight := 20
if height := wd.GetHeight(); height != expectedHeight {
t.Fatalf("expect height %d. Got %d", expectedHeight, height)
}

expectedWidth := 32
if width := wd.GetWidth(); width != expectedWidth {
t.Fatalf("expect width %d. Got %d", expectedWidth, width)
}

expectedX := 32
if x := wd.GetX(); x != expectedX {
t.Fatalf("expect x %d. Got %d", expectedX, x)
}

expectedY := 7
if y := wd.GetY(); y != expectedY {
t.Fatalf("expect y %d. Got %d", expectedY, y)
}

expectedURL := "http://path/to/image.jpg"
if url := wd.GetURL(); url != expectedURL {
t.Fatalf("expect url %s. Got %s", expectedURL, url)
}
}
19 changes: 19 additions & 0 deletions tests/fixtures/screenboard_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"board_title": "dogapi test",
"height": 768,
"id": 6334,
"widgets": [
{
"height": 20,
"type": "image",
"url": "http://path/to/image.jpg",
"width": 32,
"x": 32,
"y": 7
}
],
"width": 1024,
"created": "2015-12-17T23:06:06.703087+00:00",
"modified": "2015-12-17T23:12:26.726234+00:00",
"read_only": false
}

0 comments on commit 643a7df

Please sign in to comment.