2016-03-01 05:38:43 -09:00
package api_test
import (
"testing"
2016-03-01 08:35:30 -09:00
"github.com/deluan/gosonic/utils"
2016-03-01 05:38:43 -09:00
. "github.com/smartystreets/goconvey/convey"
"github.com/deluan/gosonic/tests"
2016-03-01 08:35:30 -09:00
"encoding/xml"
"github.com/deluan/gosonic/api/responses"
2016-03-01 10:40:26 -09:00
"github.com/deluan/gosonic/consts"
"github.com/deluan/gosonic/tests/mocks"
2016-03-02 05:07:24 -09:00
"github.com/deluan/gosonic/domain"
2016-03-01 10:40:26 -09:00
)
const (
emptyResponse = ` <indexes lastModified="1" ignoredArticles="The El La Los Las Le Les Os As O A"></indexes> `
2016-03-01 05:38:43 -09:00
)
func TestGetIndexes ( t * testing . T ) {
tests . Init ( t , false )
2016-03-01 10:40:26 -09:00
mockRepo := mocks . CreateMockArtistIndexRepo ( )
2016-03-02 05:07:24 -09:00
utils . DefineSingleton ( new ( domain . ArtistIndexRepository ) , func ( ) domain . ArtistIndexRepository {
2016-03-01 08:35:30 -09:00
return mockRepo
} )
2016-03-01 10:40:26 -09:00
propRepo := mocks . CreateMockPropertyRepo ( )
2016-03-02 05:07:24 -09:00
utils . DefineSingleton ( new ( domain . PropertyRepository ) , func ( ) domain . PropertyRepository {
2016-03-01 10:40:26 -09:00
return propRepo
} )
2016-03-01 05:38:43 -09:00
2016-03-01 12:05:49 -09:00
mockRepo . SetData ( "[]" , 0 )
mockRepo . SetError ( false )
propRepo . Put ( consts . LastScan , "1" )
propRepo . SetError ( false )
2016-03-01 05:38:43 -09:00
Convey ( "Subject: GetIndexes Endpoint" , t , func ( ) {
2016-03-01 10:40:26 -09:00
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" )
2016-03-01 10:40:26 -09:00
v := responses . Subsonic { }
xml . Unmarshal ( w . Body . Bytes ( ) , & v )
So ( v . Status , ShouldEqual , "fail" )
} )
Convey ( "Return fail on Property Table error" , func ( ) {
propRepo . SetError ( true )
2016-03-01 08:35:30 -09:00
_ , w := Get ( AddParams ( "/rest/getIndexes.view" ) , "TestGetIndexes" )
v := responses . Subsonic { }
xml . Unmarshal ( w . Body . Bytes ( ) , & v )
So ( v . Status , ShouldEqual , "fail" )
} )
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 ( "It should return valid XML" , func ( ) {
v := new ( string )
err := xml . Unmarshal ( w . Body . Bytes ( ) , & v )
So ( err , ShouldBeNil )
} )
Convey ( "Then it should return an empty collection" , func ( ) {
2016-03-01 10:40:26 -09:00
So ( w . Body . String ( ) , ShouldContainSubstring , emptyResponse )
2016-03-01 08:35:30 -09:00
} )
} )
Convey ( "When the index is not empty" , func ( ) {
2016-03-01 10:40:26 -09:00
mockRepo . SetData ( ` [ { "Id" : "A" , "Artists" : [
2016-03-01 09:15:23 -09:00
{ "ArtistId" : "21" , "Artist" : "Afrolicious" }
] } ] ` , 2 )
2016-03-01 08:35:30 -09:00
Convey ( "Then it should return the the items in the response" , func ( ) {
2016-03-01 10:40:26 -09:00
_ , w := Get ( AddParams ( "/rest/getIndexes.view" ) , "TestGetIndexes" )
2016-03-01 09:15:23 -09:00
So ( w . Body . String ( ) , ShouldContainSubstring ,
2016-03-01 10:40:26 -09:00
` <indexes lastModified="1" ignoredArticles="The El La Los Las Le Les Os As O A"><index name="A"><artist id="21" name="Afrolicious"></artist></index></indexes> ` )
2016-03-01 08:35:30 -09:00
} )
} )
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 ( w . Body . String ( ) , ShouldContainSubstring , emptyResponse )
} )
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 ( w . Body . String ( ) , ShouldContainSubstring , emptyResponse )
} )
2016-03-01 08:35:30 -09:00
Reset ( func ( ) {
2016-03-01 10:40:26 -09:00
mockRepo . SetData ( "[]" , 0 )
mockRepo . SetError ( false )
propRepo . Put ( consts . LastScan , "1" )
propRepo . SetError ( false )
2016-03-01 05:38:43 -09:00
} )
} )
2016-03-01 08:35:30 -09:00
}