-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
186 lines (150 loc) · 5.13 KB
/
helpers_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEnsureDownloadDirectory_DirectoryExists(t *testing.T) {
// Create a temporary download directory
tempDir := t.TempDir()
// Ensure the download directory
err := ensureDownloadDirectory(tempDir)
assert.NoError(t, err)
}
func TestReadImageURLsFromFile(t *testing.T) {
// Create a temporary file with image URLs
tempFile := createTempFile(t, []byte("https://example.com/image1.jpg\nhttps://example.com/image2.jpg\nhttps://example.com/image3.jpg"))
// Read the image URLs from the file
imageURLs, err := readImageURLsFromFile(tempFile)
assert.NoError(t, err)
// Assert the image URLs
assert.Equal(t, []string{"https://example.com/image1.jpg", "https://example.com/image2.jpg", "https://example.com/image3.jpg"}, imageURLs)
}
func TestReadImageURLsFromFile_EmptyFile(t *testing.T) {
// Create a temporary empty file
tempFile := createTempFile(t, []byte{})
// Read the image URLs from the file
imageURLs, err := readImageURLsFromFile(tempFile)
assert.NoError(t, err)
// Assert the image URLs
assert.Empty(t, imageURLs)
}
func TestReadImageURLsFromFile_NonexistentFile(t *testing.T) {
// Read from a non-existent file
imageURLs, err := readImageURLsFromFile("nonexistent.txt")
// Assert the error and image URLs
assert.Error(t, err)
assert.Nil(t, imageURLs)
}
func TestBatchImageURLs(t *testing.T) {
// Create a list of image URLs
imageURLs := []string{
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg",
"https://example.com/image4.jpg",
"https://example.com/image5.jpg",
"https://example.com/image6.jpg",
"https://example.com/image7.jpg",
"https://example.com/image8.jpg",
"https://example.com/image9.jpg",
"https://example.com/image10.jpg",
"https://example.com/image11.jpg",
}
// Batch the image URLs
batches := batchImageURLs(imageURLs, 5)
// Assert the batches
assert.Equal(t, [][]string{
{"https://example.com/image1.jpg", "https://example.com/image2.jpg", "https://example.com/image3.jpg", "https://example.com/image4.jpg", "https://example.com/image5.jpg"},
{"https://example.com/image6.jpg", "https://example.com/image7.jpg", "https://example.com/image8.jpg", "https://example.com/image9.jpg", "https://example.com/image10.jpg"},
{"https://example.com/image11.jpg"},
}, batches)
}
func TestBatchImageURLs_EmptyURLs(t *testing.T) {
// Create an empty list of image URLs
imageURLs := []string{}
// Batch the image URLs
batches := batchImageURLs(imageURLs, 5)
// Assert the batches
assert.Empty(t, batches)
}
func TestDownloadImage_FileExists(t *testing.T) {
// Create a temporary directory
tempDir := t.TempDir()
// Create a dummy file in the directory
dummyFile := filepath.Join(tempDir, "dummy.txt")
err := ioutil.WriteFile(dummyFile, []byte("dummy"), 0644)
assert.NoError(t, err)
// Download an image
err = downloadImage("https://via.placeholder.com/150", tempDir)
assert.NoError(t, err)
// Check if the image file exists
imagePath := filepath.Join(tempDir, "150")
_, err = os.Stat(imagePath)
assert.NoError(t, err)
// Remove the image file
err = os.Remove(imagePath)
assert.NoError(t, err)
// Remove the dummy file
err = os.Remove(dummyFile)
assert.NoError(t, err)
}
func TestGetImageFileSize(t *testing.T) {
// Get the size of a remote image
size, err := getImageFileSize("https://via.placeholder.com/150")
assert.NoError(t, err)
assert.NotZero(t, size)
}
func TestGetImageFileSize_InvalidURL(t *testing.T) {
// Get the size of an invalid image URL
size, err := getImageFileSize("https://example.com/invalid.jpg")
assert.Error(t, err)
assert.Zero(t, size)
}
func TestParseMaxImageSize(t *testing.T) {
// Parse max image size in MB
size, err := parseMaxImageSize("5")
assert.NoError(t, err)
assert.Equal(t, int64(5*1024*1024), size)
}
func TestParseMaxImageSize_Max(t *testing.T) {
// Parse max image size with "MAX"
size, err := parseMaxImageSize("MAX")
assert.NoError(t, err)
assert.Equal(t, int64(-1), size)
}
func TestParseMaxImageSize_InvalidSize(t *testing.T) {
// Parse invalid max image size
size, err := parseMaxImageSize("invalid")
assert.Error(t, err)
assert.Zero(t, size)
}
func TestIsImageSizeExceeded(t *testing.T) {
// Check if image size is exceeded
size := int64(5 * 1024 * 1024)
exceeded := isImageSizeExceeded("https://via.placeholder.com/150", size)
assert.False(t, exceeded)
}
func TestIsImageSizeExceeded_InvalidURL(t *testing.T) {
// Check if image size is exceeded with an invalid URL
size := int64(5 * 1024 * 1024)
exceeded := isImageSizeExceeded("https://example.com/invalid.jpg", size)
assert.True(t, exceeded)
}
func TestGenerateRandomWaitTime(t *testing.T) {
// Generate random wait time
waitTime := generateRandomWaitTime(0.8, 3.0)
assert.GreaterOrEqual(t, waitTime.Seconds(), 0.8)
assert.LessOrEqual(t, waitTime.Seconds(), 3.0)
}
func createTempFile(t *testing.T, content []byte) string {
tempFile, err := ioutil.TempFile("", "testfile")
assert.NoError(t, err)
_, err = tempFile.Write(content)
assert.NoError(t, err)
err = tempFile.Close()
assert.NoError(t, err)
return tempFile.Name()
}