From ce240cfeffa718db3e6e40edb59c767939455651 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 27 Feb 2016 18:42:08 -0500 Subject: [PATCH] Better tests organization --- tests/test_helper.go => api/api_test.go | 23 ++++-------------- {tests/api => api}/get_license_test.go | 7 +++--- {tests/api => api}/get_music_folders_test.go | 7 +++--- {tests/api => api}/ping_test.go | 7 +++--- {tests/api => api}/validation_test.go | 7 +++--- repositories/.goconvey.goconvey | 1 + repositories/init_database_test.go | 12 ++++++---- tests/init_tests.go | 25 ++++++++++++++++++++ 8 files changed, 54 insertions(+), 35 deletions(-) rename tests/test_helper.go => api/api_test.go (60%) rename {tests/api => api}/get_license_test.go (86%) rename {tests/api => api}/get_music_folders_test.go (87%) rename {tests/api => api}/ping_test.go (88%) rename {tests/api => api}/validation_test.go (93%) create mode 100644 repositories/.goconvey.goconvey create mode 100644 tests/init_tests.go diff --git a/tests/test_helper.go b/api/api_test.go similarity index 60% rename from tests/test_helper.go rename to api/api_test.go index b833f8ab6..1d7d45af6 100644 --- a/tests/test_helper.go +++ b/api/api_test.go @@ -1,33 +1,20 @@ -package test +package api_test import ( "fmt" "github.com/astaxie/beego" "net/http" "net/http/httptest" - "os" - "path/filepath" - "runtime" + _ "github.com/deluan/gosonic/routers" ) const ( - testUser = "deluan" + testUser = "deluan" testPassword = "wordpass" - testClient = "test" - testVersion = "1.0.0" + testClient = "test" + testVersion = "1.0.0" ) -func init() { - _, file, _, _ := runtime.Caller(1) - appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator)))) - beego.TestBeegoInit(appPath) - - noLog := os.Getenv("NOLOG") - if noLog != "" { - beego.SetLevel(beego.LevelError) - } -} - func AddParams(url string) string { return fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s", url, testUser, testPassword, testClient, testVersion) } diff --git a/tests/api/get_license_test.go b/api/get_license_test.go similarity index 86% rename from tests/api/get_license_test.go rename to api/get_license_test.go index df07b7999..8fc4078ec 100644 --- a/tests/api/get_license_test.go +++ b/api/get_license_test.go @@ -1,14 +1,15 @@ -package api +package api_test import ( "encoding/xml" - _ "github.com/deluan/gosonic/routers" - . "github.com/deluan/gosonic/tests" . "github.com/smartystreets/goconvey/convey" "testing" + "github.com/deluan/gosonic/tests" ) func TestGetLicense(t *testing.T) { + tests.Init(t, false) + _, w := Get(AddParams("/rest/getLicense.view"), "TestGetLicense") Convey("Subject: GetLicense Endpoint\n", t, func() { diff --git a/tests/api/get_music_folders_test.go b/api/get_music_folders_test.go similarity index 87% rename from tests/api/get_music_folders_test.go rename to api/get_music_folders_test.go index 717c09798..a7ae9a039 100644 --- a/tests/api/get_music_folders_test.go +++ b/api/get_music_folders_test.go @@ -1,15 +1,16 @@ -package api +package api_test import ( - _ "github.com/deluan/gosonic/routers" - . "github.com/deluan/gosonic/tests" "testing" "encoding/xml" . "github.com/smartystreets/goconvey/convey" + "github.com/deluan/gosonic/tests" ) func TestGetMusicFolders(t *testing.T) { + tests.Init(t, false) + _, w := Get(AddParams("/rest/getMusicFolders.view"), "TestGetMusicFolders") Convey("Subject: GetMusicFolders Endpoint\n", t, func() { diff --git a/tests/api/ping_test.go b/api/ping_test.go similarity index 88% rename from tests/api/ping_test.go rename to api/ping_test.go index b0e652630..f44c012bd 100644 --- a/tests/api/ping_test.go +++ b/api/ping_test.go @@ -1,15 +1,16 @@ -package api +package api_test import ( "encoding/xml" "github.com/deluan/gosonic/api/responses" - _ "github.com/deluan/gosonic/routers" - . "github.com/deluan/gosonic/tests" . "github.com/smartystreets/goconvey/convey" "testing" + "github.com/deluan/gosonic/tests" ) func TestPing(t *testing.T) { + tests.Init(t, false) + _, w := Get(AddParams("/rest/ping.view"), "TestPing") Convey("Subject: Ping Endpoint\n", t, func() { diff --git a/tests/api/validation_test.go b/api/validation_test.go similarity index 93% rename from tests/api/validation_test.go rename to api/validation_test.go index 0d840a68d..983857a8f 100644 --- a/tests/api/validation_test.go +++ b/api/validation_test.go @@ -1,15 +1,16 @@ -package api +package api_test import ( "encoding/xml" "github.com/deluan/gosonic/api/responses" - _ "github.com/deluan/gosonic/routers" - . "github.com/deluan/gosonic/tests" . "github.com/smartystreets/goconvey/convey" "testing" + "github.com/deluan/gosonic/tests" ) func TestCheckParams(t *testing.T) { + tests.Init(t, false) + _, w := Get("/rest/ping.view", "TestCheckParams") Convey("Subject: CheckParams\n", t, func() { diff --git a/repositories/.goconvey.goconvey b/repositories/.goconvey.goconvey new file mode 100644 index 000000000..a1e78a951 --- /dev/null +++ b/repositories/.goconvey.goconvey @@ -0,0 +1 @@ +-short \ No newline at end of file diff --git a/repositories/init_database_test.go b/repositories/init_database_test.go index 2e06a4d4e..0d89ced18 100644 --- a/repositories/init_database_test.go +++ b/repositories/init_database_test.go @@ -4,6 +4,7 @@ import ( . "github.com/smartystreets/goconvey/convey" _ "github.com/deluan/gosonic/tests" "testing" + "github.com/deluan/gosonic/tests" ) const ( @@ -11,11 +12,7 @@ const ( ) func TestCreateCollection(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - - dbInstance().Drop(testCollectionName) + tests.Init(t, true) Convey("Given an empty DB", t, func() { @@ -50,5 +47,10 @@ func TestCreateCollection(t *testing.T) { So(allPaths, ShouldContainKey, "Name") }) }) + + Reset(func() { + dbInstance().Drop(testCollectionName) + }) }) + } \ No newline at end of file diff --git a/tests/init_tests.go b/tests/init_tests.go new file mode 100644 index 000000000..f604b662b --- /dev/null +++ b/tests/init_tests.go @@ -0,0 +1,25 @@ +package tests + +import ( + "github.com/astaxie/beego" + "os" + "path/filepath" + "runtime" + "testing" +) + +func Init(t *testing.T, skipOnShort bool) { + if skipOnShort && testing.Short() { + t.Skip("skipping test in short mode.") + } + + _, file, _, _ := runtime.Caller(0) + appPath, _ := filepath.Abs(filepath.Join(filepath.Dir(file), "..")) + beego.TestBeegoInit(appPath) + + noLog := os.Getenv("NOLOG") + if noLog != "" { + beego.SetLevel(beego.LevelError) + } +} +