feat: add ListAll iterator method for Go 1.23+ pagination
This commit is contained in:
@@ -493,6 +493,141 @@ func TestS3_NewUsingIAM(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAll(t *testing.T) {
|
||||
// Setup test environment
|
||||
s3 := New(
|
||||
os.Getenv("AWS_S3_REGION"),
|
||||
os.Getenv("AWS_S3_ACCESS_KEY"),
|
||||
os.Getenv("AWS_S3_SECRET_KEY"),
|
||||
)
|
||||
s3.SetEndpoint(os.Getenv("AWS_S3_ENDPOINT"))
|
||||
|
||||
// Test cases
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func() // Setup function to prepare test data
|
||||
input ListInput
|
||||
validate func([]Object, error)
|
||||
cleanup func() // Cleanup function
|
||||
}{
|
||||
{
|
||||
name: "Empty Bucket Iterator",
|
||||
input: ListInput{
|
||||
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
||||
},
|
||||
validate: func(objects []Object, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(objects) != 0 {
|
||||
t.Errorf("Expected empty bucket, got %d objects", len(objects))
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Basic Iterator Listing",
|
||||
setup: func() {
|
||||
// Upload test files
|
||||
uploadTestFiles(t, s3, os.Getenv("AWS_S3_BUCKET"), []string{"iter_file1.txt", "iter_file2.txt", "iter_file3.txt"})
|
||||
},
|
||||
input: ListInput{
|
||||
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
||||
},
|
||||
validate: func(objects []Object, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(objects) != 3 {
|
||||
t.Errorf("Expected 3 objects, got %d", len(objects))
|
||||
}
|
||||
// Check that all expected files are present
|
||||
expectedKeys := map[string]bool{
|
||||
"iter_file1.txt": false,
|
||||
"iter_file2.txt": false,
|
||||
"iter_file3.txt": false,
|
||||
}
|
||||
for _, obj := range objects {
|
||||
if _, exists := expectedKeys[obj.Key]; exists {
|
||||
expectedKeys[obj.Key] = true
|
||||
}
|
||||
}
|
||||
for key, found := range expectedKeys {
|
||||
if !found {
|
||||
t.Errorf("Expected object %s not found in iterator results", key)
|
||||
}
|
||||
}
|
||||
},
|
||||
cleanup: func() {
|
||||
cleanupTestFiles(t, s3, os.Getenv("AWS_S3_BUCKET"), []string{"iter_file1.txt", "iter_file2.txt", "iter_file3.txt"})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Iterator with Prefix",
|
||||
setup: func() {
|
||||
uploadTestFiles(t, s3, os.Getenv("AWS_S3_BUCKET"), []string{
|
||||
"iter_docs/file1.txt",
|
||||
"iter_docs/file2.txt",
|
||||
"iter_images/image1.jpg",
|
||||
"iter_images/image2.jpg",
|
||||
})
|
||||
},
|
||||
input: ListInput{
|
||||
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
||||
Prefix: "iter_docs/",
|
||||
},
|
||||
validate: func(objects []Object, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if len(objects) != 2 {
|
||||
t.Errorf("Expected 2 objects with iter_docs/ prefix, got %d", len(objects))
|
||||
}
|
||||
for _, obj := range objects {
|
||||
if !strings.HasPrefix(obj.Key, "iter_docs/") {
|
||||
t.Errorf("Object %s doesn't have expected prefix", obj.Key)
|
||||
}
|
||||
}
|
||||
},
|
||||
cleanup: func() {
|
||||
cleanupTestFiles(t, s3, os.Getenv("AWS_S3_BUCKET"), []string{
|
||||
"iter_docs/file1.txt",
|
||||
"iter_docs/file2.txt",
|
||||
"iter_images/image1.jpg",
|
||||
"iter_images/image2.jpg",
|
||||
})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.setup != nil {
|
||||
tt.setup()
|
||||
}
|
||||
|
||||
// Collect all objects from the iterator and check for errors
|
||||
var objects []Object
|
||||
seq, finish := s3.ListAll(tt.input)
|
||||
for obj := range seq {
|
||||
objects = append(objects, obj)
|
||||
}
|
||||
|
||||
// Check for any iteration errors
|
||||
if err := finish(); err != nil {
|
||||
t.Fatalf("Iterator error: %v", err)
|
||||
}
|
||||
|
||||
if tt.validate != nil {
|
||||
tt.validate(objects, nil) // No error from iterator
|
||||
}
|
||||
|
||||
if tt.cleanup != nil {
|
||||
tt.cleanup()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomEndpoint(t *testing.T) {
|
||||
s3 := New("us-east-1", "AccessKey", "SuperSecretKey")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user