-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
135 lines (104 loc) · 2.61 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package jobsrunner
import (
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestConfigJobInterval_UnmarshalJSON_Null(t *testing.T) {
// Arrange.
var c ConfigJobInterval
// Act.
err := c.UnmarshalJSON([]byte("null"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 0, c)
}
func TestConfigJobInterval_UnmarshalJSON_TooManySpaces(t *testing.T) {
// Arrange.
var c ConfigJobInterval
// Act.
err := c.UnmarshalJSON([]byte("5 seconds"))
// Assert.
require.NotNil(t, err)
}
func TestConfigJobInterval_UnmarshalJSON_CannotParseNumber(t *testing.T) {
// Arrange.
var c ConfigJobInterval
// Act.
err := c.UnmarshalJSON([]byte("five seconds"))
// Assert.
require.NotNil(t, err)
}
func TestConfigJobInterval_UnmarshalJSON_NonpositiveNumber(t *testing.T) {
// Arrange.
var c ConfigJobInterval
// Act.
err1 := c.UnmarshalJSON([]byte("-3 seconds"))
err2 := c.UnmarshalJSON([]byte("0 seconds"))
// Assert.
require.NotNil(t, err1)
require.NotNil(t, err2)
}
func TestConfigJobInterval_UnmarshalJSON_CorrectParsing(t *testing.T) {
// Arrange.
var c ConfigJobInterval
t.Run("second", func(t *testing.T) {
// Act.
err := c.UnmarshalJSON([]byte("1 second"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 1*time.Second, c)
})
t.Run("seconds", func(t *testing.T) {
// Act.
err := c.UnmarshalJSON([]byte("3 seconds"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 3*time.Second, c)
})
t.Run("minute", func(t *testing.T) {
// Act.
err := c.UnmarshalJSON([]byte("1 minute"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 1*time.Minute, c)
})
t.Run("hours", func(t *testing.T) {
// Act.
err := c.UnmarshalJSON([]byte("3 hours"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 3*time.Hour, c)
})
t.Run("hour", func(t *testing.T) {
// Act.
err := c.UnmarshalJSON([]byte("1 hour"))
// Assert.
require.Nil(t, err)
require.EqualValues(t, 1*time.Hour, c)
})
}
func TestConfigJobInterval_UnmarshalJSON_BadModifier(t *testing.T) {
// Arrange.
var c ConfigJobInterval
// Act.
err := c.UnmarshalJSON([]byte("5 daysOrSo"))
// Assert.
require.NotNil(t, err)
}
func TestNewConfigFromFile(t *testing.T) {
// Arrange.
_, err := NewConfigFromFile("absolutely-absent-file.json")
// Assert.
require.NotNil(t, err)
}
func TestNewConfigFromFile_(t *testing.T) {
// Arrange.
c, err := NewConfigFromFile("testdata/config.json")
// Assert.
require.Nil(t, err)
require.EqualValues(t, 1, c.Version)
require.Len(t, c.Jobs, 1)
require.EqualValues(t, "echo 1337", c.Jobs[0].Cmd)
require.EqualValues(t, 5*time.Second, c.Jobs[0].Interval)
}