-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
86 lines (67 loc) · 1.9 KB
/
main_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
package main
import (
"math/rand"
"os"
"path/filepath"
"strconv"
"testing"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/stretchr/testify/assert"
)
func TestReadConfig(t *testing.T) {
assert := assert.New(t)
t.Run("pass", func(t *testing.T) {
out, err := readConfigFile("./testdata/config.pass.yml")
if assert.Nil(err) {
assert.Equal("oct_token", out.Octopus.Token)
assert.Equal("http://localhost:8086", out.Influx.URL)
assert.Equal("token", out.Influx.Token)
assert.Equal("12345", out.Elec.MPAN)
assert.Equal("12345", out.Elec.Serial)
assert.Equal("12345", out.Gas.MPAN)
assert.Equal("12345", out.Gas.Serial)
}
})
t.Run("fail_open", func(t *testing.T) {
_, err := readConfigFile(filepath.Join(os.TempDir(), strconv.Itoa(rand.Intn(1e6)), ".yml"))
if assert.NotNil(err) {
assert.Contains(err.Error(), "open")
}
})
t.Run("fail_parse", func(t *testing.T) {
_, err := readConfigFile("./testdata/config.fail.yml")
if assert.NotNil(err) {
assert.Contains(err.Error(), "parse")
}
})
t.Run("fail_influx", func(t *testing.T) {
_, err := readConfigFile("./testdata/config.fail_influx.yml")
if assert.NotNil(err) {
assert.Contains(err.Error(), "InfluxDB")
}
})
t.Run("fail_token", func(t *testing.T) {
_, err := readConfigFile("./testdata/config.fail_token.yml")
if assert.NotNil(err) {
assert.Contains(err.Error(), "API token")
}
})
}
func TestGetLastTime(t *testing.T) {
assert := assert.New(t)
t.Run("fail_fuel", func(t *testing.T) {
_, err := getLastTime(nil, nil, "hydrogen")
if assert.NotNil(err) {
assert.Contains(err.Error(), "fuel type")
}
})
t.Run("fail_influx", func(t *testing.T) {
cfg := Config{}
cfg.Influx.Database = "test"
iClient := influxdb2.NewClient("http://", "test")
_, err := getLastTime(&cfg, iClient, fuelELEC)
if assert.NotNil(err) {
assert.Contains(err.Error(), "database")
}
})
}