-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_test.go
116 lines (99 loc) · 2.63 KB
/
check_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
package stickyshift
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
_shiftEmpty = Shift{}
_shiftEnd = Shift{Email: _shiftListEnder}
t0 = time.Time{}
t1 = t0.Add(time.Second)
)
func TestCheck(t *testing.T) {
err := check(Schedule{})
require.Error(t, err)
assert.Contains(t, err.Error(), "missing `id`")
}
func TestCheckId(t *testing.T) {
expectValid(t, checkId,
Schedule{Id: "_"},
)
expectInvalid(t, checkId,
Schedule{},
)
}
func TestShiftListSorted(t *testing.T) {
first := Shift{Start: t0}
second := Shift{Start: t1}
expectValid(t, checkShiftListSorted,
Schedule{Shifts: ShiftList{}},
Schedule{Shifts: ShiftList{first, second}},
)
expectInvalid(t, checkShiftListSorted,
Schedule{Shifts: ShiftList{second, first}},
)
}
func TestShiftListDupes(t *testing.T) {
a := Shift{Start: t0}
b := Shift{Start: t1}
expectValid(t, checkShiftListDupes,
Schedule{Shifts: ShiftList{}},
Schedule{Shifts: ShiftList{a}},
Schedule{Shifts: ShiftList{a, b}},
)
expectInvalid(t, checkShiftListDupes,
Schedule{Shifts: ShiftList{a, a}},
)
}
func TestShiftListDupeEmails(t *testing.T) {
expectValid(t, checkShiftListDupeEmail,
Schedule{Shifts: ShiftList{Shift{Start: t0, Email: "a"}, Shift{Start: t1, Email: "b"}}},
)
expectInvalid(t, checkShiftListDupeEmail,
Schedule{Shifts: ShiftList{Shift{Start: t0, Email: "a"}, Shift{Start: t1, Email: "a"}}},
)
}
func TestCheckExtendMaxDays(t *testing.T) {
expectValid(t, checkExtendMaxDays,
Schedule{},
Schedule{Extend: &ExtendOpts{MaxDays: _minMaxDays}},
)
expectInvalid(t, checkExtendMaxDays,
Schedule{Extend: &ExtendOpts{MaxDays: _maxMaxDays + 1}},
)
}
func TestCheckExtendMinDays(t *testing.T) {
expectValid(t, checkExtendMinDays,
Schedule{},
Schedule{Extend: &ExtendOpts{MinDays: _minMinDays}},
)
expectInvalid(t, checkExtendMinDays,
Schedule{Extend: &ExtendOpts{MinDays: _maxMinDays + 1}},
)
}
func TestExtendMinLessThanMax(t *testing.T) {
expectValid(t, checkExtendMinLessThanMax,
Schedule{},
Schedule{Extend: &ExtendOpts{MinDays: 1, MaxDays: 2}},
)
expectInvalid(t, checkExtendMinLessThanMax,
Schedule{Extend: &ExtendOpts{MinDays: 3, MaxDays: 2}},
)
}
func timeFromStr(t *testing.T, s string) time.Time {
res, err := time.Parse(time.RFC3339, s)
require.NoError(t, err)
return res
}
func expectValid(t *testing.T, f func(Schedule) error, ss ...Schedule) {
for _, s := range ss {
assert.NoError(t, f(s), "expected %+v to be valid", s)
}
}
func expectInvalid(t *testing.T, f func(Schedule) error, ss ...Schedule) {
for _, s := range ss {
assert.Error(t, f(s), "expected %+v to be invalid", s)
}
}