2015-09-16 20:33:08 +00:00
package models_test
2015-05-22 21:00:51 +00:00
2015-05-28 21:47:52 +00:00
import (
"bytes"
2015-06-25 19:20:20 +00:00
"fmt"
"math"
2015-05-28 21:47:52 +00:00
"reflect"
2015-06-25 19:20:20 +00:00
"strconv"
2015-05-29 17:16:59 +00:00
"strings"
2015-05-28 21:47:52 +00:00
"testing"
"time"
2015-07-20 19:59:46 +00:00
2015-09-16 20:33:08 +00:00
"github.com/influxdb/influxdb/models"
2015-05-28 21:47:52 +00:00
)
2015-05-22 21:00:51 +00:00
2015-06-25 19:20:20 +00:00
var (
2015-09-16 20:33:08 +00:00
tags = models . Tags { "foo" : "bar" , "apple" : "orange" , "host" : "serverA" , "region" : "uswest" }
2015-06-25 19:20:20 +00:00
maxFloat64 = strconv . FormatFloat ( math . MaxFloat64 , 'f' , 1 , 64 )
minFloat64 = strconv . FormatFloat ( - math . MaxFloat64 , 'f' , 1 , 64 )
)
2015-05-22 21:00:51 +00:00
func TestMarshal ( t * testing . T ) {
2015-07-20 19:59:46 +00:00
got := tags . HashKey ( )
2015-05-28 21:47:52 +00:00
if exp := ",apple=orange,foo=bar,host=serverA,region=uswest" ; string ( got ) != exp {
2015-05-22 21:00:51 +00:00
t . Log ( "got: " , string ( got ) )
t . Log ( "exp: " , exp )
t . Error ( "invalid match" )
}
}
func BenchmarkMarshal ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
2015-07-20 19:59:46 +00:00
tags . HashKey ( )
2015-05-28 21:47:52 +00:00
}
}
func BenchmarkParsePointNoTags ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted2 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,host=serverA,region=us-west value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted5 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,env=prod,host=serverA,region=us-west,target=servers,zone=1c value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsSorted10 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,env=prod,host=serverA,region=us-west,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5,target=servers,zone=1c value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted2 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,region=us-west,host=serverA value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted5 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2015-05-29 17:16:59 +00:00
func BenchmarkParsePointsTagsUnSorted10 ( b * testing . B ) {
2015-07-31 01:51:18 +00:00
line := ` cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c,tag1=value1,tag2=value2,tag3=value3,tag4=value4,tag5=value5 value=1i 1000000000 `
2015-05-28 21:47:52 +00:00
for i := 0 ; i < b . N ; i ++ {
2015-09-16 20:33:08 +00:00
pt , _ := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
b . SetBytes ( int64 ( len ( line ) ) )
2015-05-29 17:16:59 +00:00
pt [ 0 ] . Key ( )
2015-05-28 21:47:52 +00:00
}
}
2015-09-16 20:33:08 +00:00
func test ( t * testing . T , line string , point models . Point ) {
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( line ) , time . Unix ( 0 , 0 ) , "n" )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , line , err )
}
if exp := 1 ; len ( pts ) != exp {
t . Fatalf ( ` ParsePoints("%s") len mismatch. got %d, exp %d ` , line , len ( pts ) , exp )
}
if exp := point . Key ( ) ; ! bytes . Equal ( pts [ 0 ] . Key ( ) , exp ) {
t . Errorf ( "ParsePoints(\"%s\") key mismatch.\ngot %v\nexp %v" , line , string ( pts [ 0 ] . Key ( ) ) , string ( exp ) )
}
if exp := len ( point . Tags ( ) ) ; len ( pts [ 0 ] . Tags ( ) ) != exp {
t . Errorf ( ` ParsePoints("%s") tags mismatch. got %v, exp %v ` , line , pts [ 0 ] . Tags ( ) , exp )
}
for tag , value := range point . Tags ( ) {
if pts [ 0 ] . Tags ( ) [ tag ] != value {
t . Errorf ( ` ParsePoints("%s") tags mismatch. got %v, exp %v ` , line , pts [ 0 ] . Tags ( ) [ tag ] , value )
}
}
for name , value := range point . Fields ( ) {
2015-07-06 16:18:07 +00:00
val := pts [ 0 ] . Fields ( ) [ name ]
expfval , ok := val . ( float64 )
if ok && math . IsNaN ( expfval ) {
gotfval , ok := value . ( float64 )
if ok && ! math . IsNaN ( gotfval ) {
t . Errorf ( ` ParsePoints("%s") field '%s' mismatch. exp NaN ` , line , name )
}
} else if ! reflect . DeepEqual ( pts [ 0 ] . Fields ( ) [ name ] , value ) {
2015-05-28 21:47:52 +00:00
t . Errorf ( ` ParsePoints("%s") field '%s' mismatch. got %v, exp %v ` , line , name , pts [ 0 ] . Fields ( ) [ name ] , value )
}
}
if ! pts [ 0 ] . Time ( ) . Equal ( point . Time ( ) ) {
t . Errorf ( ` ParsePoints("%s") time mismatch. got %v, exp %v ` , line , pts [ 0 ] . Time ( ) , point . Time ( ) )
}
2015-05-29 17:16:59 +00:00
if ! strings . HasPrefix ( pts [ 0 ] . String ( ) , line ) {
2015-05-28 21:47:52 +00:00
t . Errorf ( "ParsePoints string mismatch.\ngot: %v\nexp: %v" , pts [ 0 ] . String ( ) , line )
}
}
func TestParsePointNoValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( "" )
2015-05-29 17:16:59 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , "" , err )
}
if exp := 0 ; len ( pts ) != exp {
2015-08-05 22:41:59 +00:00
t . Errorf ( ` ParsePoints("%s") len mismatch. got %v, exp %v ` , "" , len ( pts ) , exp )
}
}
func TestParsePointWhitespaceValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( " " )
2015-08-05 22:41:59 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , "" , err )
}
if exp := 0 ; len ( pts ) != exp {
t . Errorf ( ` ParsePoints("%s") len mismatch. got %v, exp %v ` , "" , len ( pts ) , exp )
}
}
func TestParsePointSingleEquals ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( "=" )
2015-08-05 22:41:59 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. expected error ` , "=" )
}
if exp := 0 ; len ( pts ) != exp {
t . Errorf ( ` ParsePoints("%s") len mismatch. got %v, exp %v ` , "" , len ( pts ) , exp )
2015-05-28 21:47:52 +00:00
}
}
func TestParsePointNoFields ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( "cpu_load_short,host=server01,region=us-west" )
2015-07-21 01:17:28 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu_load_short,host=server01,region=us-west" )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( "cpu" )
2015-05-28 21:47:52 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu" )
}
2015-06-26 18:34:35 +00:00
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( "cpu," )
2015-06-26 18:34:35 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu," )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( "cpu, value=1" )
2015-06-26 18:34:35 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu, value=1" )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( "cpu,,, value=1" )
2015-06-26 18:34:35 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu,,, value=1" )
}
2015-05-28 21:47:52 +00:00
}
func TestParsePointNoTimestamp ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
test ( t , "cpu value=1" , models . NewPoint ( "cpu" , nil , nil , time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
func TestParsePointMissingQuote ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA value="test ` )
2015-05-28 21:47:52 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , "cpu" )
}
}
2015-06-26 18:34:35 +00:00
func TestParsePointMissingTagName ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverAa\,,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverAa\,,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA\,,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA\,,=us-east value=1i ` )
2015-06-26 18:34:35 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,\ =us-east value=1i ` )
2015-06-26 18:34:35 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,\ =us-east value=1i ` , err )
2015-06-26 18:34:35 +00:00
}
}
2015-05-28 21:47:52 +00:00
func TestParsePointMissingTagValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host value=1i ` )
2015-06-29 21:15:50 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host value=1i ` )
2015-06-29 21:15:50 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region value=1i ` )
2015-05-28 21:47:52 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region value=1i ` )
2015-06-26 18:34:35 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region= value=1i ` )
2015-06-26 18:34:35 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region= value=1i ` )
2015-05-28 21:47:52 +00:00
}
}
2015-06-26 18:02:46 +00:00
func TestParsePointMissingFieldName ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west = ` )
2015-06-26 18:02:46 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west = ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west =123i ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west =123i ` )
2015-06-26 18:02:46 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west a\ =123i ` )
2015-06-26 18:02:46 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west a\ =123i ` )
2015-06-26 18:02:46 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=123i,=456i ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=123i,=456i ` )
2015-06-26 18:02:46 +00:00
}
}
2015-06-11 17:25:25 +00:00
func TestParsePointMissingFieldValue ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value= ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-06-22 16:23:37 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value= ` )
2015-06-11 17:25:25 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value= 1000000000i ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value= 1000000000i ` )
2015-06-11 17:25:25 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=,value2=1i ` )
2015-06-11 17:25:25 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=,value2=1i ` )
2015-06-11 17:25:25 +00:00
}
2015-06-22 16:23:37 +00:00
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=server01,region=us-west 1434055562000000000i ` )
2015-06-22 16:23:37 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=server01,region=us-west 1434055562000000000i ` )
2015-06-22 16:23:37 +00:00
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=server01,region=us-west value=1i,b ` )
2015-06-26 18:02:46 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=server01,region=us-west value=1i,b ` )
2015-06-26 18:02:46 +00:00
}
2015-06-11 19:06:09 +00:00
}
func TestParsePointBadNumber ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1a ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=1a ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1ii ` )
2015-07-31 01:51:18 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=1ii ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.0i ` )
2015-07-31 01:55:08 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=1.0i ` )
}
2015-06-11 19:06:09 +00:00
}
2015-06-25 19:20:20 +00:00
func TestParsePointMaxInt64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9223372036854775808i ` )
2015-07-31 01:51:18 +00:00
exp := ` unable to parse 'cpu,host=serverA,region=us-west value=9223372036854775808i': unable to parse integer 9223372036854775808: strconv.ParseInt: parsing "9223372036854775808": value out of range `
if err == nil || ( err != nil && err . Error ( ) != exp ) {
t . Fatalf ( "Error mismatch:\nexp: %s\ngot: %v" , exp , err )
2015-06-25 19:20:20 +00:00
}
// max int
2015-09-16 20:33:08 +00:00
p , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9223372036854775807i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=9223372036854775807i ` , err )
}
if exp , got := int64 ( 9223372036854775807 ) , p [ 0 ] . Fields ( ) [ "value" ] . ( int64 ) ; exp != got {
t . Fatalf ( "ParsePoints Value mistmatch. \nexp: %v\ngot: %v" , exp , got )
2015-06-25 19:20:20 +00:00
}
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=0009223372036854775807i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Fatalf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=0009223372036854775807i ` , err )
2015-06-25 19:20:20 +00:00
}
}
func TestParsePointMinInt64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-9223372036854775809i ` )
2015-06-25 19:20:20 +00:00
if err == nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=-9223372036854775809i ` )
2015-06-25 19:20:20 +00:00
}
// min int
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-9223372036854775808i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-9223372036854775808i ` , err )
2015-06-25 19:20:20 +00:00
}
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-0009223372036854775808i ` )
2015-06-25 19:20:20 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-0009223372036854775808i ` , err )
2015-06-25 19:20:20 +00:00
}
}
func TestParsePointMaxFloat64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "1" + string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=... ` )
}
// max float
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=9223372036854775807 ` , err )
}
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "0000" + string ( maxFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=0009223372036854775807 ` , err )
}
}
func TestParsePointMinFloat64 ( t * testing . T ) {
// out of range
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "-1" + string ( minFloat64 ) [ 1 : ] ) )
2015-06-25 19:20:20 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=... ` )
}
// min float
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , string ( minFloat64 ) ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=... ` , err )
}
// leading zeros
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( fmt . Sprintf ( ` cpu,host=serverA,region=us-west value=%s ` , "-0000000" + string ( minFloat64 ) [ 1 : ] ) )
2015-06-25 19:20:20 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=... ` , err )
}
}
2015-06-11 19:06:09 +00:00
func TestParsePointNumberNonNumeric ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=.1a ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=.1a ` )
}
}
func TestParsePointNegativeWrongPlace ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=0.-1 ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=0.-1 ` )
}
}
2015-10-02 03:45:10 +00:00
func TestParsePointOnlyNegativeSign ( t * testing . T ) {
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=- ` )
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=- ` )
}
}
2015-06-11 19:06:09 +00:00
func TestParsePointFloatMultipleDecimals ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.1.1 ` )
2015-06-11 19:06:09 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=1.1.1 ` )
}
}
2015-06-11 17:25:25 +00:00
2015-06-11 19:06:09 +00:00
func TestParsePointInteger ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1i ` )
2015-06-11 19:06:09 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1i ` , err )
2015-06-11 19:06:09 +00:00
}
}
func TestParsePointNegativeInteger ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1i ` )
2015-06-11 19:06:09 +00:00
if err != nil {
2015-07-31 01:51:18 +00:00
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1i ` , err )
2015-06-11 19:06:09 +00:00
}
}
func TestParsePointNegativeFloat ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1.0 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1.0 ` , err )
}
}
func TestParsePointFloatNoLeadingDigit ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=.1 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1.0 ` , err )
}
}
func TestParsePointFloatScientific ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.0e4 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1.0e4 ` , err )
}
2015-07-10 20:18:51 +00:00
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1e4 ` )
2015-07-10 20:18:51 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1.0e4 ` , err )
}
if pts [ 0 ] . Fields ( ) [ "value" ] != 1e4 {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1e4 ` , err )
}
2015-06-11 19:06:09 +00:00
}
func TestParsePointFloatScientificDecimal ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=1.0e-4 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=1.0e-4 ` , err )
}
}
func TestParsePointFloatNegativeScientific ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=-1.0e-4 ` )
2015-06-11 19:06:09 +00:00
if err != nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got %v, exp nil ` , ` cpu,host=serverA,region=us-west value=-1.0e-4 ` , err )
}
2015-06-11 17:25:25 +00:00
}
2015-06-11 20:53:53 +00:00
func TestParsePointBooleanInvalid ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=a ` )
2015-06-11 20:53:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=a ` )
}
}
2015-08-10 19:46:53 +00:00
func TestParsePointScientificIntInvalid ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9ie10 ` )
2015-08-10 19:46:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=9ie10 ` )
}
2015-09-16 20:33:08 +00:00
_ , err = models . ParsePointsString ( ` cpu,host=serverA,region=us-west value=9e10i ` )
2015-08-10 19:46:53 +00:00
if err == nil {
t . Errorf ( ` ParsePoints("%s") mismatch. got nil, exp error ` , ` cpu,host=serverA,region=us-west value=9e10i ` )
}
}
2015-05-28 21:47:52 +00:00
func TestParsePointUnescape ( t * testing . T ) {
2015-07-31 01:51:18 +00:00
test ( t , ` foo\,bar value=1i ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-29 21:15:50 +00:00
"foo,bar" , // comma in the name
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-06-29 21:15:50 +00:00
"value" : 1 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-29 20:34:06 +00:00
// commas in measuremnt name
test ( t , ` cpu\,main,regions=east\,west value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-29 20:34:06 +00:00
"cpu,main" , // comma in the name
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-29 20:34:06 +00:00
"regions" : "east,west" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
// spaces in measurement name
test ( t , ` cpu\ load,region=east value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-29 20:34:06 +00:00
"cpu load" , // space in the name
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-29 20:34:06 +00:00
"region" : "east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
// commas in tag names
test ( t , ` cpu,region\,zone=east value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"region,zone" : "east" , // comma in the tag name
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
// spaces in tag names
test ( t , ` cpu,region\ zone=east value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"region zone" : "east" , // comma in the tag name
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
// commas in tag values
test ( t , ` cpu,regions=east\,west value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-28 21:47:52 +00:00
"regions" : "east,west" , // comma in the tag value
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
2015-05-29 20:34:06 +00:00
// spaces in tag values
test ( t , ` cpu,regions=east\ west value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"regions" : "east west" , // comma in the tag value
2015-05-28 21:47:52 +00:00
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
2015-05-29 20:34:06 +00:00
// commas in field names
test ( t , ` cpu,regions=east value\,ms=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value,ms" : 1.0 , // comma in the field name
} ,
time . Unix ( 0 , 0 ) ) )
// spaces in field names
test ( t , ` cpu,regions=east value\ ms=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value ms" : 1.0 , // comma in the field name
} ,
time . Unix ( 0 , 0 ) ) )
// commas in field values
test ( t , ` cpu,regions=east value="1,0" ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-29 20:34:06 +00:00
"regions" : "east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : "1,0" , // comma in the field value
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
// random character escaped
test ( t , ` cpu,regions=eas\t value=1.0 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-28 21:47:52 +00:00
"regions" : "eas\\t" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
} ,
2015-05-29 17:16:59 +00:00
time . Unix ( 0 , 0 ) ) )
2015-07-21 17:12:40 +00:00
// field name using escape char.
2015-07-31 01:51:18 +00:00
test ( t , ` cpu \a=1i ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-07-21 17:12:40 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-07-21 17:12:40 +00:00
"\\a" : 1 , // Left as parsed since it's not a known escape sequence.
} ,
time . Unix ( 0 , 0 ) ) )
2015-08-21 17:56:08 +00:00
// measurement, tag and tag value with equals
test ( t , ` cpu=load,equals\=foo=tag\=value value=1i ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-21 17:56:08 +00:00
"cpu=load" , // Not escaped
2015-09-16 20:33:08 +00:00
models . Tags {
2015-08-21 17:56:08 +00:00
"equals=foo" : "tag=value" , // Tag and value unescaped
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-21 17:56:08 +00:00
"value" : 1 ,
} ,
time . Unix ( 0 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
func TestParsePointWithTags ( t * testing . T ) {
test ( t ,
"cpu,host=serverA,region=us-east value=1.0 1000000000" ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags { "host" : "serverA" , "region" : "us-east" } ,
models . Fields { "value" : 1.0 } , time . Unix ( 1 , 0 ) ) )
2015-05-28 21:47:52 +00:00
}
func TestParsPointWithDuplicateTags ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
_ , err := models . ParsePoints ( [ ] byte ( ` cpu,host=serverA,host=serverB value=1i 1000000000 ` ) )
2015-05-28 21:47:52 +00:00
if err == nil {
t . Fatalf ( ` ParsePoint() expected error. got nil ` )
}
}
func TestParsePointWithStringField ( t * testing . T ) {
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo",str2="bar" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
"str" : "foo" ,
"str2" : "bar" ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu,host=serverA,region=us-east str="foo \" bar" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint ( "cpu" ,
models . Tags {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"str" : ` foo " bar ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
func TestParsePointWithStringWithSpaces ( t * testing . T ) {
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo bar" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-28 21:47:52 +00:00
"value" : 1.0 ,
"str" : "foo bar" , // spaces in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-05-29 20:34:06 +00:00
}
2015-05-28 21:47:52 +00:00
2015-08-05 20:12:19 +00:00
func TestParsePointWithStringWithNewline ( t * testing . T ) {
test ( t , "cpu,host=serverA,region=us-east value=1.0,str=\"foo\nbar\" 1000000000" ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-05 20:12:19 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-08-05 20:12:19 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-05 20:12:19 +00:00
"value" : 1.0 ,
"str" : "foo\nbar" , // newline in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-09 21:44:43 +00:00
func TestParsePointWithStringWithCommas ( t * testing . T ) {
// escaped comma
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo\,bar" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-09 21:44:43 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-06-09 21:44:43 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-09 21:44:43 +00:00
"value" : 1.0 ,
2015-06-22 18:22:03 +00:00
"str" : ` foo\,bar ` , // commas in string value
2015-06-09 21:44:43 +00:00
} ,
time . Unix ( 1 , 0 ) ) ,
)
// non-escaped comma
test ( t , ` cpu,host=serverA,region=us-east value=1.0,str="foo,bar" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-09 21:44:43 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-06-09 21:44:43 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-09 21:44:43 +00:00
"value" : 1.0 ,
"str" : "foo,bar" , // commas in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-08-18 16:24:03 +00:00
}
2015-06-09 21:44:43 +00:00
2015-08-18 16:24:03 +00:00
func TestParsePointQuotedMeasurement ( t * testing . T ) {
// non-escaped comma
test ( t , ` "cpu",host=serverA,region=us-east value=1.0 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-18 16:24:03 +00:00
` "cpu" ` ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-08-18 16:24:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-18 16:24:03 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-06-09 21:44:43 +00:00
}
2015-08-18 16:44:31 +00:00
func TestParsePointQuotedTags ( t * testing . T ) {
test ( t , ` cpu,"host"="serverA",region=us-east value=1.0 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-18 16:44:31 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-08-18 16:44:31 +00:00
` "host" ` : ` "serverA" ` ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-08-18 16:44:31 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-09-01 16:56:31 +00:00
func TestParsePointsUnbalancedQuotedTags ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsString ( "baz,mytag=\"a x=1 1441103862125\nbaz,mytag=a z=1 1441103862126" )
2015-09-01 16:56:31 +00:00
if err != nil {
t . Fatalf ( "ParsePoints failed: %v" , err )
}
if exp := 2 ; len ( pts ) != exp {
t . Fatalf ( "ParsePoints count mismatch. got %v, exp %v" , len ( pts ) , exp )
}
// Expected " in the tag value
2015-09-16 20:33:08 +00:00
exp := models . NewPoint ( "baz" , models . Tags { "mytag" : ` "a ` } ,
models . Fields { "x" : float64 ( 1 ) } , time . Unix ( 0 , 1441103862125 ) )
2015-09-01 16:56:31 +00:00
if pts [ 0 ] . String ( ) != exp . String ( ) {
t . Errorf ( "Point mismatch:\ngot: %v\nexp: %v" , pts [ 0 ] . String ( ) , exp . String ( ) )
}
// Expected two points to ensure we did not overscan the line
2015-09-16 20:33:08 +00:00
exp = models . NewPoint ( "baz" , models . Tags { "mytag" : ` a ` } ,
models . Fields { "z" : float64 ( 1 ) } , time . Unix ( 0 , 1441103862126 ) )
2015-09-01 16:56:31 +00:00
if pts [ 1 ] . String ( ) != exp . String ( ) {
t . Errorf ( "Point mismatch:\ngot: %v\nexp: %v" , pts [ 1 ] . String ( ) , exp . String ( ) )
}
}
2015-06-22 18:22:03 +00:00
func TestParsePointEscapedStringsAndCommas ( t * testing . T ) {
// non-escaped comma and quotes
test ( t , ` cpu,host=serverA,region=us-east value=" { Hello\" { ,}\" World}" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-22 18:22:03 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-06-22 18:22:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-22 18:22:03 +00:00
"value" : ` { Hello" { ,}" World} ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
// escaped comma and quotes
test ( t , ` cpu,host=serverA,region=us-east value=" { Hello\" { \,}\" World}" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-22 18:22:03 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-06-22 18:22:03 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-22 18:22:03 +00:00
"value" : ` { Hello" { \,}" World} ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-29 20:34:06 +00:00
func TestParsePointWithStringWithEquals ( t * testing . T ) {
2015-06-26 18:02:46 +00:00
test ( t , ` cpu,host=serverA,region=us-east str="foo=bar",value=1.0 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-29 20:34:06 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-29 20:34:06 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : 1.0 ,
"str" : "foo=bar" , // spaces in string value
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-05-28 21:47:52 +00:00
}
2015-08-19 15:27:17 +00:00
func TestParsePointWithStringWithBackslash ( t * testing . T ) {
test ( t , ` cpu value="test\\\"" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\\" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\ ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\\\"" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test\" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value="test\"" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-19 15:27:17 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-08-19 15:27:17 +00:00
"value" : ` test" ` ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-28 21:47:52 +00:00
func TestParsePointWithBoolField ( t * testing . T ) {
2015-07-13 17:27:24 +00:00
test ( t , ` cpu,host=serverA,region=us-east true=true,t=t,T=T,TRUE=TRUE,True=True,false=false,f=f,F=F,FALSE=FALSE,False=False 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-28 21:47:52 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-28 21:47:52 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-06-11 20:53:53 +00:00
"t" : true ,
"T" : true ,
"true" : true ,
2015-07-13 17:27:24 +00:00
"True" : true ,
2015-06-11 20:53:53 +00:00
"TRUE" : true ,
"f" : false ,
"F" : false ,
"false" : false ,
2015-07-13 17:27:24 +00:00
"False" : false ,
2015-06-11 20:53:53 +00:00
"FALSE" : false ,
2015-05-28 21:47:52 +00:00
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-05-29 20:34:06 +00:00
func TestParsePointUnicodeString ( t * testing . T ) {
test ( t , ` cpu,host=serverA,region=us-east value="wè" 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-05-29 20:34:06 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags {
2015-05-29 20:34:06 +00:00
"host" : "serverA" ,
"region" : "us-east" ,
} ,
2015-09-16 20:33:08 +00:00
models . Fields {
2015-05-29 20:34:06 +00:00
"value" : "wè" ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-07-31 01:51:18 +00:00
func TestNewPointFloatWithoutDecimal ( t * testing . T ) {
test ( t , ` cpu value=1 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-07-31 01:51:18 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-07-31 01:51:18 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-12 06:06:54 +00:00
func TestNewPointNegativeFloat ( t * testing . T ) {
test ( t , ` cpu value=-0.64 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-12 06:06:54 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-06-12 06:06:54 +00:00
"value" : - 0.64 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
func TestNewPointFloatNoDecimal ( t * testing . T ) {
test ( t , ` cpu value=1. 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-12 06:06:54 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-06-12 06:06:54 +00:00
"value" : 1.0 ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-06-12 17:08:11 +00:00
func TestNewPointFloatScientific ( t * testing . T ) {
test ( t , ` cpu value=6.632243e+06 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-12 17:08:11 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-06-12 17:08:11 +00:00
"value" : float64 ( 6632243 ) ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
func TestNewPointLargeInteger ( t * testing . T ) {
2015-07-31 01:51:18 +00:00
test ( t , ` cpu value=6632243i 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-06-12 17:08:11 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-06-12 17:08:11 +00:00
"value" : 6632243 , // if incorrectly encoded as a float, it would show up as 6.632243e+06
} ,
time . Unix ( 1 , 0 ) ) ,
)
}
2015-07-06 16:18:07 +00:00
func TestNewPointNaN ( t * testing . T ) {
test ( t , ` cpu value=NaN 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-07-06 16:18:07 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-07-06 16:18:07 +00:00
"value" : math . NaN ( ) ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
test ( t , ` cpu value=nAn 1000000000 ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-07-06 16:18:07 +00:00
"cpu" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-07-06 16:18:07 +00:00
"value" : math . NaN ( ) ,
} ,
time . Unix ( 1 , 0 ) ) ,
)
2015-08-21 18:12:48 +00:00
test ( t , ` nan value=NaN ` ,
2015-09-16 20:33:08 +00:00
models . NewPoint (
2015-08-21 18:12:48 +00:00
"nan" ,
2015-09-16 20:33:08 +00:00
models . Tags { } ,
models . Fields {
2015-08-21 18:12:48 +00:00
"value" : math . NaN ( ) ,
} ,
time . Unix ( 0 , 0 ) ) ,
)
2015-08-05 18:24:42 +00:00
}
func TestNewPointLargeNumberOfTags ( t * testing . T ) {
tags := ""
for i := 0 ; i < 255 ; i ++ {
tags += fmt . Sprintf ( ",tag%d=value%d" , i , i )
}
2015-07-06 16:18:07 +00:00
2015-09-16 20:33:08 +00:00
pt , err := models . ParsePointsString ( fmt . Sprintf ( "cpu%s value=1" , tags ) )
2015-08-05 18:24:42 +00:00
if err != nil {
t . Fatalf ( "ParsePoints() with max tags failed: %v" , err )
}
if len ( pt [ 0 ] . Tags ( ) ) != 255 {
t . Fatalf ( "ParsePoints() with max tags failed: %v" , err )
}
2015-07-06 16:18:07 +00:00
}
2015-05-28 21:47:52 +00:00
func TestParsePointIntsFloats ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( ` cpu,host=serverA,region=us-east int=10i,float=11.0,float2=12.1 1000000000 ` ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2015-05-28 21:47:52 +00:00
if _ , ok := pt . Fields ( ) [ "int" ] . ( int64 ) ; ! ok {
t . Errorf ( "ParsePoint() int field mismatch: got %T, exp %T" , pt . Fields ( ) [ "int" ] , int64 ( 10 ) )
}
if _ , ok := pt . Fields ( ) [ "float" ] . ( float64 ) ; ! ok {
t . Errorf ( "ParsePoint() float field mismatch: got %T, exp %T" , pt . Fields ( ) [ "float64" ] , float64 ( 11.0 ) )
}
if _ , ok := pt . Fields ( ) [ "float2" ] . ( float64 ) ; ! ok {
t . Errorf ( "ParsePoint() float field mismatch: got %T, exp %T" , pt . Fields ( ) [ "float64" ] , float64 ( 12.1 ) )
}
}
func TestParsePointKeyUnsorted ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( "cpu,last=1,first=2 value=1i" ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2015-05-28 21:47:52 +00:00
if exp := "cpu,first=2,last=1" ; string ( pt . Key ( ) ) != exp {
t . Errorf ( "ParsePoint key not sorted. got %v, exp %v" , pt . Key ( ) , exp )
}
}
func TestParsePointToString ( t * testing . T ) {
2015-08-25 13:49:49 +00:00
line := ` cpu,host=serverA,region=us-east bool=false,float=11,float2=12.123,int=10i,str="string val" 1000000000 `
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePoints ( [ ] byte ( line ) )
2015-05-28 21:47:52 +00:00
if err != nil {
t . Fatalf ( ` ParsePoints() failed. got %s ` , err )
}
2015-05-29 17:16:59 +00:00
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "ParsePoint() len mismatch: got %v, exp %v" , len ( pts ) , exp )
}
pt := pts [ 0 ]
2015-05-28 21:47:52 +00:00
got := pt . String ( )
if line != got {
t . Errorf ( "ParsePoint() to string mismatch:\n got %v\n exp %v" , got , line )
}
2015-09-16 20:33:08 +00:00
pt = models . NewPoint ( "cpu" , models . Tags { "host" : "serverA" , "region" : "us-east" } ,
models . Fields { "int" : 10 , "float" : float64 ( 11.0 ) , "float2" : float64 ( 12.123 ) , "bool" : false , "str" : "string val" } ,
2015-05-28 21:47:52 +00:00
time . Unix ( 1 , 0 ) )
got = pt . String ( )
if line != got {
t . Errorf ( "NewPoint() to string mismatch:\n got %v\n exp %v" , got , line )
2015-05-22 21:00:51 +00:00
}
}
2015-05-29 17:16:59 +00:00
func TestParsePointsWithPrecision ( t * testing . T ) {
2015-06-04 17:22:53 +00:00
tests := [ ] struct {
name string
line string
precision string
exp string
} {
{
name : "nanosecond by default" ,
line : ` cpu,host=serverA,region=us-east value=1.0 946730096789012345 ` ,
precision : "" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
} ,
{
name : "nanosecond" ,
line : ` cpu,host=serverA,region=us-east value=1.0 946730096789012345 ` ,
precision : "n" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
} ,
{
name : "microsecond" ,
line : ` cpu,host=serverA,region=us-east value=1.0 946730096789012 ` ,
precision : "u" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012000" ,
} ,
{
name : "millisecond" ,
line : ` cpu,host=serverA,region=us-east value=1.0 946730096789 ` ,
precision : "ms" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789000000" ,
} ,
{
name : "second" ,
line : ` cpu,host=serverA,region=us-east value=1.0 946730096 ` ,
precision : "s" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096000000000" ,
} ,
{
name : "minute" ,
line : ` cpu,host=serverA,region=us-east value=1.0 15778834 ` ,
precision : "m" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730040000000000" ,
} ,
{
name : "hour" ,
line : ` cpu,host=serverA,region=us-east value=1.0 262980 ` ,
precision : "h" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946728000000000000" ,
} ,
}
for _ , test := range tests {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( test . line ) , time . Now ( ) . UTC ( ) , test . precision )
2015-06-04 17:22:53 +00:00
if err != nil {
t . Fatalf ( ` %s: ParsePoints() failed. got %s ` , test . name , err )
}
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "%s: ParsePoint() len mismatch: got %v, exp %v" , test . name , len ( pts ) , exp )
}
pt := pts [ 0 ]
got := pt . String ( )
if got != test . exp {
t . Errorf ( "%s: ParsePoint() to string mismatch:\n got %v\n exp %v" , test . name , got , test . exp )
}
2015-05-29 17:16:59 +00:00
}
2015-06-04 17:22:53 +00:00
}
2015-05-29 17:16:59 +00:00
2015-06-04 17:22:53 +00:00
func TestParsePointsWithPrecisionNoTime ( t * testing . T ) {
line := ` cpu,host=serverA,region=us-east value=1.0 `
tm , _ := time . Parse ( time . RFC3339Nano , "2000-01-01T12:34:56.789012345Z" )
tests := [ ] struct {
name string
precision string
exp string
} {
{
name : "no precision" ,
precision : "" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
} ,
{
name : "nanosecond precision" ,
precision : "n" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
} ,
{
name : "microsecond precision" ,
precision : "u" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012000" ,
} ,
{
name : "millisecond precision" ,
precision : "ms" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789000000" ,
} ,
{
name : "second precision" ,
precision : "s" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096000000000" ,
} ,
{
name : "minute precision" ,
precision : "m" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730040000000000" ,
} ,
{
name : "hour precision" ,
precision : "h" ,
exp : "cpu,host=serverA,region=us-east value=1.0 946728000000000000" ,
} ,
}
for _ , test := range tests {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( line ) , tm , test . precision )
2015-06-04 17:22:53 +00:00
if err != nil {
t . Fatalf ( ` %s: ParsePoints() failed. got %s ` , test . name , err )
}
if exp := 1 ; len ( pts ) != exp {
t . Errorf ( "%s: ParsePoint() len mismatch: got %v, exp %v" , test . name , len ( pts ) , exp )
}
pt := pts [ 0 ]
got := pt . String ( )
if got != test . exp {
t . Errorf ( "%s: ParsePoint() to string mismatch:\n got %v\n exp %v" , test . name , got , test . exp )
}
2015-05-29 17:16:59 +00:00
}
}
2015-05-29 20:34:06 +00:00
2015-06-18 18:59:20 +00:00
func TestParsePointsWithPrecisionComments ( t * testing . T ) {
tests := [ ] struct {
name string
batch string
exp string
lenPoints int
} {
{
name : "comment only" ,
batch : ` # comment only ` ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
lenPoints : 0 ,
} ,
{
name : "point with comment above" ,
batch : ` # a point is below
cpu , host = serverA , region = us - east value = 1.0 946730096789012345 ` ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
lenPoints : 1 ,
} ,
{
name : "point with comment below" ,
batch : ` cpu , host = serverA , region = us - east value = 1.0 946730096789012345
# end of points ` ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
lenPoints : 1 ,
} ,
{
name : "indented comment" ,
batch : ` # a point is below
cpu , host = serverA , region = us - east value = 1.0 946730096789012345 ` ,
exp : "cpu,host=serverA,region=us-east value=1.0 946730096789012345" ,
lenPoints : 1 ,
} ,
}
for _ , test := range tests {
2015-09-16 20:33:08 +00:00
pts , err := models . ParsePointsWithPrecision ( [ ] byte ( test . batch ) , time . Now ( ) . UTC ( ) , "" )
2015-06-18 18:59:20 +00:00
if err != nil {
t . Fatalf ( ` %s: ParsePoints() failed. got %s ` , test . name , err )
}
pointsLength := len ( pts )
if exp := test . lenPoints ; pointsLength != exp {
t . Errorf ( "%s: ParsePoint() len mismatch: got %v, exp %v" , test . name , pointsLength , exp )
}
if pointsLength > 0 {
pt := pts [ 0 ]
got := pt . String ( )
if got != test . exp {
t . Errorf ( "%s: ParsePoint() to string mismatch:\n got %v\n exp %v" , test . name , got , test . exp )
}
}
}
}
2015-05-29 20:34:06 +00:00
func TestNewPointEscaped ( t * testing . T ) {
// commas
2015-09-16 20:33:08 +00:00
pt := models . NewPoint ( "cpu,main" , models . Tags { "tag,bar" : "value" } , models . Fields { "name,bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu\,main,tag\,bar=value name\,bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
// spaces
2015-09-16 20:33:08 +00:00
pt = models . NewPoint ( "cpu main" , models . Tags { "tag bar" : "value" } , models . Fields { "name bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu\ main,tag\ bar=value name\ bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
// equals
2015-09-16 20:33:08 +00:00
pt = models . NewPoint ( "cpu=main" , models . Tags { "tag=bar" : "value=foo" } , models . Fields { "name=bar" : 1.0 } , time . Unix ( 0 , 0 ) )
2015-08-25 13:49:49 +00:00
if exp := ` cpu=main,tag\=bar=value\=foo name\=bar=1 0 ` ; pt . String ( ) != exp {
2015-05-29 20:34:06 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
}
2015-07-06 22:03:03 +00:00
func TestNewPointUnhandledType ( t * testing . T ) {
// nil value
2015-09-16 20:33:08 +00:00
pt := models . NewPoint ( "cpu" , nil , models . Fields { "value" : nil } , time . Unix ( 0 , 0 ) )
2015-07-06 22:03:03 +00:00
if exp := ` cpu value= 0 ` ; pt . String ( ) != exp {
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
// unsupported type gets stored as string
2015-07-07 03:11:09 +00:00
now := time . Unix ( 0 , 0 ) . UTC ( )
2015-09-16 20:33:08 +00:00
pt = models . NewPoint ( "cpu" , nil , models . Fields { "value" : now } , time . Unix ( 0 , 0 ) )
2015-07-07 03:11:09 +00:00
if exp := ` cpu value="1970-01-01 00:00:00 +0000 UTC" 0 ` ; pt . String ( ) != exp {
2015-07-06 22:03:03 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
2015-07-07 03:11:09 +00:00
if exp := "1970-01-01 00:00:00 +0000 UTC" ; pt . Fields ( ) [ "value" ] != exp {
2015-07-06 22:03:03 +00:00
t . Errorf ( "NewPoint().String() mismatch.\ngot %v\nexp %v" , pt . String ( ) , exp )
}
}
2015-08-18 16:02:48 +00:00
func TestMakeKeyEscaped ( t * testing . T ) {
2015-09-16 20:33:08 +00:00
if exp , got := ` cpu\ load ` , models . MakeKey ( [ ] byte ( ` cpu\ load ` ) , models . Tags { } ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2015-09-16 20:33:08 +00:00
if exp , got := ` cpu\ load ` , models . MakeKey ( [ ] byte ( ` cpu load ` ) , models . Tags { } ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2015-09-16 20:33:08 +00:00
if exp , got := ` cpu\,load ` , models . MakeKey ( [ ] byte ( ` cpu\,load ` ) , models . Tags { } ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
2015-09-16 20:33:08 +00:00
if exp , got := ` cpu\,load ` , models . MakeKey ( [ ] byte ( ` cpu,load ` ) , models . Tags { } ) ; string ( got ) != exp {
2015-08-18 16:02:48 +00:00
t . Errorf ( "MakeKey() mismatch.\ngot %v\nexp %v" , got , exp )
}
}
2015-09-15 17:59:52 +00:00
func TestPrecisionString ( t * testing . T ) {
tags := map [ string ] interface { } { "value" : float64 ( 1 ) }
tm , _ := time . Parse ( time . RFC3339Nano , "2000-01-01T12:34:56.789012345Z" )
tests := [ ] struct {
name string
precision string
exp string
} {
{
name : "no precision" ,
precision : "" ,
exp : "cpu value=1 946730096789012345" ,
} ,
{
name : "nanosecond precision" ,
precision : "ns" ,
exp : "cpu value=1 946730096789012345" ,
} ,
{
name : "microsecond precision" ,
precision : "u" ,
exp : "cpu value=1 946730096789012" ,
} ,
{
name : "millisecond precision" ,
precision : "ms" ,
exp : "cpu value=1 946730096789" ,
} ,
{
name : "second precision" ,
precision : "s" ,
exp : "cpu value=1 946730096" ,
} ,
{
name : "minute precision" ,
precision : "m" ,
exp : "cpu value=1 15778834" ,
} ,
{
name : "hour precision" ,
precision : "h" ,
exp : "cpu value=1 262980" ,
} ,
}
for _ , test := range tests {
2015-09-16 20:33:08 +00:00
pt := models . NewPoint ( "cpu" , nil , tags , tm )
2015-09-15 17:59:52 +00:00
act := pt . PrecisionString ( test . precision )
if act != test . exp {
t . Errorf ( "%s: PrecisionString() mismatch:\n actual: %v\n exp: %v" ,
test . name , act , test . exp )
}
}
}