forked from cbrake/influxdbhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
121 lines (98 loc) · 2.56 KB
/
encode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package influxdbhelper
import (
"reflect"
"testing"
"time"
)
func TestEncodeDataNotStruct(t *testing.T) {
_, _, _, _, err := encode([]int{1, 2, 3}, nil)
if err == nil {
t.Error("Expected error")
}
}
func TestEncodeSetsMesurment(t *testing.T) {
type MyType struct {
Val string `influx:"val"`
}
d := &MyType{"test-data"}
_, _, _, measurement, err := encode(d, nil)
if err != nil {
t.Error("Error encoding: ", err)
}
if measurement != "MyType" {
t.Errorf("%v != %v", measurement, "MyType")
}
}
func TestEncodeUsesTimeField(t *testing.T) {
type MyType struct {
MyTimeField time.Time `influx:"my_time_field"`
Val string `influx:"val"`
}
td, _ := time.Parse(time.RFC822, "27 Oct 78 15:04 PST")
d := &MyType{td, "test-data"}
tv, _, _, _, err := encode(d, &usingValue{"my_time_field", false})
if tv != td {
t.Error("Did not properly use the time field specified")
}
if err != nil {
t.Error("Error encoding: ", err)
}
}
func TestEncode(t *testing.T) {
type MyType struct {
InfluxMeasurement Measurement
Time time.Time `influx:"time"`
TagValue string `influx:"tagValue,tag"`
TagAndFieldValue string `influx:"tagAndFieldValue,tag,field"`
IntValue int `influx:"intValue"`
FloatValue float64 `influx:"floatValue"`
BoolValue bool `influx:"boolValue"`
StringValue string `influx:"stringValue"`
StructFieldName string `influx:""`
IgnoredValue string `influx:"-"`
}
d := MyType{
"test",
time.Now(),
"tag-value",
"tag-and-field-value",
10,
10.5,
true,
"string",
"struct-field",
"ignored",
}
timeExp := d.Time
tagsExp := map[string]string{
"tagValue": "tag-value",
"tagAndFieldValue": "tag-and-field-value",
}
fieldsExp := map[string]interface{}{
"tagAndFieldValue": d.TagAndFieldValue,
"intValue": d.IntValue,
"floatValue": d.FloatValue,
"boolValue": d.BoolValue,
"stringValue": d.StringValue,
"StructFieldName": d.StructFieldName,
}
tm, tags, fields, measurement, err := encode(d, nil)
if err != nil {
t.Error("Error encoding: ", err)
}
if measurement != d.InfluxMeasurement {
t.Errorf("%v != %v", measurement, d.InfluxMeasurement)
}
if _, ok := fields["InfluxMeasurement"]; ok {
t.Errorf("Found InfluxMeasurement in the fields!")
}
if !tm.Equal(timeExp) {
t.Error("Time does not match")
}
if !reflect.DeepEqual(tags, tagsExp) {
t.Error("tags not encoded correctly")
}
if !reflect.DeepEqual(fields, fieldsExp) {
t.Error("fields not encoded correctly")
}
}