-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
158 lines (142 loc) · 4.79 KB
/
utils_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
149
150
151
152
153
154
155
156
157
158
package main
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestCheckFilesExist tests the checkFilesExist function.
func TestCheckFilesExist(t *testing.T) {
// Define a temporary directory for test files.
tempDir := t.TempDir()
// Case 1: All files exist.
t.Run("All files exist", func(t *testing.T) {
// Create test files.
file1 := filepath.Join(tempDir, "file1.txt")
file2 := filepath.Join(tempDir, "file2.txt")
assert.NoError(
t, os.WriteFile(file1, []byte("test content"), 0644),
)
assert.NoError(
t, os.WriteFile(file2, []byte("test content"), 0644),
)
// Check that all files exist.
err := checkFilesExist(file1, file2)
assert.NoError(t, err, "All specified files should exist")
})
// Case 2: One file does not exist.
t.Run("One file does not exist", func(t *testing.T) {
// Create one test file.
file1 := filepath.Join(tempDir, "file1.txt")
assert.NoError(
t, os.WriteFile(file1, []byte("test content"), 0644),
)
// Check that one file does not exist.
err := checkFilesExist(
file1, filepath.Join(tempDir, "nonexistent.txt"),
)
assert.Error(
t, err, "An error should be returned if any specified "+
"file does not exist",
)
})
// Case 3: No files exist.
t.Run("No files exist", func(t *testing.T) {
// Check that no files exist.
err := checkFilesExist(
filepath.Join(tempDir, "nonexistent1.txt"),
filepath.Join(tempDir, "nonexistent2.txt"),
)
assert.Error(
t, err, "An error should be returned if no "+
"specified files exist",
)
})
// Case 4: Empty file list.
t.Run("Empty file list", func(t *testing.T) {
// Check with an empty list of files.
err := checkFilesExist()
assert.NoError(
t, err, "No error should be returned for an empty "+
"file list",
)
})
}
// TestMostRecentUnixTimestamp tests the mostRecentUnixTimestamp function.
func TestMostRecentUnixTimestamp(t *testing.T) {
// Case 1: Two timestamps provided.
t.Run("Two timestamps provided", func(t *testing.T) {
result := mostRecentUnixTimestamp(
1622132035, 1622132045,
)
expected := int64(1622132045)
if result != expected {
t.Errorf("mostRecentUnixTimestamp returned %d, "+
"expected %d", result, expected)
}
})
// Case 2: Three timestamps provided.
t.Run("Three timestamps provided", func(t *testing.T) {
result := mostRecentUnixTimestamp(
1622132035, 1622132045, 1622132055,
)
expected := int64(1622132055)
if result != expected {
t.Errorf("mostRecentUnixTimestamp returned %d, "+
"expected %d", result, expected)
}
})
// Case 3: Four timestamps provided with one negative value.
t.Run("Four timestamps with negative value", func(t *testing.T) {
result := mostRecentUnixTimestamp(
-1622132035, 1622132045, 1622132055, 1622132065,
)
expected := int64(1622132065)
if result != expected {
t.Errorf("mostRecentUnixTimestamp returned %d, "+
"expected %d", result, expected)
}
})
// Case 4: Five timestamps provided with one zero value.
t.Run("Five timestamps with zero value", func(t *testing.T) {
result := mostRecentUnixTimestamp(
0, 1622132035, 1622132045, 1622132055, 1622132065,
)
expected := int64(1622132065)
if result != expected {
t.Errorf("mostRecentUnixTimestamp returned %d, "+
"expected %d", result, expected)
}
})
// Case 5: No timestamps provided.
t.Run("No timestamps provided", func(t *testing.T) {
result := mostRecentUnixTimestamp()
// By default, return 0 if no timestamps provided.
expected := int64(0)
if result != expected {
t.Errorf("mostRecentUnixTimestamp returned %d, "+
"expected %d", result, expected)
}
})
}
// TestFormatDuration tests the formatDuration function.
func TestFormatDuration(t *testing.T) {
tests := []struct {
duration time.Duration // Duration to be formatted
expected string // Expected formatted string
}{
{duration: time.Second * 45, expected: "0 hours, 0 minutes, 45 seconds"}, // Less than a minute
{duration: time.Minute * 60, expected: "1 hours, 0 minutes, 0 seconds"}, // Exactly one hour
{duration: time.Minute*120 + time.Second*30, expected: "2 hours, 0 minutes, 30 seconds"}, // More than one hour
{duration: time.Hour*25 + time.Minute*30 + time.Second*15, expected: "25 hours, 30 minutes, 15 seconds"}, // Spanning multiple days
{duration: 0, expected: "0 hours, 0 minutes, 0 seconds"}, // Zero duration
{duration: -time.Minute * 30, expected: "0 hours, -30 minutes, 0 seconds"}, // Negative duration
}
for _, test := range tests {
result := formatDuration(test.duration)
if result != test.expected {
t.Errorf("formatDuration(%v) returned %s, expected %s", test.duration, result, test.expected)
}
}
}