Better tests organization

This commit is contained in:
Deluan 2016-02-27 18:42:08 -05:00
parent ecc0df9e7c
commit ce240cfeff
8 changed files with 54 additions and 35 deletions

View File

@ -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)
}

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -0,0 +1 @@
-short

View File

@ -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)
})
})
}

25
tests/init_tests.go Normal file
View File

@ -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)
}
}