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

View File

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

View File

@ -8,6 +8,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
@ -43,7 +45,7 @@ type httpTest struct {
} }
func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder { 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) req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody)

View File

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

View File

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

View File

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