Add tests for Config structure

This commit is contained in:
Marius 2016-03-11 20:46:34 +01:00
parent be8fd83d0d
commit 2e44bad1b0
4 changed files with 74 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package tusd
import (
"errors"
"log"
"net/url"
"os"
@ -56,8 +57,13 @@ func (config *Config) validate() error {
if config.StoreComposer == nil {
config.StoreComposer = newStoreComposerFromDataStore(config.DataStore)
config.DataStore = nil
} else if config.DataStore != nil {
// TODO: consider returning an error
return errors.New("tusd: either StoreComposer or DataStore may be set in Config, but not both")
}
if config.StoreComposer.Core == nil {
return errors.New("tusd: StoreComposer in Config needs to contain a non-nil core")
}
return nil

58
config_test.go Normal file
View File

@ -0,0 +1,58 @@
package tusd
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
)
type zeroStore struct{}
func (store zeroStore) NewUpload(info FileInfo) (string, error) {
return "", nil
}
func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
return 0, nil
}
func (store zeroStore) GetInfo(id string) (FileInfo, error) {
return FileInfo{}, nil
}
func TestConfig(t *testing.T) {
a := assert.New(t)
config := Config{
DataStore: zeroStore{},
BasePath: "files",
}
a.Nil(config.validate())
a.NotNil(config.Logger)
a.NotNil(config.StoreComposer)
a.Equal("/files/", config.BasePath)
}
func TestConfigEmptyCore(t *testing.T) {
a := assert.New(t)
config := Config{
StoreComposer: NewStoreComposer(),
}
a.Error(config.validate())
}
func TestConfigStoreAndComposer(t *testing.T) {
a := assert.New(t)
composer := NewStoreComposer()
composer.UseCore(zeroStore{})
config := Config{
StoreComposer: composer,
DataStore: zeroStore{},
}
a.Error(config.validate())
}

View File

@ -8,7 +8,11 @@ import (
)
func TestCORS(t *testing.T) {
handler, _ := NewHandler(Config{})
store := NewStoreComposer()
store.UseCore(zeroStore{})
handler, _ := NewHandler(Config{
StoreComposer: store,
})
(&httpTest{
Name: "Preflight request",

View File

@ -8,8 +8,11 @@ import (
)
func TestOptions(t *testing.T) {
store := NewStoreComposer()
store.UseCore(zeroStore{})
handler, _ := NewHandler(Config{
MaxSize: 400,
StoreComposer: store,
MaxSize: 400,
})
(&httpTest{