Use assertions library for tests

This commit is contained in:
Marius 2016-01-20 16:40:13 +01:00
parent b6a28421af
commit 0e750291c9
6 changed files with 85 additions and 176 deletions

View File

@ -2,29 +2,22 @@ package tusd_test
import (
"net/http"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd"
)
type concatPartialStore struct {
t *testing.T
t *assert.Assertions
zeroStore
}
func (s concatPartialStore) NewUpload(info FileInfo) (string, error) {
if !info.IsPartial {
s.t.Error("expected upload to be partial")
}
if info.IsFinal {
s.t.Error("expected upload to not be final")
}
if len(info.PartialUploads) != 0 {
s.t.Error("expected no partial uploads")
}
s.t.True(info.IsPartial)
s.t.False(info.IsFinal)
s.t.Nil(info.PartialUploads)
return "foo", nil
}
@ -44,7 +37,7 @@ func TestConcatPartial(t *testing.T) {
MaxSize: 400,
BasePath: "files",
DataStore: concatPartialStore{
t: t,
t: assert.New(t),
},
})
@ -84,22 +77,14 @@ func TestConcatPartial(t *testing.T) {
}
type concatFinalStore struct {
t *testing.T
t *assert.Assertions
zeroStore
}
func (s concatFinalStore) NewUpload(info FileInfo) (string, error) {
if info.IsPartial {
s.t.Error("expected upload to not be partial")
}
if !info.IsFinal {
s.t.Error("expected upload to be final")
}
if !reflect.DeepEqual(info.PartialUploads, []string{"a", "b"}) {
s.t.Error("unexpected partial uploads")
}
s.t.False(info.IsPartial)
s.t.True(info.IsFinal)
s.t.Equal([]string{"a", "b"}, info.PartialUploads)
return "foo", nil
}
@ -134,13 +119,9 @@ func (s concatFinalStore) GetInfo(id string) (FileInfo, error) {
}
func (s concatFinalStore) ConcatUploads(id string, uploads []string) error {
if id != "foo" {
s.t.Error("expected final file id to be foo")
}
s.t.Equal("foo", id)
s.t.Equal([]string{"a", "b"}, uploads)
if !reflect.DeepEqual(uploads, []string{"a", "b"}) {
s.t.Errorf("expected Concatenating uploads to be a and b")
}
return nil
}
@ -149,7 +130,7 @@ func TestConcatFinal(t *testing.T) {
MaxSize: 400,
BasePath: "files",
DataStore: concatFinalStore{
t: t,
t: assert.New(t),
},
})
@ -192,7 +173,7 @@ func TestConcatFinal(t *testing.T) {
MaxSize: 9,
BasePath: "files",
DataStore: concatFinalStore{
t: t,
t: assert.New(t),
},
})

View File

