Refactor storage tests to use unique filenames with UUIDs

- Introduced a `generateTestFileName` function to create unique test filenames for both local and S3 storage tests, reducing conflicts during test execution.
- Updated test cases to utilize the new filename generation method, ensuring consistent and unique identifiers for uploaded files across various scenarios.
- Enhanced readability and maintainability of test code by replacing hardcoded filenames with dynamically generated ones.
This commit is contained in:
Max 2025-07-27 10:05:50 +08:00
parent c3b374f043
commit 8471343311
2 changed files with 52 additions and 34 deletions

View file

@ -10,9 +10,15 @@ import (
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
// generateTestFileName generates a unique test filename with the given prefix and extension
func generateTestFileName(prefix, ext string) string {
return prefix + "-" + uuid.New().String() + ext
}
func TestLocalStorage(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "local_storage_test")
@ -41,7 +47,8 @@ func TestLocalStorage(t *testing.T) {
content := []byte("test content")
reader := bytes.NewReader(content)
fileID, err := storage.Upload(context.Background(), "test.txt", reader, "text/plain")
fileID := generateTestFileName("upload-download", ".txt")
_, err = storage.Upload(context.Background(), fileID, reader, "text/plain")
assert.NoError(t, err)
assert.NotEmpty(t, fileID)
@ -70,7 +77,8 @@ func TestLocalStorage(t *testing.T) {
// Upload
reader := bytes.NewReader(buf.Bytes())
fileID, err := storage.Upload(context.Background(), "test.png", reader, "image/png")
fileID := generateTestFileName("image-with-compression", ".png")
_, err = storage.Upload(context.Background(), fileID, reader, "image/png")
assert.NoError(t, err)
assert.NotEmpty(t, fileID)
@ -107,7 +115,8 @@ func TestLocalStorage(t *testing.T) {
// Upload
reader := bytes.NewReader(buf.Bytes())
fileID, err := storage.Upload(context.Background(), "test.png", reader, "image/png")
fileID := generateTestFileName("image-without-compression", ".png")
_, err = storage.Upload(context.Background(), fileID, reader, "image/png")
assert.NoError(t, err)
assert.NotEmpty(t, fileID)
@ -232,41 +241,44 @@ func TestLocalStorage(t *testing.T) {
// Test different file types to verify content type detection
testFiles := []struct {
name string
ext string
content []byte
contentType string
expectedCT string
}{
{"test.txt", []byte("Hello World"), "text/plain", "text/plain"},
{"test.json", []byte(`{"key": "value"}`), "application/json", "application/json"},
{"test.html", []byte("<html><body>Test</body></html>"), "text/html", "text/html"},
{"test.csv", []byte("col1,col2\nval1,val2"), "text/csv", "text/csv"},
{"test.md", []byte("# Markdown Content"), "text/markdown", "text/markdown"},
{"test.yao", []byte("yao file content"), "application/yao", "application/yao"},
{".txt", []byte("Hello World"), "text/plain", "text/plain"},
{".json", []byte(`{"key": "value"}`), "application/json", "application/json"},
{".html", []byte("<html><body>Test</body></html>"), "text/html", "text/html"},
{".csv", []byte("col1,col2\nval1,val2"), "text/csv", "text/csv"},
{".md", []byte("# Markdown Content"), "text/markdown", "text/markdown"},
{".yao", []byte("yao file content"), "application/yao", "application/yao"},
}
for _, tf := range testFiles {
// Generate unique filename with UUID to avoid conflicts
fileName := generateTestFileName("localpath-test", tf.ext)
// Upload file
_, err = storage.Upload(context.Background(), tf.name, bytes.NewReader(tf.content), tf.contentType)
assert.NoError(t, err, "Failed to upload %s", tf.name)
_, err = storage.Upload(context.Background(), fileName, bytes.NewReader(tf.content), tf.contentType)
assert.NoError(t, err, "Failed to upload %s", fileName)
// Get local path and content type
localPath, detectedCT, err := storage.LocalPath(context.Background(), tf.name)
assert.NoError(t, err, "Failed to get local path for %s", tf.name)
assert.NotEmpty(t, localPath, "Local path should not be empty for %s", tf.name)
assert.Equal(t, tf.expectedCT, detectedCT, "Content type mismatch for %s", tf.name)
localPath, detectedCT, err := storage.LocalPath(context.Background(), fileName)
assert.NoError(t, err, "Failed to get local path for %s", fileName)
assert.NotEmpty(t, localPath, "Local path should not be empty for %s", fileName)
assert.Equal(t, tf.expectedCT, detectedCT, "Content type mismatch for %s", fileName)
// Verify the path is absolute
assert.True(t, filepath.IsAbs(localPath), "Path should be absolute for %s", tf.name)
assert.True(t, filepath.IsAbs(localPath), "Path should be absolute for %s", fileName)
// Verify the file exists at the returned path
_, err = os.Stat(localPath)
assert.NoError(t, err, "File should exist at local path for %s", tf.name)
assert.NoError(t, err, "File should exist at local path for %s", fileName)
// Verify file content
fileContent, err := os.ReadFile(localPath)
assert.NoError(t, err, "Failed to read file at local path for %s", tf.name)
assert.Equal(t, tf.content, fileContent, "File content mismatch for %s", tf.name)
assert.NoError(t, err, "Failed to read file at local path for %s", fileName)
assert.Equal(t, tf.content, fileContent, "File content mismatch for %s", fileName)
}
})

View file

@ -16,6 +16,11 @@ import (
"github.com/stretchr/testify/assert"
)
// generateTestFileName generates a unique test filename with the given prefix and extension
func generateTestFileName(prefix, ext string) string {
return prefix + "-" + uuid.New().String() + ext
}
func getS3Config() map[string]interface{} {
return map[string]interface{}{
"endpoint": os.Getenv("S3_API"),
@ -68,7 +73,8 @@ func TestS3Storage(t *testing.T) {
content := []byte("test content")
reader := bytes.NewReader(content)
fileID, err := storage.Upload(context.Background(), "test.txt", reader, "text/plain")
fileID := generateTestFileName("upload-test", ".txt")
_, err = storage.Upload(context.Background(), fileID, reader, "text/plain")
assert.NoError(t, err)
assert.NotEmpty(t, fileID)
@ -98,7 +104,7 @@ func TestS3Storage(t *testing.T) {
storage, err := New(getS3Config())
assert.NoError(t, err)
fileID := "test-chunked-" + uuid.New().String() + ".txt"
fileID := generateTestFileName("test-chunked", ".txt")
content1 := []byte("chunk1")
content2 := []byte("chunk2")
@ -133,7 +139,7 @@ func TestS3Storage(t *testing.T) {
storage, err := New(getS3Config())
assert.NoError(t, err)
fileID := "test-ops-" + uuid.New().String() + ".txt"
fileID := generateTestFileName("test-ops", ".txt")
content := []byte("test content")
// Upload file
@ -174,7 +180,7 @@ func TestS3Storage(t *testing.T) {
assert.NoError(t, err)
// Use UUID for non-existent file to avoid any potential conflicts
nonExistentFileID := "non-existent-" + uuid.New().String() + ".txt"
nonExistentFileID := generateTestFileName("non-existent", ".txt")
_, _, err = storage.Download(context.Background(), nonExistentFileID)
assert.Error(t, err)
})
@ -222,17 +228,17 @@ func TestS3Storage(t *testing.T) {
contentType string
expectedCT string
}{
{"test.txt", []byte("Hello S3 World"), "text/plain", "text/plain"},
{"test.json", []byte(`{"s3": "test"}`), "application/json", "application/json"},
{"test.html", []byte("<html><body>S3 Test</body></html>"), "text/html", "text/html"},
{"test.csv", []byte("s3,test\nval1,val2"), "text/csv", "text/csv"},
{"test.md", []byte("# S3 Markdown"), "text/markdown", "text/markdown"},
{"test.yao", []byte("s3 yao content"), "application/yao", "application/yao"},
{"localpath-test.txt", []byte("Hello S3 World"), "text/plain", "text/plain"},
{"localpath-test.json", []byte(`{"s3": "test"}`), "application/json", "application/json"},
{"localpath-test.html", []byte("<html><body>S3 Test</body></html>"), "text/html", "text/html"},
{"localpath-test.csv", []byte("s3,test\nval1,val2"), "text/csv", "text/csv"},
{"localpath-test.md", []byte("# S3 Markdown"), "text/markdown", "text/markdown"},
{"localpath-test.yao", []byte("s3 yao content"), "application/yao", "application/yao"},
}
for _, tf := range testFiles {
// Upload file to S3
fileID := "s3-localpath-" + uuid.New().String() + "-" + tf.name
fileID := generateTestFileName("s3-localpath", "-"+tf.name)
_, err = storage.Upload(context.Background(), fileID, bytes.NewReader(tf.content), tf.contentType)
assert.NoError(t, err, "Failed to upload %s", tf.name)
@ -288,7 +294,7 @@ func TestS3Storage(t *testing.T) {
gzipWriter.Close()
// Upload gzipped file
fileID := "gzipped-" + uuid.New().String() + ".txt.gz"
fileID := generateTestFileName("gzipped", ".txt.gz")
_, err = storage.Upload(context.Background(), fileID, bytes.NewReader(gzipBuf.Bytes()), "text/plain")
assert.NoError(t, err)
@ -319,7 +325,7 @@ func TestS3Storage(t *testing.T) {
assert.NoError(t, err)
// Test with non-existent file
nonExistentFileID := "non-existent-" + uuid.New().String() + ".txt"
nonExistentFileID := generateTestFileName("non-existent-localpath", ".txt")
_, _, err = storage.LocalPath(context.Background(), nonExistentFileID)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to download file")
@ -344,7 +350,7 @@ func TestS3Storage(t *testing.T) {
// Upload a test file
content := []byte("Custom cache directory test")
fileID := "custom-cache-" + uuid.New().String() + ".txt"
fileID := generateTestFileName("custom-cache", ".txt")
_, err = storage.Upload(context.Background(), fileID, bytes.NewReader(content), "text/plain")
assert.NoError(t, err)