-
Notifications
You must be signed in to change notification settings - Fork 0
/
workcalendar_test.go
112 lines (95 loc) · 3.43 KB
/
workcalendar_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
package workcalendar_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/retailcrm/workcalendar"
"github.com/retailcrm/workcalendar/providers"
)
func TestWorkCalendar__SingleDayCheck(t *testing.T) {
wc, err := workcalendar.NewWorkCalendar("ru")
require.NoError(t, err)
assert.False(t, wc.IsWorkday(time.Date(2024, 1, 4, 0, 0, 0, 0, time.UTC)))
assert.True(t, wc.IsDayOff(time.Date(2024, 1, 4, 0, 0, 0, 0, time.UTC)))
assert.True(t, wc.IsWorkday(time.Date(2024, 2, 21, 0, 0, 0, 0, time.UTC)))
assert.False(t, wc.IsDayOff(time.Date(2024, 2, 21, 0, 0, 0, 0, time.UTC)))
assert.False(t, wc.IsWorkday(time.Date(2024, 3, 16, 0, 0, 0, 0, time.UTC)))
assert.True(t, wc.IsDayOff(time.Date(2024, 3, 16, 0, 0, 0, 0, time.UTC)))
}
func TestWorkCalendar__WrongCode(t *testing.T) {
_, err := workcalendar.NewWorkCalendar("oops")
require.Equal(t, err, providers.ErrCodeDoesNotExist)
}
func TestWorkCalendar__ListDays(t *testing.T) {
wc, err := workcalendar.NewWorkCalendar("ru")
require.NoError(t, err)
holidays, err := wc.ListHolidays(
time.Date(2023, 12, 30, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2023, 12, 30, 0, 0, 0, 0, time.UTC),
time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 3, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 4, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 5, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 6, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 7, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 8, 0, 0, 0, 0, time.UTC),
}, holidays)
workdays, err := wc.ListWorkdays(
time.Date(2023, 12, 30, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2024, 1, 9, 0, 0, 0, 0, time.UTC),
time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC),
}, workdays)
}
func TestWorkCalendar__CalendarModification(t *testing.T) {
wc, err := workcalendar.NewWorkCalendar("ru")
require.NoError(t, err)
holidays, err := wc.ListHolidays(
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2024, 2, 23, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
}, holidays)
workdays, err := wc.ListWorkdays(
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
}, workdays)
wc.AddWorkDay(time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC))
wc.AddDayOff(time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC))
holidays, err = wc.ListHolidays(
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 23, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
}, holidays)
workdays, err = wc.ListWorkdays(
time.Date(2024, 2, 22, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 25, 0, 0, 0, 0, time.UTC),
)
require.NoError(t, err)
assert.Equal(t, []time.Time{
time.Date(2024, 2, 24, 0, 0, 0, 0, time.UTC),
}, workdays)
}