github.com/artpar/rclone@v1.67.3/cmd/serve/restic/cache_test.go (about)

     1  package restic
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/artpar/rclone/fstest/mockobject"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func (c *cache) String() string {
    13  	keys := []string{}
    14  	c.mu.Lock()
    15  	for k := range c.items {
    16  		keys = append(keys, k)
    17  	}
    18  	c.mu.Unlock()
    19  	sort.Strings(keys)
    20  	return strings.Join(keys, ",")
    21  }
    22  
    23  func TestCacheCRUD(t *testing.T) {
    24  	c := newCache(true)
    25  	assert.Equal(t, "", c.String())
    26  	assert.Nil(t, c.find("potato"))
    27  	o := mockobject.New("potato")
    28  	c.add(o.Remote(), o)
    29  	assert.Equal(t, "potato", c.String())
    30  	assert.Equal(t, o, c.find("potato"))
    31  	c.remove("potato")
    32  	assert.Equal(t, "", c.String())
    33  	assert.Nil(t, c.find("potato"))
    34  	c.remove("notfound")
    35  }
    36  
    37  func TestCacheRemovePrefix(t *testing.T) {
    38  	c := newCache(true)
    39  	for _, remote := range []string{
    40  		"a",
    41  		"b",
    42  		"b/1",
    43  		"b/2/3",
    44  		"b/2/4",
    45  		"b/2",
    46  		"c",
    47  	} {
    48  		c.add(remote, mockobject.New(remote))
    49  	}
    50  	assert.Equal(t, "a,b,b/1,b/2,b/2/3,b/2/4,c", c.String())
    51  	c.removePrefix("b")
    52  	assert.Equal(t, "a,b,c", c.String())
    53  	c.removePrefix("/")
    54  	assert.Equal(t, "", c.String())
    55  }