-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
82 lines (75 loc) · 1.62 KB
/
config_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
package main
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func CreateTestConfig(configContent []byte) (*os.File, error) {
tmpfile, err := os.CreateTemp("", "*-pass")
if err != nil {
return nil, err
}
if _, err := tmpfile.Write(configContent); err != nil {
return nil, err
}
if err := tmpfile.Close(); err != nil {
return nil, err
}
return tmpfile, nil
}
func TestLoadConfig(t *testing.T) {
var testCases = []struct {
name string
configContent []byte
token string
locationID int
interval int
tempUnit string
err error
}{
{
"full-token",
[]byte(fmt.Sprintf("token: \"1234567890\"\nlocationId: 0\ninterval: 60\ntempUnit: \"C\"")),
"1234567890",
0,
60,
"C",
nil,
},
{
"missing-interval",
[]byte(fmt.Sprintf("token: \"1234567890\"\ntempUnit: \"F\"")),
"1234567890",
0,
0,
"F",
nil,
},
{
"invalid-config",
[]byte(`foobar-invalid`),
"",
0,
0,
"",
&yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!str `foobar-...` into main.Config"}}},
}
for _, tC := range testCases {
t.Run(tC.name, func(t *testing.T) {
tempFile, err := CreateTestConfig(tC.configContent)
require.NoError(t, err)
defer os.Remove(tempFile.Name())
cfg, err := LoadConfig(tempFile.Name())
if err == nil {
assert.Equal(t, tC.token, cfg.Token)
assert.Equal(t, tC.locationID, cfg.LocationID)
assert.Equal(t, tC.interval, cfg.Interval)
assert.Equal(t, tC.tempUnit, cfg.TempUnit)
}
assert.Equal(t, tC.err, err)
})
}
}