use db.New() for tests
This commit is contained in:
@@ -8,20 +8,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGetIndexes(t *testing.T) {
|
func TestGetIndexes(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetIndexes, []*queryCase{
|
runQueryCases(t, testController.GetIndexes, []*queryCase{
|
||||||
{url.Values{}, "no_args", false},
|
{url.Values{}, "no_args", false},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMusicDirectory(t *testing.T) {
|
func TestGetMusicDirectory(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetMusicDirectory, []*queryCase{
|
runQueryCases(t, testController.GetMusicDirectory, []*queryCase{
|
||||||
{url.Values{"id": []string{"2"}}, "without_tracks", false},
|
{url.Values{"id": []string{"2"}}, "without_tracks", false},
|
||||||
{url.Values{"id": []string{"3"}}, "with_tracks", false},
|
{url.Values{"id": []string{"3"}}, "with_tracks", false},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAlbumList(t *testing.T) {
|
func TestGetAlbumList(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetAlbumList, []*queryCase{
|
runQueryCases(t, testController.GetAlbumList, []*queryCase{
|
||||||
{url.Values{"type": []string{"alphabeticalByArtist"}}, "alpha_artist", false},
|
{url.Values{"type": []string{"alphabeticalByArtist"}}, "alpha_artist", false},
|
||||||
{url.Values{"type": []string{"alphabeticalByName"}}, "alpha_name", false},
|
{url.Values{"type": []string{"alphabeticalByName"}}, "alpha_name", false},
|
||||||
{url.Values{"type": []string{"newest"}}, "newest", false},
|
{url.Values{"type": []string{"newest"}}, "newest", false},
|
||||||
@@ -30,7 +30,7 @@ func TestGetAlbumList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchTwo(t *testing.T) {
|
func TestSearchTwo(t *testing.T) {
|
||||||
testQueryCases(t, testController.SearchTwo, []*queryCase{
|
runQueryCases(t, testController.SearchTwo, []*queryCase{
|
||||||
{url.Values{"query": []string{"13"}}, "q_13", false},
|
{url.Values{"query": []string{"13"}}, "q_13", false},
|
||||||
{url.Values{"query": []string{"ani"}}, "q_ani", false},
|
{url.Values{"query": []string{"ani"}}, "q_ani", false},
|
||||||
{url.Values{"query": []string{"cert"}}, "q_cert", false},
|
{url.Values{"query": []string{"cert"}}, "q_cert", false},
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestGetArtists(t *testing.T) {
|
func TestGetArtists(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetArtists, []*queryCase{
|
runQueryCases(t, testController.GetArtists, []*queryCase{
|
||||||
{url.Values{}, "no_args", false},
|
{url.Values{}, "no_args", false},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetArtist(t *testing.T) {
|
func TestGetArtist(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetArtist, []*queryCase{
|
runQueryCases(t, testController.GetArtist, []*queryCase{
|
||||||
{url.Values{"id": []string{"1"}}, "id_one", false},
|
{url.Values{"id": []string{"1"}}, "id_one", false},
|
||||||
{url.Values{"id": []string{"2"}}, "id_two", false},
|
{url.Values{"id": []string{"2"}}, "id_two", false},
|
||||||
{url.Values{"id": []string{"3"}}, "id_three", false},
|
{url.Values{"id": []string{"3"}}, "id_three", false},
|
||||||
@@ -20,14 +20,14 @@ func TestGetArtist(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAlbum(t *testing.T) {
|
func TestGetAlbum(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetAlbum, []*queryCase{
|
runQueryCases(t, testController.GetAlbum, []*queryCase{
|
||||||
{url.Values{"id": []string{"2"}}, "without_cover", false},
|
{url.Values{"id": []string{"2"}}, "without_cover", false},
|
||||||
{url.Values{"id": []string{"3"}}, "with_cover", false},
|
{url.Values{"id": []string{"3"}}, "with_cover", false},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAlbumListTwo(t *testing.T) {
|
func TestGetAlbumListTwo(t *testing.T) {
|
||||||
testQueryCases(t, testController.GetAlbumListTwo, []*queryCase{
|
runQueryCases(t, testController.GetAlbumListTwo, []*queryCase{
|
||||||
{url.Values{"type": []string{"alphabeticalByArtist"}}, "alpha_artist", false},
|
{url.Values{"type": []string{"alphabeticalByArtist"}}, "alpha_artist", false},
|
||||||
{url.Values{"type": []string{"alphabeticalByName"}}, "alpha_name", false},
|
{url.Values{"type": []string{"alphabeticalByName"}}, "alpha_name", false},
|
||||||
{url.Values{"type": []string{"newest"}}, "newest", false},
|
{url.Values{"type": []string{"newest"}}, "newest", false},
|
||||||
@@ -36,7 +36,7 @@ func TestGetAlbumListTwo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchThree(t *testing.T) {
|
func TestSearchThree(t *testing.T) {
|
||||||
testQueryCases(t, testController.SearchThree, []*queryCase{
|
runQueryCases(t, testController.SearchThree, []*queryCase{
|
||||||
{url.Values{"query": []string{"13"}}, "q_13", false},
|
{url.Values{"query": []string{"13"}}, "q_13", false},
|
||||||
{url.Values{"query": []string{"ani"}}, "q_ani", false},
|
{url.Values{"query": []string{"ani"}}, "q_ani", false},
|
||||||
{url.Values{"query": []string{"cert"}}, "q_cert", false},
|
{url.Values{"query": []string{"cert"}}, "q_cert", false},
|
||||||
|
|||||||
@@ -10,23 +10,26 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
jd "github.com/josephburnett/jd/lib"
|
jd "github.com/josephburnett/jd/lib"
|
||||||
|
|
||||||
|
"github.com/sentriz/gonic/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testController *Controller
|
|
||||||
testDataDir = "test_data"
|
testDataDir = "test_data"
|
||||||
testCamelExpr = regexp.MustCompile("([a-z0-9])([A-Z])")
|
testCamelExpr = regexp.MustCompile("([a-z0-9])([A-Z])")
|
||||||
|
testDBPath = path.Join(testDataDir, "db")
|
||||||
|
testController *Controller
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
testDBPath := path.Join(testDataDir, "db")
|
db, err := db.New(testDBPath)
|
||||||
testDB, err := gorm.Open("sqlite3", testDBPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error opening database: %v\n", err)
|
log.Fatalf("error opening database: %v\n", err)
|
||||||
}
|
}
|
||||||
testController = &Controller{DB: testDB}
|
testController = &Controller{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type queryCase struct {
|
type queryCase struct {
|
||||||
@@ -35,20 +38,16 @@ type queryCase struct {
|
|||||||
listSet bool
|
listSet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func testNameToPath(name string) string {
|
func runQueryCases(t *testing.T, handler http.HandlerFunc, cases []*queryCase) {
|
||||||
snake := testCamelExpr.ReplaceAllString(name, "${1}_${2}")
|
|
||||||
lower := strings.ToLower(snake)
|
|
||||||
relPath := strings.Replace(lower, "/", "_", -1)
|
|
||||||
return path.Join(testDataDir, relPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testQueryCases(t *testing.T, handler http.HandlerFunc, cases []*queryCase) {
|
|
||||||
for _, qc := range cases {
|
for _, qc := range cases {
|
||||||
qc := qc // pin
|
qc := qc // pin
|
||||||
t.Run(qc.expectPath, func(t *testing.T) {
|
t.Run(qc.expectPath, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
//
|
||||||
// ensure the handlers give us json
|
// ensure the handlers give us json
|
||||||
qc.params.Add("f", "json")
|
qc.params.Add("f", "json")
|
||||||
|
//
|
||||||
|
// request from the handler in question
|
||||||
req, _ := http.NewRequest("", "?"+qc.params.Encode(), nil)
|
req, _ := http.NewRequest("", "?"+qc.params.Encode(), nil)
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
handler.ServeHTTP(rr, req)
|
handler.ServeHTTP(rr, req)
|
||||||
@@ -56,7 +55,14 @@ func testQueryCases(t *testing.T, handler http.HandlerFunc, cases []*queryCase)
|
|||||||
if status := rr.Code; status != http.StatusOK {
|
if status := rr.Code; status != http.StatusOK {
|
||||||
t.Fatalf("didn't give a 200\n%s", body)
|
t.Fatalf("didn't give a 200\n%s", body)
|
||||||
}
|
}
|
||||||
absExpPath := testNameToPath(t.Name())
|
//
|
||||||
|
// convert test name to query case path
|
||||||
|
snake := testCamelExpr.ReplaceAllString(t.Name(), "${1}_${2}")
|
||||||
|
lower := strings.ToLower(snake)
|
||||||
|
relPath := strings.Replace(lower, "/", "_", -1)
|
||||||
|
absExpPath := path.Join(testDataDir, relPath)
|
||||||
|
//
|
||||||
|
// read case to differ with handler result
|
||||||
expected, err := jd.ReadJsonFile(absExpPath)
|
expected, err := jd.ReadJsonFile(absExpPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("parsing expected: %v", err)
|
t.Fatalf("parsing expected: %v", err)
|
||||||
@@ -70,6 +76,8 @@ func testQueryCases(t *testing.T, handler http.HandlerFunc, cases []*queryCase)
|
|||||||
diffOpts = append(diffOpts, jd.SET)
|
diffOpts = append(diffOpts, jd.SET)
|
||||||
}
|
}
|
||||||
diff := expected.Diff(actual, diffOpts...)
|
diff := expected.Diff(actual, diffOpts...)
|
||||||
|
//
|
||||||
|
// pass or fail
|
||||||
if len(diff) == 0 {
|
if len(diff) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user