From 2e44bad1b08bfa4e758a2c7bf22938fc24b21256 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 11 Mar 2016 20:46:34 +0100 Subject: [PATCH] Add tests for Config structure --- config.go | 8 ++++++- config_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ cors_test.go | 6 ++++- options_test.go | 5 ++++- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 config_test.go diff --git a/config.go b/config.go index 3e5e144..e84babc 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..00736a6 --- /dev/null +++ b/config_test.go @@ -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()) +} diff --git a/cors_test.go b/cors_test.go index d1f0209..5033b80 100644 --- a/cors_test.go +++ b/cors_test.go @@ -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", diff --git a/options_test.go b/options_test.go index e82eb88..1cc304b 100644 --- a/options_test.go +++ b/options_test.go @@ -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{