forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.go
More file actions
77 lines (68 loc) · 1.6 KB
/
utils_test.go
File metadata and controls
77 lines (68 loc) · 1.6 KB
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
package adminanalytics
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGetTimestamps(t *testing.T) {
now := time.Date(2023, 5, 30, 10, 12, 0, 0, time.UTC)
mockTimeNow := func(t time.Time) {
timeNow = func() time.Time {
return t
}
}
testCases := []struct {
name string
months int
now time.Time
expectedFrom string
expectedTo string
}{
{
name: "3 months",
months: 3,
now: now,
expectedFrom: "2023-02-01T00:00:00Z",
expectedTo: "2023-05-30T10:12:00Z",
},
{
name: "1 month",
months: 1,
now: now,
expectedFrom: "2023-04-01T00:00:00Z",
expectedTo: "2023-05-30T10:12:00Z",
},
{
name: "0 months",
months: 0,
now: now,
expectedFrom: "2023-05-01T00:00:00Z",
expectedTo: "2023-05-30T10:12:00Z",
},
{
name: "February non-leap year",
months: 2,
now: time.Date(2023, 4, 30, 10, 59, 0, 0, time.UTC),
expectedFrom: "2023-02-01T00:00:00Z",
expectedTo: "2023-04-30T10:59:00Z",
},
{
name: "February leap year",
months: 2,
now: time.Date(2024, 4, 29, 10, 59, 0, 0, time.UTC), // 2024 is a leap year
expectedFrom: "2024-02-01T00:00:00Z",
expectedTo: "2024-04-29T10:59:00Z",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockTimeNow(tc.now)
t.Cleanup(func() {
timeNow = time.Now
})
from, to := getTimestamps(tc.months)
require.Equal(t, tc.expectedFrom, from)
require.Equal(t, tc.expectedTo, to)
})
}
}