-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
489 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package blob | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gocloud.dev/blob" | ||
"gocloud.dev/blob/memblob" | ||
) | ||
|
||
func TestFetchAndUpload(t *testing.T) { | ||
// Create a temporary directory for the test | ||
tempDir, err := os.MkdirTemp("", "blob_test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp dir: %v", err) | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
// Create a test file | ||
testFile := filepath.Join(tempDir, "test.txt") | ||
if err := os.WriteFile(testFile, []byte("test content"), 0o644); err != nil { | ||
t.Fatalf("Failed to create test file: %v", err) | ||
} | ||
|
||
// Create a mock bucket | ||
bucket := memblob.OpenBucket(nil) | ||
defer bucket.Close() | ||
|
||
// Override the openBucket function for testing | ||
originalOpenBucket := openBucket | ||
defer func() { openBucket = originalOpenBucket }() | ||
openBucket = func(_ context.Context, _ string) (*blob.Bucket, func() error, error) { | ||
return bucket, func() error { return nil }, nil | ||
} | ||
|
||
ctx := context.Background() | ||
testURL, _ := url.Parse("mem://test-bucket/test-object") | ||
|
||
// Test Upload | ||
if err := Upload(ctx, *testURL, tempDir); err != nil { | ||
t.Fatalf("Upload failed: %v", err) | ||
} | ||
|
||
// Verify the uploaded content | ||
data, err := bucket.ReadAll(ctx, "test-object") | ||
if err != nil { | ||
t.Fatalf("Failed to read uploaded data: %v", err) | ||
} | ||
if len(data) == 0 { | ||
t.Errorf("Uploaded data is empty") | ||
} | ||
|
||
// Clean up the temp directory | ||
os.RemoveAll(tempDir) | ||
|
||
// Create a new temp directory for Fetch | ||
tempDir, err = os.MkdirTemp("", "blob_test_fetch") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp dir for fetch: %v", err) | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
// Test Fetch | ||
if err := Fetch(ctx, *testURL, tempDir); err != nil { | ||
t.Fatalf("Fetch failed: %v", err) | ||
} | ||
|
||
// Verify the fetched content | ||
fetchedFiles, err := os.ReadDir(tempDir) | ||
if err != nil { | ||
t.Fatalf("Failed to read fetched directory: %v", err) | ||
} | ||
if len(fetchedFiles) == 0 { | ||
t.Errorf("No files were fetched") | ||
} | ||
} |
Oops, something went wrong.