-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunixtime_test.go
52 lines (45 loc) · 1.25 KB
/
unixtime_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
package main
import (
"testing"
"time"
)
func TestConvertUnixTimestamp(t *testing.T) {
format := "2006-01-02 15:04:05.000000000"
tests := []struct {
timestamp int64
expected string
}{
{1627670400005, "2021-07-30 20:40:00.005000000"},
{1627670400000005, "2021-07-30 20:40:00.000005000"},
{1627670400000000005, "2021-07-30 20:40:00.000000005"},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
result, err := convertUnixTimestamp(tt.timestamp, format)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("convertUnixTimestamp(%d, %q) = %q; want %q", tt.timestamp, format, result, tt.expected)
}
})
}
}
func TestConvertUnixTimestampInvalid(t *testing.T) {
format := "2006-01-02 15:04:05.999999999"
_, err := convertUnixTimestamp(1627670, format)
if err == nil {
t.Error("expected error for invalid timestamp, got nil")
}
}
func TestUnixTimestamp(t *testing.T) {
start := time.Now().Unix()
result := unixTimestamp()
end := time.Now().Unix()
// Check if the result falls within the expected range
t.Run("", func(t *testing.T) {
if result < start || result > end {
t.Errorf("Expected timestamp to be between %d and %d, but got %d", start, end, result)
}
})
}