@ -7,6 +7,8 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tus/tusd"
)
@ -18,10 +20,10 @@ var _ tusd.LockerDataStore = FileStore{}
var _ tusd.ConcaterDataStore = FileStore{}
func TestFilestore(t *testing.T) {
a := assert.New(t)
tmp, err := ioutil.TempDir("", "tusd-filestore-")
if err != nil {
t.Fatal(err)
}
a.NoError(err)
store := FileStore{tmp}
@ -32,96 +34,55 @@ func TestFilestore(t *testing.T) {
"hello": "world",
},
})
if err != nil {
t.Fatal(err)
}
if id == "" {
t.Errorf("id must not be empty")
}
a.NoError(err)
a.NotEqual("", id)
// Check info without writing
info, err := store.GetInfo(id)
if err != nil {
t.Fatal(err)
}
if info.Size != 42 {
t.Errorf("expected size to be 42")
}
if info.Offset != 0 {
t.Errorf("expected offset to be 0")
}
if len(info.MetaData) != 1 || info.MetaData["hello"] != "world" {
t.Errorf("expected metadata to have one value")
}
a.NoError(err)
a.EqualValues(42, info.Size)
a.EqualValues(0, info.Offset)
a.Equal(tusd.MetaData{"hello": "world"}, info.MetaData)
// Write data to upload
bytesWritten, err := store.WriteChunk(id, 0, strings.NewReader("hello world"))
if err != nil {
t.Fatal(err)
}
if bytesWritten != int64(len("hello world")) {
t.Errorf("expected 11 bytes to be written")
}
a.NoError(err)
a.EqualValues(len("hello world"), bytesWritten)
// Check new offset
info, err = store.GetInfo(id)
if err != nil {
t.Fatal(err)
}
if info.Size != 42 {
t.Errorf("expected size to be 42")
}
if info.Offset != int64(len("hello world")) {
t.Errorf("expected offset to be 0")
}
a.NoError(err)
a.EqualValues(42, info.Size)
a.EqualValues(11, info.Offset)
// Read content
reader, err := store.GetReader(id)
if err != nil {
t.Fatal(err)
}
a.NoError(err)
content, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
if string(content) != "hello world" {
t.Errorf("expected content to be 'hello world'")
}
a.NoError(err)
a.Equal("hello world", string(content))
reader.(io.Closer).Close()
// Terminate upload
if err := store.Terminate(id); err != nil {
t.Fatal(err)
}
a.NoError(store.Terminate(id))
// Test if upload is deleted
if _, err := store.GetInfo(id); !os.IsNotExist(err) {
t.Fatal("expected os.ErrIsNotExist")
}
_, err = store.GetInfo(id)
a.True(os.IsNotExist(err))
}
func TestFileLocker(t *testing.T) {
a := assert.New(t)
dir, err := ioutil.TempDir("", "tusd-file-locker")
if err != nil {
t.Fatal(err)
}
a.NoError(err)
var locker tusd.LockerDataStore
locker = FileStore{dir}
if err := locker.LockUpload("one"); err != nil {
t.Errorf("unexpected error when locking file: %s", err)
}
if err := locker.LockUpload("one"); err != tusd.ErrFileLocked {
t.Errorf("expected error when locking locked file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file again: %s", err)
}
a.NoError(locker.LockUpload("one"))
a.Equal(tusd.ErrFileLocked, locker.LockUpload("one"))
a.NoError(locker.UnlockUpload("one"))
a.NoError(locker.UnlockUpload("one"))
}

View File

@ -8,6 +8,8 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd"
)
@ -43,7 +45,7 @@ type httpTest struct {
}
func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder {
t.Log(test.Name)
t.Logf("'%s' in %s", test.Name, assert.CallerInfo()[1])
req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody)

View File

