kra-oa/internal/biz/media_test.go

82 lines
2.3 KiB
Go

package biz
import (
"context"
"io"
"strings"
"testing"
)
type listStorage struct {
items []*StoredFile
limits []int
}
func (*listStorage) Put(context.Context, string, io.Reader) (*StoredFile, error) {
return nil, nil
}
func (*listStorage) Open(context.Context, string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("")), nil
}
func (*listStorage) Delete(context.Context, string) error { return nil }
func (*listStorage) Compose(context.Context, []string, string) (*StoredFile, string, error) {
return nil, "", nil
}
func (*listStorage) DeletePrefix(context.Context, string) error { return nil }
func (s *listStorage) List(_ context.Context, _ string, cursor string, limit int) ([]*StoredFile, string, bool, error) {
s.limits = append(s.limits, limit)
start := 0
for start < len(s.items) && s.items[start].Path <= cursor {
start++
}
end := start + limit
if end > len(s.items) {
end = len(s.items)
}
page := s.items[start:end]
more := end < len(s.items)
next := ""
if len(page) > 0 {
next = page[len(page)-1].Path
}
return page, next, more, nil
}
func TestListStorageFiltersChunksWithoutSkippingVisibleObjects(t *testing.T) {
storage := &listStorage{items: []*StoredFile{
{Path: "uploads/chunks/1/00000000"},
{Path: "uploads/chunks/1/00000001"},
{Path: "visible/a"},
{Path: "visible/b"},
{Path: "visible/c"},
{Path: "visible/d"},
{Path: "visible/e"},
}}
uc := NewMediaUsecase(nil, storage, nil)
first, cursor, more, err := uc.ListStorage(context.Background(), "", "", 3)
if err != nil {
t.Fatal(err)
}
if got := []string{first[0].Path, first[1].Path, first[2].Path}; strings.Join(got, ",") != "visible/a,visible/b,visible/c" {
t.Fatalf("first page = %v", got)
}
if cursor != "visible/c" || !more {
t.Fatalf("first cursor/more = %q/%v", cursor, more)
}
if len(storage.limits) != 2 || storage.limits[0] != 3 || storage.limits[1] != 2 {
t.Fatalf("storage limits = %v, want [3 2]", storage.limits)
}
second, cursor, more, err := uc.ListStorage(context.Background(), "", cursor, 3)
if err != nil {
t.Fatal(err)
}
if got := []string{second[0].Path, second[1].Path}; strings.Join(got, ",") != "visible/d,visible/e" {
t.Fatalf("second page = %v", got)
}
if cursor != "visible/e" || more {
t.Fatalf("second cursor/more = %q/%v", cursor, more)
}
}