github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/store/cache/cache_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/store/cache/cache_test.go 14 15 package cache 16 17 import ( 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/tickoalcantara12/micro/v3/service/store" 23 "github.com/tickoalcantara12/micro/v3/service/store/file" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func cleanup(db string, s store.Store) { 28 s.Close() 29 dir := filepath.Join(file.DefaultDir, db+"/") 30 os.RemoveAll(dir) 31 } 32 33 func TestRead(t *testing.T) { 34 cf := NewStore(file.NewStore()) 35 cf.Init() 36 cfInt := cf.(*cache) 37 defer cleanup(file.DefaultDatabase, cf) 38 39 _, err := cf.Read("key1") 40 assert.Error(t, err, "Unexpected record") 41 cfInt.b.Write(&store.Record{ 42 Key: "key1", 43 Value: []byte("foo"), 44 }) 45 recs, err := cf.Read("key1") 46 assert.NoError(t, err) 47 assert.Len(t, recs, 1, "Expected a record to be pulled from file store") 48 recs, err = cfInt.m.Read("key1") 49 assert.NoError(t, err) 50 assert.Len(t, recs, 1, "Expected a memory store to be populatedfrom file store") 51 52 } 53 54 func TestWrite(t *testing.T) { 55 cf := NewStore(file.NewStore()) 56 cf.Init() 57 cfInt := cf.(*cache) 58 defer cleanup(file.DefaultDatabase, cf) 59 60 cf.Write(&store.Record{ 61 Key: "key1", 62 Value: []byte("foo"), 63 }) 64 recs, _ := cfInt.m.Read("key1") 65 assert.Len(t, recs, 1, "Expected a record in the memory store") 66 recs, _ = cfInt.b.Read("key1") 67 assert.Len(t, recs, 1, "Expected a record in the file store") 68 69 } 70 71 func TestDelete(t *testing.T) { 72 cf := NewStore(file.NewStore()) 73 cf.Init() 74 cfInt := cf.(*cache) 75 defer cleanup(file.DefaultDatabase, cf) 76 77 cf.Write(&store.Record{ 78 Key: "key1", 79 Value: []byte("foo"), 80 }) 81 recs, _ := cfInt.m.Read("key1") 82 assert.Len(t, recs, 1, "Expected a record in the memory store") 83 recs, _ = cfInt.b.Read("key1") 84 assert.Len(t, recs, 1, "Expected a record in the file store") 85 cf.Delete("key1") 86 87 _, err := cfInt.m.Read("key1") 88 assert.Error(t, err, "Expected no records in memory store") 89 _, err = cfInt.b.Read("key1") 90 assert.Error(t, err, "Expected no records in file store") 91 92 } 93 94 func TestList(t *testing.T) { 95 cf := NewStore(file.NewStore()) 96 cf.Init() 97 cfInt := cf.(*cache) 98 defer cleanup(file.DefaultDatabase, cf) 99 100 keys, err := cf.List() 101 assert.NoError(t, err) 102 assert.Len(t, keys, 0) 103 cfInt.b.Write(&store.Record{ 104 Key: "key1", 105 Value: []byte("foo"), 106 }) 107 108 cfInt.b.Write(&store.Record{ 109 Key: "key2", 110 Value: []byte("foo"), 111 }) 112 keys, err = cf.List() 113 assert.NoError(t, err) 114 assert.Len(t, keys, 2) 115 116 }