@ -1,14 +1,17 @@
package limitedstore
import (
"github.com/tus/tusd"
"io"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tus/tusd"
)
type dataStore struct {
t *testing.T
t *assert.Assertions
numCreatedUploads int
numTerminatedUploads int
}
@ -20,9 +23,7 @@ func (store *dataStore) NewUpload(info tusd.FileInfo) (string, error) {
// These sizes correlate to this order.
expectedSize := []int64{30, 60, 80}[uploadId]
if info.Size != expectedSize {
store.t.Errorf("expect size to be %v, got %v", expectedSize, info.Size)
}
store.t.Equal(expectedSize, info.Size)
store.numCreatedUploads += 1
@ -46,9 +47,7 @@ func (store *dataStore) Terminate(id string) error {
// come first)
expectedUploadId := []string{"1", "0"}[store.numTerminatedUploads]
if id != expectedUploadId {
store.t.Errorf("exptect upload %v to be terminated, got %v", expectedUploadId, id)
}
store.t.Equal(expectedUploadId, id)
store.numTerminatedUploads += 1
@ -56,8 +55,9 @@ func (store *dataStore) Terminate(id string) error {
}
func TestLimitedStore(t *testing.T) {
a := assert.New(t)
dataStore := &dataStore{
t: t,
t: a,
}
store := New(100, dataStore)
@ -65,34 +65,22 @@ func TestLimitedStore(t *testing.T) {
id, err := store.NewUpload(tusd.FileInfo{
Size: 30,
})
if err != nil {
t.Fatal(err)
}
if id != "0" {
t.Errorf("expected first upload to be created, got %v", id)
}
a.NoError(err)
a.Equal("0", id)
// Create new upload (60 bytes)
id, err = store.NewUpload(tusd.FileInfo{
Size: 60,
})
if err != nil {
t.Fatal(err)
}
if id != "1" {
t.Errorf("expected second upload to be created, got %v", id)
}
a.NoError(err)
a.Equal("1", id)
// 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)
}
a.NoError(err)
a.Equal("2", id)
if dataStore.numTerminatedUploads != 2 {
t.Error("expected two uploads to be terminated")

View File

@ -4,6 +4,8 @@ import (
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tus/tusd"
)
@ -25,22 +27,13 @@ func (store zeroStore) GetReader(id string) (io.Reader, error) {
}
func TestMemoryLocker(t *testing.T) {
a := assert.New(t)
var locker tusd.LockerDataStore
locker = NewMemoryLocker(&zeroStore{})
if err := locker.LockUpload("one"); err != nil {
t.Errorf("unexpected error when locking file: %s", err)
}
if err := locker.LockUpload("one"); err != tusd.ErrFileLocked {
t.Errorf("expected error when locking locked file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file: %s", err)
}
if err := locker.UnlockUpload("one"); err != nil {
t.Errorf("unexpected error when unlocking file again: %s", err)
}
a.NoError(locker.LockUpload("one"))
a.Equal(tusd.ErrFileLocked, locker.LockUpload("one"))
a.NoError(locker.UnlockUpload("one"))
a.NoError(locker.UnlockUpload("one"))
}

View File

@ -8,12 +8,14 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd"
)
type patchStore struct {
zeroStore
t *testing.T
t *assert.Assertions
called bool
}
@ -29,23 +31,14 @@ func (s patchStore) GetInfo(id string) (FileInfo, error) {
}
func (s patchStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
if s.called {
s.t.Errorf("WriteChunk must be called only once")
}
s.t.False(s.called, "WriteChunk must be called only once")
s.called = true
if offset != 5 {
s.t.Errorf("Expected offset to be 5 (got %v)", offset)
}
s.t.Equal(int64(5), offset)
data, err := ioutil.ReadAll(src)
if err != nil {
s.t.Error(err)
}
if string(data) != "hello" {
s.t.Errorf("Expected source to be 'hello'")
}
s.t.Nil(err)
s.t.Equal("hello", string(data))
return 5, nil
}
@ -54,7 +47,7 @@ func TestPatch(t *testing.T) {
handler, _ := NewHandler(Config{
MaxSize: 100,
DataStore: patchStore{
t: t,
t: assert.New(t),
},
})
@ -114,7 +107,7 @@ func TestPatch(t *testing.T) {
type overflowPatchStore struct {
zeroStore
t *testing.T
t *assert.Assertions
called bool
}
@ -130,23 +123,14 @@ func (s overflowPatchStore) GetInfo(id string) (FileInfo, error) {
}
func (s overflowPatchStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
if s.called {
s.t.Errorf("WriteChunk must be called only once")
}
s.t.False(s.called, "WriteChunk must be called only once")
s.called = true
if offset != 5 {
s.t.Errorf("Expected offset to be 5 (got %v)", offset)
}
s.t.Equal(int64(5), offset)
data, err := ioutil.ReadAll(src)
if err != nil {
s.t.Error(err)
}
if len(data) != 15 {
s.t.Errorf("Expected 15 bytes got %v", len(data))
}
s.t.Nil(err)
s.t.Equal("hellothisismore", string(data))
return 15, nil
}
@ -183,7 +167,7 @@ func TestPatchOverflow(t *testing.T) {
handler, _ := NewHandler(Config{
MaxSize: 100,
DataStore: overflowPatchStore{
t: t,
t: assert.New(t),
},
})