fix(lidarr): wrap MBID-lookup decode errors with ErrInvalidPayload + fill test gaps
- Decode errors in LookupArtist/AlbumByMBID now wrap ErrInvalidPayload so callers can errors.Is the same way they do against the M5a methods. - Tighten TestLookupAlbumByMBID_BadJSON to assert on ErrInvalidPayload. - Add empty-mbid rejection tests for both methods. - Add TestLookupArtistByMBID_EmptyArrayReturnsErrNotFound (only the album variant had the test before — symmetric coverage now). - Normalize the network-error test's api key to key123 for consistency with the rest of delete_test.go.
This commit is contained in:
@@ -86,8 +86,44 @@ func TestLookupAlbumByMBID_BadJSON(t *testing.T) {
|
|||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
_, err := c.LookupAlbumByMBID(context.Background(), "x")
|
_, err := c.LookupAlbumByMBID(context.Background(), "x")
|
||||||
if err == nil {
|
if !errors.Is(err, ErrInvalidPayload) {
|
||||||
t.Fatal("err = nil, want decode error")
|
t.Errorf("err = %v, want ErrInvalidPayload", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupAlbumByMBID_EmptyMBIDRejected(t *testing.T) {
|
||||||
|
c, srv := newTestClient(func(http.ResponseWriter, *http.Request) {
|
||||||
|
t.Error("server should not be called with empty mbid")
|
||||||
|
})
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
if _, err := c.LookupAlbumByMBID(context.Background(), ""); err == nil {
|
||||||
|
t.Error("err = nil, want empty-mbid rejection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupArtistByMBID_EmptyArrayReturnsErrNotFound(t *testing.T) {
|
||||||
|
c, srv := newTestClient(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte("[]"))
|
||||||
|
})
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
_, err := c.LookupArtistByMBID(context.Background(), "x")
|
||||||
|
if !errors.Is(err, ErrNotFound) {
|
||||||
|
t.Errorf("err = %v, want ErrNotFound", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupArtistByMBID_EmptyMBIDRejected(t *testing.T) {
|
||||||
|
c, srv := newTestClient(func(http.ResponseWriter, *http.Request) {
|
||||||
|
t.Error("server should not be called with empty mbid")
|
||||||
|
})
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
if _, err := c.LookupArtistByMBID(context.Background(), ""); err == nil {
|
||||||
|
t.Error("err = nil, want empty-mbid rejection")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +194,7 @@ func TestDeleteAlbum_5xxReturnsErrServerError(t *testing.T) {
|
|||||||
func TestDeleteAlbum_NetworkErrorReturnsErrUnreachable(t *testing.T) {
|
func TestDeleteAlbum_NetworkErrorReturnsErrUnreachable(t *testing.T) {
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||||
srv.Close() // server is closed; client should fail to connect
|
srv.Close() // server is closed; client should fail to connect
|
||||||
c := NewClient(srv.URL, "test-key")
|
c := NewClient(srv.URL, "key123")
|
||||||
|
|
||||||
err := c.DeleteAlbum(context.Background(), 42, true, true)
|
err := c.DeleteAlbum(context.Background(), 42, true, true)
|
||||||
if !errors.Is(err, ErrUnreachable) {
|
if !errors.Is(err, ErrUnreachable) {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func (c *Client) LookupArtistByMBID(ctx context.Context, mbid string) (LidarrArt
|
|||||||
|
|
||||||
var rows []LidarrArtist
|
var rows []LidarrArtist
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil {
|
||||||
return LidarrArtist{}, fmt.Errorf("lidarr: decode artist: %w", err)
|
return LidarrArtist{}, fmt.Errorf("%w: decode artist: %v", ErrInvalidPayload, err)
|
||||||
}
|
}
|
||||||
if len(rows) == 0 {
|
if len(rows) == 0 {
|
||||||
return LidarrArtist{}, ErrNotFound
|
return LidarrArtist{}, ErrNotFound
|
||||||
@@ -45,7 +45,7 @@ func (c *Client) LookupAlbumByMBID(ctx context.Context, mbid string) (LidarrAlbu
|
|||||||
|
|
||||||
var rows []LidarrAlbum
|
var rows []LidarrAlbum
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil {
|
||||||
return LidarrAlbum{}, fmt.Errorf("lidarr: decode album: %w", err)
|
return LidarrAlbum{}, fmt.Errorf("%w: decode album: %v", ErrInvalidPayload, err)
|
||||||
}
|
}
|
||||||
if len(rows) == 0 {
|
if len(rows) == 0 {
|
||||||
return LidarrAlbum{}, ErrNotFound
|
return LidarrAlbum{}, ErrNotFound
|
||||||
|
|||||||
Reference in New Issue
Block a user