Skip to content

🎧 Your Personal Streaming Service

More getMusicDirectory

Deluan4f5af423a87d
diff --git a/api/get_music_directory.go b/api/get_music_directory.go
index 58991e4ce..8bdb875b3 100644
--- a/api/get_music_directory.go
+++ b/api/get_music_directory.go
@@ -30,15 +30,30 @@ func (c *GetMusicDirectoryController) Get() {
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
+ dir := &responses.Directory{}
if found {
- _, err := c.artistRepo.Get(id)
- if err != nil {
- beego.Error("Error reading Artist from DB", err)
- c.SendError(responses.ERROR_GENERIC, "Internal Error")
- }
+ a, _:= c.retrieveArtist(id)
+
+ dir.Id = a.Id
+ dir.Name = a.Name
+ } else {
+ beego.Info("Artist", id, "not found")
+ c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
}
response := c.NewEmpty()
- response.Directory = &responses.Directory{}
+ response.Directory = dir
c.SendResponse(response)
}
+
+func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as[]domain.Album) {
+ var err error
+
+ if a, err = c.artistRepo.Get(id); err != nil {
+ beego.Error("Error reading Artist from DB", err)
+ c.SendError(responses.ERROR_GENERIC, "Internal Error")
+ }
+
+ as = make([]domain.Album, 0)
+ return
+}
\ No newline at end of file
diff --git a/api/get_music_directory_test.go b/api/get_music_directory_test.go
index c85680a14..cfdc593aa 100644
--- a/api/get_music_directory_test.go
+++ b/api/get_music_directory_test.go
@@ -14,13 +14,13 @@ import (
func TestGetMusicDirectory(t *testing.T) {
Init(t, false)
- mockRepo := mocks.CreateMockArtistRepo()
+ mockArtistRepo := mocks.CreateMockArtistRepo()
utils.DefineSingleton(new(domain.ArtistRepository), func() domain.ArtistRepository {
- return mockRepo
+ return mockArtistRepo
})
- mockRepo.SetData("[]")
- mockRepo.SetError(false)
+ mockArtistRepo.SetData("[]", 0)
+ mockArtistRepo.SetError(false)
Convey("Subject: GetMusicDirectory Endpoint", t, func() {
Convey("Should fail if missing Id parameter", func() {
@@ -30,11 +30,27 @@ func TestGetMusicDirectory(t *testing.T) {
})
Convey("Id is for an artist", func() {
Convey("Return fail on Artist Table error", func() {
- mockRepo.SetError(true)
+ mockArtistRepo.SetError(true)
_, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
So(w.Body, ShouldReceiveError, responses.ERROR_GENERIC)
})
})
+ Convey("When id is not found", func() {
+ mockArtistRepo.SetData(`[{"Id":"1","Name":"The Charlatans"}]`, 1)
+ _, w := Get(AddParams("/rest/getMusicDirectory.view", "id=NOT_FOUND"), "TestGetMusicDirectory")
+
+ So(w.Body, ShouldReceiveError, responses.ERROR_DATA_NOT_FOUND)
+ })
+ Convey("When id matches an artist without albums", func() {
+ mockArtistRepo.SetData(`[{"Id":"1","Name":"The KLF"}]`, 1)
+ _, w := Get(AddParams("/rest/getMusicDirectory.view", "id=1"), "TestGetMusicDirectory")
+
+ So(w.Body, ShouldContainJSON, `"id":"1","name":"The KLF"`)
+ })
+ Reset(func() {
+ mockArtistRepo.SetData("[]", 0)
+ mockArtistRepo.SetError(false)
+ })
})
}
\ No newline at end of file
diff --git a/tests/matchers.go b/tests/matchers.go
index 92ade6aaf..015bd21d5 100644
--- a/tests/matchers.go
+++ b/tests/matchers.go
@@ -27,6 +27,12 @@ func ShouldMatchJSON(actual interface{}, expected ...interface{}) string {
return ShouldEqual(s, expected[0].(string))
}
+func ShouldContainJSON(actual interface{}, expected ...interface{}) string {
+ a := UnindentJSON(actual.(*bytes.Buffer).Bytes())
+
+ return ShouldContainSubstring(a, expected[0].(string))
+}
+
func ShouldReceiveError(actual interface{}, expected ...interface{}) string {
v := responses.Subsonic{}
err := xml.Unmarshal(actual.(*bytes.Buffer).Bytes(), &v)
@@ -35,7 +41,6 @@ func ShouldReceiveError(actual interface{}, expected ...interface{}) string {
}
return ShouldEqual(v.Error.Code, expected[0].(int))
-
}
func UnindentJSON(j []byte) string {
diff --git a/tests/mocks/mock_artist_repo.go b/tests/mocks/mock_artist_repo.go
index 5f3650c5c..c19ed4cdd 100644
--- a/tests/mocks/mock_artist_repo.go
+++ b/tests/mocks/mock_artist_repo.go
@@ -13,7 +13,7 @@ func CreateMockArtistRepo() *MockArtist {
type MockArtist struct {
domain.ArtistRepository
- data map[string]domain.Artist
+ data map[string]*domain.Artist
err bool
}
@@ -21,12 +21,16 @@ func (m *MockArtist) SetError(err bool) {
m.err = err
}
-func (m *MockArtist) SetData(j string) {
- m.data = make(map[string]domain.Artist)
- err := json.Unmarshal([]byte(j), &m.data)
+func (m *MockArtist) SetData(j string, size int) {
+ m.data = make(map[string]*domain.Artist)
+ var l = make([]domain.Artist, size)
+ err := json.Unmarshal([]byte(j), &l)
if err != nil {
fmt.Println("ERROR: ", err)
}
+ for _, a := range l {
+ m.data[a.Id] = &a
+ }
}
func (m *MockArtist) Exists(id string) (bool, error) {
@@ -35,4 +39,11 @@ func (m *MockArtist) Exists(id string) (bool, error) {
}
_, found := m.data[id];
return found, nil
+}
+
+func (m *MockArtist) Get(id string) (*domain.Artist, error) {
+ if m.err {
+ return nil, errors.New("Error!")
+ }
+ return m.data[id], nil
}
\ No newline at end of file