github.com/mckael/restic@v0.8.3/internal/restic/backend_find_test.go (about)

     1  package restic
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  )
     7  
     8  type mockBackend struct {
     9  	list func(context.Context, FileType, func(FileInfo) error) error
    10  }
    11  
    12  func (m mockBackend) List(ctx context.Context, t FileType, fn func(FileInfo) error) error {
    13  	return m.list(ctx, t, fn)
    14  }
    15  
    16  var samples = IDs{
    17  	TestParseID("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff"),
    18  	TestParseID("20bdc1402a6fc9b633ccd578c4a92d0f4ef1a457fa2e16c596bc73fb409d6cc0"),
    19  	TestParseID("20bdc1402a6fc9b633ffffffffffffffffffffffffffffffffffffffffffffff"),
    20  	TestParseID("20ff988befa5fc40350f00d531a767606efefe242c837aaccb80673f286be53d"),
    21  	TestParseID("326cb59dfe802304f96ee9b5b9af93bdee73a30f53981e5ec579aedb6f1d0f07"),
    22  	TestParseID("86b60b9594d1d429c4aa98fa9562082cabf53b98c7dc083abe5dae31074dd15a"),
    23  	TestParseID("96c8dbe225079e624b5ce509f5bd817d1453cd0a85d30d536d01b64a8669aeae"),
    24  	TestParseID("fa31d65b87affcd167b119e9d3d2a27b8236ca4836cb077ed3e96fcbe209b792"),
    25  }
    26  
    27  func TestPrefixLength(t *testing.T) {
    28  	list := samples
    29  
    30  	m := mockBackend{}
    31  	m.list = func(ctx context.Context, t FileType, fn func(FileInfo) error) error {
    32  		for _, id := range list {
    33  			err := fn(FileInfo{Name: id.String()})
    34  			if err != nil {
    35  				return err
    36  			}
    37  		}
    38  		return nil
    39  	}
    40  
    41  	l, err := PrefixLength(m, SnapshotFile)
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  	if l != 19 {
    46  		t.Errorf("wrong prefix length returned, want %d, got %d", 19, l)
    47  	}
    48  
    49  	list = samples[:3]
    50  	l, err = PrefixLength(m, SnapshotFile)
    51  	if err != nil {
    52  		t.Error(err)
    53  	}
    54  	if l != 19 {
    55  		t.Errorf("wrong prefix length returned, want %d, got %d", 19, l)
    56  	}
    57  
    58  	list = samples[3:]
    59  	l, err = PrefixLength(m, SnapshotFile)
    60  	if err != nil {
    61  		t.Error(err)
    62  	}
    63  	if l != 8 {
    64  		t.Errorf("wrong prefix length returned, want %d, got %d", 8, l)
    65  	}
    66  }