Skip to content

Commit

Permalink
Add tests for marshal/unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksontj committed May 16, 2019
1 parent 66d0987 commit 2c03b23
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/prometheus/v1/api.go
Expand Up @@ -36,12 +36,16 @@ func init() {
}

func unMarshalPointJSON(ptr unsafe.Pointer, iter *json.Iterator) {
p := *((*model.SamplePair)(ptr))
p := (*model.SamplePair)(ptr)
if !iter.ReadArray() {
iter.ReportError("unmarshal model.SamplePair", "value must be an array")
return
}
p.Timestamp = model.Time(iter.ReadFloat64())
t := iter.ReadNumber()
if err := p.Timestamp.UnmarshalJSON([]byte(t)); err != nil {
iter.ReportError("unmarshal model.SamplePair", err.Error())
return
}
if !iter.ReadArray() {
iter.ReportError("unmarshal model.SamplePair", "SamplePair must be [timestamp, value]")
return
Expand Down
90 changes: 90 additions & 0 deletions api/prometheus/v1/api_test.go
Expand Up @@ -17,16 +17,19 @@ import (
"context"
"errors"
"fmt"
"math"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"testing"
"time"

json "github.com/json-iterator/go"

"github.com/prometheus/common/model"
"github.com/prometheus/tsdb/testutil"
)

type apiTest struct {
Expand Down Expand Up @@ -873,3 +876,90 @@ func TestAPIClientDo(t *testing.T) {

}
}

func TestSamplePair(t *testing.T) {
tests := []struct {
point model.SamplePair
expected string
}{
{
point: model.SamplePair{0, 0},
expected: `[0,"0"]`,
},
{
point: model.SamplePair{1, 20},
expected: `[0.001,"20"]`,
},
{
point: model.SamplePair{10, 20},
expected: `[0.010,"20"]`,
},
{
point: model.SamplePair{100, 20},
expected: `[0.100,"20"]`,
},
{
point: model.SamplePair{1001, 20},
expected: `[1.001,"20"]`,
},
{
point: model.SamplePair{1010, 20},
expected: `[1.010,"20"]`,
},
{
point: model.SamplePair{1100, 20},
expected: `[1.100,"20"]`,
},
{
point: model.SamplePair{12345678123456555, 20},
expected: `[12345678123456.555,"20"]`,
},
{
point: model.SamplePair{-1, 20},
expected: `[-0.001,"20"]`,
},
{
point: model.SamplePair{0, model.SampleValue(math.NaN())},
expected: `[0,"NaN"]`,
},
{
point: model.SamplePair{0, model.SampleValue(math.Inf(1))},
expected: `[0,"+Inf"]`,
},
{
point: model.SamplePair{0, model.SampleValue(math.Inf(-1))},
expected: `[0,"-Inf"]`,
},
{
point: model.SamplePair{0, model.SampleValue(1.2345678e6)},
expected: `[0,"1234567.8"]`,
},
{
point: model.SamplePair{0, 1.2345678e-6},
expected: `[0,"0.0000012345678"]`,
},
{
point: model.SamplePair{0, 1.2345678e-67},
expected: `[0,"1.2345678e-67"]`,
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, err := json.Marshal(test.point)
testutil.Ok(t, err)
testutil.Equals(t, string(b), test.expected, "mismatch marshal")

// To test Unmarshal we will Unmarshal then re-Marshal this way we
// can do a string compare, otherwise Nan values don't show equivalence
// properly.
var sp model.SamplePair
err = json.Unmarshal(b, &sp)
testutil.Ok(t, err)

b, err = json.Marshal(sp)
testutil.Ok(t, err)
testutil.Equals(t, string(b), test.expected, "mismatch unmarshal")
})
}
}

0 comments on commit 2c03b23

Please sign in to comment.