@@ -16,6 +16,7 @@ package data
1616
1717import (
1818 "errors"
19+ "path/filepath"
1920 "reflect"
2021 "testing"
2122 "time"
@@ -169,3 +170,63 @@ func TestBlobKeysPrefix(t *testing.T) {
169170 })
170171 }
171172}
173+
174+ func TestBlobExists (t * testing.T ) {
175+ t .Parallel ()
176+ testcases := []struct {
177+ name string
178+ bucketURL string
179+ key string
180+ want bool
181+ wantErr bool
182+ }{
183+ {
184+ name : "exists" ,
185+ bucketURL : func () string {
186+ // convert local to absolute path, which is needed for the fileblob bucket
187+ testdataPath , err := filepath .Abs ("testdata/blob_test" )
188+ if err != nil {
189+ t .Fatalf ("unexpected error: %v" , err )
190+ }
191+ return "file:///" + testdataPath
192+ }(),
193+ key : "key1.txt" ,
194+ want : true ,
195+ wantErr : false ,
196+ },
197+ {
198+ name : "not exists" ,
199+ bucketURL : func () string {
200+ // convert local to absolute path, which is needed for the fileblob bucket
201+ testdataPath , err := filepath .Abs ("testdata/blob_test" )
202+ if err != nil {
203+ t .Fatalf ("unexpected error: %v" , err )
204+ }
205+ return "file:///" + testdataPath
206+ }(),
207+ key : "notfound.txt" ,
208+ want : false ,
209+ wantErr : false ,
210+ },
211+ {
212+ name : "open bucket error" ,
213+ bucketURL : "invalid" ,
214+ want : false ,
215+ wantErr : true ,
216+ },
217+ }
218+
219+ for i := range testcases {
220+ tt := & testcases [i ]
221+ t .Run (tt .name , func (t * testing.T ) {
222+ t .Parallel ()
223+ exists , err := BlobExists (t .Context (), tt .bucketURL , tt .key )
224+ if (err != nil ) != tt .wantErr {
225+ t .Fatalf ("BlobExists() error = %v, wantErr %v" , err , tt .wantErr )
226+ }
227+ if exists != tt .want {
228+ t .Errorf ("expected blob to exist: got %v, want %v" , exists , tt .want )
229+ }
230+ })
231+ }
232+ }
0 commit comments