Skip to content

Commit 925548b

Browse files
committed
feat: add TestDataToPoint
1 parent 496f4bc commit 925548b

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed

api/data_to_point.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func DataToPoint(x interface{}) (*write.Point, error) {
3737
}
3838
fields := reflect.VisibleFields(t)
3939

40-
var measurement string = ""
40+
var measurement = ""
4141
var lpTags = make(map[string]string)
4242
var lpFields = make(map[string]interface{})
4343
var lpTime time.Time
@@ -63,8 +63,14 @@ func DataToPoint(x interface{}) (*write.Point, error) {
6363
}
6464
measurement = v.FieldByIndex(f.Index).String()
6565
case "tag":
66+
if name == "" {
67+
return nil, fmt.Errorf("cannot use field '%s': invalid lp tag name \"\"", f.Name)
68+
}
6669
lpTags[name] = v.FieldByIndex(f.Index).String()
6770
case "field":
71+
if name == "" {
72+
return nil, fmt.Errorf("cannot use field '%s': invalid lp field name \"\"", f.Name)
73+
}
6874
lpFields[name] = v.FieldByIndex(f.Index).Interface()
6975
case "timestamp":
7076
if f.Type != timeType {

api/data_to_point_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package api
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/influxdata/influxdb-client-go/v2/api/write"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
lp "github.com/influxdata/line-protocol"
14+
)
15+
16+
func TestDataToPoint(t *testing.T) {
17+
pointToLine := func(point *write.Point) string {
18+
var buffer bytes.Buffer
19+
e := lp.NewEncoder(&buffer)
20+
e.SetFieldTypeSupport(lp.UintSupport)
21+
e.FailOnFieldErr(true)
22+
_, err := e.Encode(point)
23+
if err != nil {
24+
panic(err)
25+
}
26+
return buffer.String()
27+
}
28+
now := time.Now()
29+
tests := []struct {
30+
name string
31+
s interface{}
32+
line string
33+
error string
34+
}{{
35+
name: "test normal structure",
36+
s: struct {
37+
Measurement string `lp:"measurement"`
38+
Sensor string `lp:"tag,sensor"`
39+
ID string `lp:"tag,device_id"`
40+
Temp float64 `lp:"field,temperature"`
41+
Hum int `lp:"field,humidity"`
42+
Time time.Time `lp:"timestamp"`
43+
Description string `lp:"-"`
44+
}{
45+
"air",
46+
"SHT31",
47+
"10",
48+
23.5,
49+
55,
50+
now,
51+
"Room temp",
52+
},
53+
line: fmt.Sprintf("air,device_id=10,sensor=SHT31 humidity=55i,temperature=23.5 %d\n", now.UnixNano()),
54+
},
55+
{
56+
name: "test pointer to normal structure",
57+
s: &struct {
58+
Measurement string `lp:"measurement"`
59+
Sensor string `lp:"tag,sensor"`
60+
ID string `lp:"tag,device_id"`
61+
Temp float64 `lp:"field,temperature"`
62+
Hum int `lp:"field,humidity"`
63+
Time time.Time `lp:"timestamp"`
64+
Description string `lp:"-"`
65+
}{
66+
"air",
67+
"SHT31",
68+
"10",
69+
23.5,
70+
55,
71+
now,
72+
"Room temp",
73+
},
74+
line: fmt.Sprintf("air,device_id=10,sensor=SHT31 humidity=55i,temperature=23.5 %d\n", now.UnixNano()),
75+
}, {
76+
name: "test no tag, no timestamp",
77+
s: &struct {
78+
Measurement string `lp:"measurement"`
79+
Temp float64 `lp:"field,temperature"`
80+
}{
81+
"air",
82+
23.5,
83+
},
84+
line: "air temperature=23.5\n",
85+
},
86+
{
87+
name: "test default struct field name",
88+
s: &struct {
89+
Measurement string `lp:"measurement"`
90+
Sensor string `lp:"tag"`
91+
Temp float64 `lp:"field"`
92+
}{
93+
"air",
94+
"SHT31",
95+
23.5,
96+
},
97+
line: "air,Sensor=SHT31 Temp=23.5\n",
98+
},
99+
{
100+
name: "test missing struct field tag name",
101+
s: &struct {
102+
Measurement string `lp:"measurement"`
103+
Sensor string `lp:"tag,"`
104+
Temp float64 `lp:"field"`
105+
}{
106+
"air",
107+
"SHT31",
108+
23.5,
109+
},
110+
error: `cannot use field 'Sensor': invalid lp tag name ""`,
111+
},
112+
{
113+
name: "test missing struct field field name",
114+
s: &struct {
115+
Measurement string `lp:"measurement"`
116+
Temp float64 `lp:"field,"`
117+
}{
118+
"air",
119+
23.5,
120+
},
121+
error: `cannot use field 'Temp': invalid lp field name ""`,
122+
},
123+
{
124+
name: "test missing measurement",
125+
s: &struct {
126+
Measurement string `lp:"tag"`
127+
Sensor string `lp:"tag"`
128+
Temp float64 `lp:"field"`
129+
}{
130+
"air",
131+
"SHT31",
132+
23.5,
133+
},
134+
error: `no struct field with tag 'measurement'`,
135+
},
136+
{
137+
name: "test no field",
138+
s: &struct {
139+
Measurement string `lp:"measurement"`
140+
Sensor string `lp:"tag"`
141+
Temp float64 `lp:"tag"`
142+
}{
143+
"air",
144+
"SHT31",
145+
23.5,
146+
},
147+
error: `no struct field with tag 'field'`,
148+
},
149+
{
150+
name: "test double measurement",
151+
s: &struct {
152+
Measurement string `lp:"measurement"`
153+
Sensor string `lp:"measurement"`
154+
Temp float64 `lp:"field,a"`
155+
Hum float64 `lp:"field,a"`
156+
}{
157+
"air",
158+
"SHT31",
159+
23.5,
160+
43.1,
161+
},
162+
error: `multiple measurement fields`,
163+
},
164+
{
165+
name: "test multiple tag attributes",
166+
s: &struct {
167+
Measurement string `lp:"measurement"`
168+
Sensor string `lp:"tag,a,a"`
169+
Temp float64 `lp:"field,a"`
170+
Hum float64 `lp:"field,a"`
171+
}{
172+
"air",
173+
"SHT31",
174+
23.5,
175+
43.1,
176+
},
177+
error: `multiple tag attributes are not supported`,
178+
},
179+
{
180+
name: "test wrong timestamp type",
181+
s: &struct {
182+
Measurement string `lp:"measurement"`
183+
Sensor string `lp:"tag,sensor"`
184+
Temp float64 `lp:"field,a"`
185+
Hum float64 `lp:"timestamp"`
186+
}{
187+
"air",
188+
"SHT31",
189+
23.5,
190+
43.1,
191+
},
192+
error: `cannot use field 'Hum' as a timestamp`,
193+
},
194+
{
195+
name: "test map",
196+
s: map[string]interface{}{
197+
"measurement": "air",
198+
"sensor": "SHT31",
199+
"temp": 23.5,
200+
},
201+
error: `cannot use map[string]interface {} as point`,
202+
},
203+
}
204+
for _, ts := range tests {
205+
t.Run(ts.name, func(t *testing.T) {
206+
point, err := DataToPoint(ts.s)
207+
if ts.error == "" {
208+
require.NoError(t, err)
209+
assert.Equal(t, ts.line, pointToLine(point))
210+
} else {
211+
require.Error(t, err)
212+
assert.Equal(t, ts.error, err.Error())
213+
}
214+
})
215+
}
216+
}

0 commit comments

Comments
 (0)