-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_test.go
148 lines (133 loc) · 3.89 KB
/
time_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
136
137
138
139
140
141
142
143
144
145
146
147
148
package utils_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/dollarsignteam/go-utils"
)
const (
TestTimeLayout = "02/01/2006 15:04:05"
TestTimeString = "07/02/2023 20:28:39"
)
var TestUTCTime, _ = time.Parse(TestTimeLayout, TestTimeString)
func TestParseInBangkokLocation(t *testing.T) {
bangkokTime, err := utils.Time.ParseInBangkokLocation(TestTimeLayout, TestTimeString)
expected := "2023-02-07T20:28:39+07:00"
assert.Nil(t, err)
assert.Equal(t, expected, bangkokTime.Format(time.RFC3339))
}
func TestParseInHongKongLocation(t *testing.T) {
hongKongTime, err := utils.Time.ParseInHongKongLocation(TestTimeLayout, TestTimeString)
expected := "2023-02-07T20:28:39+08:00"
assert.Nil(t, err)
assert.Equal(t, expected, hongKongTime.Format(time.RFC3339))
}
func TestInBangkokTime(t *testing.T) {
result := utils.Time.InBangkokTime(TestUTCTime)
expected := "2023-02-08T03:28:39+07:00"
assert.Equal(t, expected, result.Format(time.RFC3339))
}
func TestInHongKongTime(t *testing.T) {
result := utils.Time.InHongKongTime(TestUTCTime)
expected := "2023-02-08T04:28:39+08:00"
assert.Equal(t, expected, result.Format(time.RFC3339))
}
func TestToMySQLDateTime(t *testing.T) {
result := utils.Time.ToMySQLDateTime(TestUTCTime)
expected := "2023-02-07 20:28:39"
assert.Equal(t, expected, result)
}
func TestToMySQLDate(t *testing.T) {
result := utils.Time.ToMySQLDate(TestUTCTime)
expected := "2023-02-07"
assert.Equal(t, expected, result)
}
func TestToMySQLTime(t *testing.T) {
result := utils.Time.ToMySQLTime(TestUTCTime)
expected := "20:28:39"
assert.Equal(t, expected, result)
}
func TestTimeUtil(t *testing.T) {
currentDate := time.Date(2024, 01, 31, 10, 30, 45, 123456789, time.UTC)
tests := []struct {
name string
function func(time.Time) time.Time
input time.Time
expected time.Time
}{
{
name: "Yesterday",
function: utils.Time.Yesterday,
input: currentDate,
expected: time.Date(2024, 01, 30, 10, 30, 45, 123456789, time.UTC),
},
{
name: "Tomorrow",
function: utils.Time.Tomorrow,
input: currentDate,
expected: time.Date(2024, 02, 1, 10, 30, 45, 123456789, time.UTC),
},
{
name: "BeginningOfDay",
function: utils.Time.BeginningOfDay,
input: currentDate,
expected: time.Date(2024, 01, 31, 0, 0, 0, 0, time.UTC),
},
{
name: "EndOfDay",
function: utils.Time.EndOfDay,
input: currentDate,
expected: time.Date(2024, 01, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "BeginningOfWeek",
function: utils.Time.BeginningOfWeek,
input: currentDate,
expected: time.Date(2024, 01, 29, 0, 0, 0, 0, time.UTC),
},
{
name: "EndOfWeek",
function: utils.Time.EndOfWeek,
input: currentDate,
expected: time.Date(2024, 02, 4, 23, 59, 59, 999999999, time.UTC),
},
{
name: "BeginningOfMonth",
function: utils.Time.BeginningOfMonth,
input: currentDate,
expected: time.Date(2024, 01, 01, 0, 0, 0, 0, time.UTC),
},
{
name: "EndOfMonth",
function: utils.Time.EndOfMonth,
input: currentDate,
expected: time.Date(2024, 01, 31, 23, 59, 59, 999999999, time.UTC),
},
{
name: "BeginningOfYear",
function: utils.Time.BeginningOfYear,
input: currentDate,
expected: time.Date(2024, 01, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "EndOfYear",
function: utils.Time.EndOfYear,
input: currentDate,
expected: time.Date(2024, 12, 31, 23, 59, 59, 999999999, time.UTC),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.function(test.input)
assert.Equal(t, test.expected, result)
})
}
}
func TestIsYesterday(t *testing.T) {
yesterday := utils.Time.Yesterday(time.Now())
assert.True(t, utils.Time.IsYesterday(yesterday))
}
func TestIsTomorrow(t *testing.T) {
tomorrow := utils.Time.Tomorrow(time.Now())
assert.True(t, utils.Time.IsTomorrow(tomorrow))
}