github.com/creachadair/ffs@v0.17.3/storage/cachestore/cachestore_test.go (about) 1 // Copyright 2020 Michael J. Fromberger. All Rights Reserved. 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 // http://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 cachestore_test 16 17 import ( 18 "testing" 19 20 "github.com/creachadair/ffs/blob" 21 "github.com/creachadair/ffs/blob/memstore" 22 "github.com/creachadair/ffs/blob/storetest" 23 "github.com/creachadair/ffs/storage/cachestore" 24 ) 25 26 var ( 27 _ blob.KV = (*cachestore.KV)(nil) 28 _ blob.StoreCloser = cachestore.Store{} 29 ) 30 31 func TestStore(t *testing.T) { 32 s := cachestore.New(memstore.New(nil), 100) 33 storetest.Run(t, storetest.NopCloser(s)) 34 } 35 36 func TestRegression_keyMap(t *testing.T) { 37 const data = "stuff" 38 m := memstore.NewKV() 39 m.Put(t.Context(), blob.PutOptions{ 40 Key: "init", 41 Data: []byte(data), 42 }) 43 c := cachestore.NewKV(m, 100) 44 got, err := c.Get(t.Context(), "init") 45 if err != nil { 46 t.Fatalf("Get failed: %v", err) 47 } else if s := string(got); s != data { 48 t.Fatalf("Wrong data: got %#q, want %#q", s, data) 49 } 50 } 51 52 func TestRecurrentList(t *testing.T) { 53 ctx := t.Context() 54 55 want := map[string]string{ 56 "1": "one", 57 "2": "two", 58 "3": "three", 59 "4": "four", 60 } 61 base := memstore.New(func() blob.KV { 62 return memstore.NewKV().Init(want) 63 }) 64 cs := cachestore.New(base, 100) 65 kv := storetest.SubKV(t, ctx, cs, "test") 66 67 for key, err := range kv.List(ctx, "") { 68 if err != nil { 69 t.Fatalf("List: unexpected error: %v", err) 70 } 71 if got, err := kv.Get(ctx, key); err != nil { 72 t.Errorf("Get %q: unexpected error: %v", key, err) 73 } else if string(got) != want[key] { 74 t.Errorf("Get %q: got %q, want %q", key, got, want[key]) 75 } 76 77 for k2, err := range kv.List(ctx, key) { 78 if err != nil { 79 t.Fatalf("Inner List: unexpected error: %v", err) 80 } 81 t.Logf("List %q OK", k2) 82 } 83 } 84 }