tusd/limitedstore/limitedstore_test.go

101 lines
2.1 KiB
Go
Raw Normal View History

2015-04-11 11:40:39 +00:00
package limitedstore
import (
"github.com/tus/tusd"
"io"
2015-11-26 11:43:49 +00:00
"strconv"
2015-04-11 11:40:39 +00:00
"testing"
)
type dataStore struct {
2015-11-26 11:43:49 +00:00
t *testing.T
numCreatedUploads int
numTerminatedUploads int
2015-04-11 11:40:39 +00:00
}
func (store *dataStore) NewUpload(info tusd.FileInfo) (string, error) {
2015-11-26 11:43:49 +00:00
uploadId := store.numCreatedUploads
2015-04-11 11:40:39 +00:00
2015-11-26 11:43:49 +00:00
// We expect the uploads to be created in a specific order.
// These sizes correlate to this order.
expectedSize := []int64{30, 60, 80}[uploadId]
2015-04-11 11:40:39 +00:00
2015-11-26 11:43:49 +00:00
if info.Size != expectedSize {
store.t.Errorf("expect size to be %v, got %v", expectedSize, info.Size)
2015-04-11 11:40:39 +00:00
}
2015-11-26 11:43:49 +00:00
store.numCreatedUploads += 1
return strconv.Itoa(uploadId), nil
2015-04-11 11:40:39 +00:00
}
func (store *dataStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
return 0, nil
}
func (store *dataStore) GetInfo(id string) (tusd.FileInfo, error) {
return tusd.FileInfo{}, nil
}
func (store *dataStore) GetReader(id string) (io.Reader, error) {
return nil, tusd.ErrNotImplemented
}
func (store *dataStore) Terminate(id string) error {
2015-11-26 11:43:49 +00:00
// We expect the uploads to be terminated in a specific order (the bigger
// come first)
expectedUploadId := []string{"1", "0"}[store.numTerminatedUploads]
if id != expectedUploadId {
store.t.Errorf("exptect upload %v to be terminated, got %v", expectedUploadId, id)
2015-04-11 11:40:39 +00:00
}
2015-11-26 11:43:49 +00:00
store.numTerminatedUploads += 1
2015-04-11 11:40:39 +00:00
return nil
}
func TestLimitedStore(t *testing.T) {
dataStore := &dataStore{
t: t,
}
store := New(100, dataStore)
2015-11-26 11:43:49 +00:00
// Create new upload (30 bytes)
2015-04-11 11:40:39 +00:00
id, err := store.NewUpload(tusd.FileInfo{
2015-11-26 11:43:49 +00:00
Size: 30,
2015-04-11 11:40:39 +00:00
})
if err != nil {
t.Fatal(err)
}
2015-11-26 11:43:49 +00:00
if id != "0" {
2015-04-11 11:40:39 +00:00
t.Errorf("expected first upload to be created, got %v", id)
}
2015-11-26 11:43:49 +00:00
// Create new upload (60 bytes)
2015-04-11 11:40:39 +00:00
id, err = store.NewUpload(tusd.FileInfo{
2015-11-26 11:43:49 +00:00
Size: 60,
2015-04-11 11:40:39 +00:00
})
if err != nil {
t.Fatal(err)
}
2015-11-26 11:43:49 +00:00
if id != "1" {
2015-04-11 11:40:39 +00:00
t.Errorf("expected second upload to be created, got %v", id)
}
2015-11-26 11:43:49 +00:00
// Create new upload (80 bytes)
id, err = store.NewUpload(tusd.FileInfo{
Size: 80,
})
if err != nil {
t.Fatal(err)
}
if id != "2" {
t.Errorf("expected thrid upload to be created, got %v", id)
}
if dataStore.numTerminatedUploads != 2 {
t.Error("expected two uploads to be terminated")
2015-04-11 11:40:39 +00:00
}
}