-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboards: Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal (#167
) * Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal * Add in the AutoMin and AutoMax fields for the Yaxis struct so we can work around the times when datadog's API returns a string when we expect a float64. * Add a custom UnmarshalJSON for Yaxis to utilize the new AutoMin and AutoMax fields. * Update custom Yaxis UnmarshalJSON and add unittests
- Loading branch information
1 parent
d7b8b10
commit 47fbc01
Showing
2 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package datadog | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type Alias Yaxis | ||
|
||
type YAxisTestSuite struct { | ||
suite.Suite | ||
yJSON []byte | ||
yMarshalledJSON []byte | ||
y Yaxis | ||
yAutoMinMaxJSON []byte | ||
yAutoMinMaxMarshalledJSON []byte | ||
yAutoMinMax Yaxis | ||
} | ||
|
||
func (suite *YAxisTestSuite) SetupTest() { | ||
// Custom Y.Min, Y.Max | ||
suite.yJSON = []byte(`{"min":0,"max":1,"scale":"linear"}`) | ||
suite.yMarshalledJSON = suite.yJSON | ||
yMinFloat := float64(0) | ||
yMaxFloat := float64(1) | ||
yScale := "linear" | ||
suite.y = Yaxis{ | ||
Min: &yMinFloat, | ||
AutoMin: false, | ||
Max: &yMaxFloat, | ||
AutoMax: false, | ||
Scale: &yScale, | ||
} | ||
// Auto Y.Min, Y.Max | ||
suite.yAutoMinMaxJSON = []byte(`{"min":"auto","max":"auto","scale":"linear"}`) | ||
suite.yAutoMinMaxMarshalledJSON = []byte(`{"scale":"linear"}`) | ||
suite.yAutoMinMax = Yaxis{ | ||
Min: nil, | ||
AutoMin: true, | ||
Max: nil, | ||
AutoMax: true, | ||
Scale: &yScale, | ||
} | ||
} | ||
|
||
func (suite *YAxisTestSuite) TestYAxisJSONMarshalCustomMinMax() { | ||
jsonData, err := json.Marshal(suite.y) | ||
assert.Nil(suite.T(), err) | ||
assert.Equal(suite.T(), suite.yMarshalledJSON, jsonData) | ||
} | ||
|
||
func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalCustomMinMax() { | ||
var res Yaxis | ||
err := json.Unmarshal(suite.yJSON, &res) | ||
assert.Nil(suite.T(), err) | ||
assert.Equal(suite.T(), suite.y, res) | ||
} | ||
|
||
func (suite *YAxisTestSuite) TestYAxisJSONMarshalAutoMinMax() { | ||
jsonData, err := json.Marshal(suite.yAutoMinMax) | ||
assert.Nil(suite.T(), err) | ||
assert.Equal(suite.T(), suite.yAutoMinMaxMarshalledJSON, jsonData) | ||
} | ||
|
||
func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalAutoMinMax() { | ||
var res Yaxis | ||
err := json.Unmarshal(suite.yAutoMinMaxJSON, &res) | ||
assert.Nil(suite.T(), err) | ||
assert.Equal(suite.T(), suite.yAutoMinMax, res) | ||
} | ||
|
||
func TestYAxisTestSuite(t *testing.T) { | ||
suite.Run(t, new(YAxisTestSuite)) | ||
} |