quintodrome/api/get_indexes_test.go

100 lines
3.2 KiB
Go
Raw Normal View History

package api_test
import (
"testing"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/consts"
2016-03-02 05:07:24 -09:00
"github.com/deluan/gosonic/domain"
2016-03-08 14:40:16 -09:00
"github.com/deluan/gosonic/engine"
2016-03-02 20:46:23 -09:00
. "github.com/deluan/gosonic/tests"
2016-03-02 09:18:39 -09:00
"github.com/deluan/gosonic/tests/mocks"
"github.com/deluan/gosonic/utils"
. "github.com/smartystreets/goconvey/convey"
)
const (
emptyResponse = `{"indexes":{"ignoredArticles":"The El La Los Las Le Les Os As O A","lastModified":"1"}`
)
func TestGetIndexes(t *testing.T) {
Init(t, false)
mockRepo := mocks.CreateMockArtistIndexRepo()
2016-03-02 05:07:24 -09:00
utils.DefineSingleton(new(domain.ArtistIndexRepository), func() domain.ArtistIndexRepository {
return mockRepo
})
propRepo := mocks.CreateMockPropertyRepo()
2016-03-08 14:40:16 -09:00
utils.DefineSingleton(new(engine.PropertyRepository), func() engine.PropertyRepository {
return propRepo
})
2016-03-01 12:05:49 -09:00
mockRepo.SetData("[]", 0)
mockRepo.SetError(false)
propRepo.Put(consts.LastScan, "1")
propRepo.SetError(false)
Convey("Subject: GetIndexes Endpoint", t, func() {
Convey("Return fail on Index Table error", func() {
mockRepo.SetError(true)
2016-03-01 12:05:49 -09:00
_, w := Get(AddParams("/rest/getIndexes.view", "ifModifiedSince=0"), "TestGetIndexes")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
Convey("Return fail on Property Table error", func() {
propRepo.SetError(true)
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
Convey("When the index is empty", func() {
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("Then it should return an empty collection", func() {
So(UnindentJSON(w.Body.Bytes()), ShouldContainSubstring, emptyResponse)
})
})
Convey("When the index is not empty", func() {
mockRepo.SetData(`[{"Id": "A","Artists": [
{"ArtistId": "21", "Artist": "Afrolicious"}
]}]`, 2)
SkipConvey("Then it should return the the items in the response", func() {
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
So(w.Body.String(), ShouldContainSubstring,
`<index name="A"><artist id="21" name="Afrolicious"></artist></index>`)
})
})
2016-03-01 12:05:49 -09:00
Convey("And it should return empty if 'ifModifiedSince' is more recent than the index", func() {
mockRepo.SetData(`[{"Id": "A","Artists": [
{"ArtistId": "21", "Artist": "Afrolicious"}
]}]`, 2)
propRepo.Put(consts.LastScan, "1")
_, w := Get(AddParams("/rest/getIndexes.view", "ifModifiedSince=2"), "TestGetIndexes")
So(UnindentJSON(w.Body.Bytes()), ShouldContainSubstring, emptyResponse)
2016-03-01 12:05:49 -09:00
})
Convey("And it should return empty if 'ifModifiedSince' is the asme as tie index last update", func() {
mockRepo.SetData(`[{"Id": "A","Artists": [
{"ArtistId": "21", "Artist": "Afrolicious"}
]}]`, 2)
propRepo.Put(consts.LastScan, "1")
_, w := Get(AddParams("/rest/getIndexes.view", "ifModifiedSince=1"), "TestGetIndexes")
So(UnindentJSON(w.Body.Bytes()), ShouldContainSubstring, emptyResponse)
2016-03-01 12:05:49 -09:00
})
Reset(func() {
mockRepo.SetData("[]", 0)
mockRepo.SetError(false)
propRepo.Put(consts.LastScan, "1")
propRepo.SetError(false)
})
})
2016-03-02 09:18:39 -09:00
}