github.com/thiagoyeds/go-cloud@v0.26.0/blob/memblob/memblob_test.go (about) 1 // Copyright 2018 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package memblob 16 17 import ( 18 "context" 19 "net/http" 20 "testing" 21 22 "gocloud.dev/blob" 23 "gocloud.dev/blob/driver" 24 "gocloud.dev/blob/drivertest" 25 ) 26 27 type harness struct { 28 prefix string 29 } 30 31 func newHarness(ctx context.Context, t *testing.T, prefix string) (drivertest.Harness, error) { 32 return &harness{prefix: prefix}, nil 33 } 34 35 func (h *harness) HTTPClient() *http.Client { 36 return nil 37 } 38 39 func (h *harness) MakeDriver(ctx context.Context) (driver.Bucket, error) { 40 drv := openBucket(nil) 41 if h.prefix == "" { 42 return drv, nil 43 } 44 return driver.NewPrefixedBucket(drv, h.prefix), nil 45 } 46 47 func (h *harness) MakeDriverForNonexistentBucket(ctx context.Context) (driver.Bucket, error) { 48 // Does not make sense for this driver. 49 return nil, nil 50 } 51 52 func (h *harness) Close() {} 53 54 func TestConformance(t *testing.T) { 55 newHarnessNoPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { 56 return newHarness(ctx, t, "") 57 } 58 drivertest.RunConformanceTests(t, newHarnessNoPrefix, nil) 59 } 60 61 func TestConformanceWithPrefix(t *testing.T) { 62 const prefix = "some/prefix/dir/" 63 newHarnessWithPrefix := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { 64 return newHarness(ctx, t, prefix) 65 } 66 drivertest.RunConformanceTests(t, newHarnessWithPrefix, nil) 67 } 68 69 func BenchmarkMemblob(b *testing.B) { 70 drivertest.RunBenchmarks(b, OpenBucket(nil)) 71 } 72 73 func TestOpenBucketFromURL(t *testing.T) { 74 tests := []struct { 75 URL string 76 WantErr bool 77 }{ 78 // OK. 79 {"mem://", false}, 80 // With prefix. 81 {"mem://?prefix=foo/bar", false}, 82 // Invalid parameter. 83 {"mem://?param=value", true}, 84 } 85 86 ctx := context.Background() 87 for _, test := range tests { 88 b, err := blob.OpenBucket(ctx, test.URL) 89 if b != nil { 90 defer b.Close() 91 } 92 if (err != nil) != test.WantErr { 93 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) 94 } 95 } 96 }