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
-26
View File
@@ -1,26 +0,0 @@
package api
import (
"encoding/xml"
_ "github.com/deluan/gosonic/routers"
. "github.com/deluan/gosonic/tests"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestGetLicense(t *testing.T) {
_, w := Get(AddParams("/rest/getLicense.view"), "TestGetLicense")
Convey("Subject: GetLicense Endpoint\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The license should always be valid", func() {
v := new(string)
err := xml.Unmarshal(w.Body.Bytes(), &v)
So(err, ShouldBeNil)
So(w.Body.String(), ShouldContainSubstring, `license valid="true"`)
})
})
}
-26
View File
@@ -1,26 +0,0 @@
package api
import (
_ "github.com/deluan/gosonic/routers"
. "github.com/deluan/gosonic/tests"
"testing"
"encoding/xml"
. "github.com/smartystreets/goconvey/convey"
)
func TestGetMusicFolders(t *testing.T) {
_, w := Get(AddParams("/rest/getMusicFolders.view"), "TestGetMusicFolders")
Convey("Subject: GetMusicFolders Endpoint\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The response should include the default folder", func() {
v := new(string)
err := xml.Unmarshal(w.Body.Bytes(), &v)
So(err, ShouldBeNil)
So(w.Body.String(), ShouldContainSubstring, `musicFolder id="0" name="iTunes Library"`)
})
})
}
-30
View File
@@ -1,30 +0,0 @@
package api
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"
)
func TestPing(t *testing.T) {
_, w := Get(AddParams("/rest/ping.view"), "TestPing")
Convey("Subject: Ping Endpoint\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The result should not be empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
Convey("The result should be a valid ping response", func() {
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "ok")
So(v.Version, ShouldEqual, "1.0.0")
})
})
}
-46
View File
@@ -1,46 +0,0 @@
package api
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"
)
func TestCheckParams(t *testing.T) {
_, w := Get("/rest/ping.view", "TestCheckParams")
Convey("Subject: CheckParams\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The errorCode should be 10", func() {
So(w.Body.String(), ShouldContainSubstring, `error code="10" message=`)
})
Convey("The status should be 'fail'", func() {
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
})
})
}
func TestAuthentication(t *testing.T) {
_, w := Get("/rest/ping.view?u=INVALID&p=INVALID&c=test&v=1.0.0", "TestAuthentication")
Convey("Subject: Authentication\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The errorCode should be 10", func() {
So(w.Body.String(), ShouldContainSubstring, `error code="40" message=`)
})
Convey("The status should be 'fail'", func() {
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
})
})
}
+25
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)
}
}
-43
View File
@@ -1,43 +0,0 @@
package test
import (
"fmt"
"github.com/astaxie/beego"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
)
const (
testUser = "deluan"
testPassword = "wordpass"
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)
}
func Get(url string, testCase string) (*http.Request, *httptest.ResponseRecorder) {
r, _ := http.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
return r, w
}