package diskcache import ( "context" "path/filepath" "testing" "github.com/spf13/afero" "github.com/stretchr/testify/require" ) func TestFileCache(t *testing.T) { ctx := context.Background() const ( key = "key" value = "some text" newValue = "new text" cacheRoot = "/cache" cachedFilePath = "a/62/a62f2225bf70bfaccbc7f1ef2a397836717377de" ) fs := afero.NewMemMapFs() cache := New(fs, "/cache") // store new key err := cache.Store(ctx, key, []byte(value)) require.NoError(t, err) checkValue(t, ctx, fs, filepath.Join(cacheRoot, cachedFilePath), cache, key, value) // update existing key err = cache.Store(ctx, key, []byte(newValue)) require.NoError(t, err) checkValue(t, ctx, fs, filepath.Join(cacheRoot, cachedFilePath), cache, key, newValue) // delete key err = cache.Delete(ctx, key) require.NoError(t, err) exists, err := afero.Exists(fs, filepath.Join(cacheRoot, cachedFilePath)) require.NoError(t, err) require.False(t, exists) } func checkValue(t *testing.T, ctx context.Context, fs afero.Fs, fileFullPath string, cache *FileCache, key, wantValue string) { //nolint:revive t.Helper() // check actual file content b, err := afero.ReadFile(fs, fileFullPath) require.NoError(t, err) require.Equal(t, wantValue, string(b)) // check cache content b, ok, err := cache.Load(ctx, key) require.NoError(t, err) require.True(t, ok) require.Equal(t, wantValue, string(b)) }