github.com/creachadair/ffs@v0.17.3/blob/memstore/memstore_test.go (about) 1 // Copyright 2019 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 memstore_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/google/go-cmp/cmp" 24 ) 25 26 func TestStore(t *testing.T) { 27 var s memstore.Store 28 storetest.Run(t, &s) 29 } 30 31 func TestSnapshot(t *testing.T) { 32 kv := memstore.NewKV() 33 kv.Put(t.Context(), blob.PutOptions{ 34 Key: "foo", 35 Data: []byte("bar"), 36 }) 37 kv.Put(t.Context(), blob.PutOptions{ 38 Key: "baz", 39 Data: []byte("quux"), 40 }) 41 kv.Delete(t.Context(), "baz") 42 43 if diff := cmp.Diff(kv.Snapshot(nil), map[string]string{ 44 "foo": "bar", 45 }); diff != "" { 46 t.Errorf("Wrong snapshot: (-want, +got):\n%s", diff) 47 } 48 } 49 50 func TestConsistency(t *testing.T) { 51 ctx := t.Context() 52 data := map[string]string{ 53 "natha": "striped", 54 "zuulie": "roumnd", 55 "thena": "scurred", 56 "asha": "wild", 57 } 58 s := memstore.New(func() blob.KV { 59 return memstore.NewKV().Init(data) 60 }) 61 62 k1 := storetest.SubKV(t, ctx, s, "foo", "bar") 63 k2 := storetest.SubKV(t, ctx, s, "foo", "bar") 64 65 for key, want := range data { 66 got1, err := k1.Get(ctx, key) 67 if err != nil { 68 t.Fatalf("Get 1 key %q: %v", key, err) 69 } 70 got2, err := k2.Get(ctx, key) 71 if err != nil { 72 t.Fatalf("Get 2 key %q: %v", key, err) 73 } 74 if string(got1) != want || string(got2) != want { 75 t.Errorf("Check key %q: got (%q, %q), want %q", key, got1, got2, want) 76 } 77 } 78 } 79 80 func TestReadWhileListing(t *testing.T) { 81 ctx := t.Context() 82 83 want := map[string]string{ 84 "cheddar": "ham", 85 "babou": "dozing", 86 "olive": "slumpt", 87 "monty": "grumpus", 88 "luna": "buckwild", 89 } 90 kv := memstore.NewKV().Init(want) 91 for key, err := range kv.List(ctx, "") { 92 if err != nil { 93 t.Fatalf("Unexpected error from list: %v", err) 94 } 95 got, err := kv.Get(ctx, key) 96 if err != nil { 97 t.Errorf("Get %q: unexpected error: %v", key, err) 98 } else if string(got) != want[key] { 99 t.Errorf("Get %q: got %q, want %q", key, got, want[key]) 100 } 101 } 102 }