github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/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) <-chan string
    10  }
    11  
    12  func (m mockBackend) List(ctx context.Context, t FileType) <-chan string {
    13  	return m.list(ctx, t)
    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) <-chan string {
    32  		ch := make(chan string)
    33  		go func() {
    34  			defer close(ch)
    35  			for _, id := range list {
    36  				select {
    37  				case ch <- id.String():
    38  				case <-ctx.Done():
    39  					return
    40  				}
    41  			}
    42  		}()
    43  		return ch
    44  	}
    45  
    46  	l, err := PrefixLength(m, SnapshotFile)
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  	if l != 19 {
    51  		t.Errorf("wrong prefix length returned, want %d, got %d", 19, l)
    52  	}
    53  
    54  	list = samples[:3]
    55  	l, err = PrefixLength(m, SnapshotFile)
    56  	if err != nil {
    57  		t.Error(err)
    58  	}
    59  	if l != 19 {
    60  		t.Errorf("wrong prefix length returned, want %d, got %d", 19, l)
    61  	}
    62  
    63  	list = samples[3:]
    64  	l, err = PrefixLength(m, SnapshotFile)
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  	if l != 8 {
    69  		t.Errorf("wrong prefix length returned, want %d, got %d", 8, l)
    70  	}
    71  }