UpdateAlbumInfo tests - wip

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-03-30 15:20:50 -04:00
parent 68c24bdb5b
commit e03cc1abeb
7 changed files with 66 additions and 88 deletions

View file

@ -0,0 +1,4 @@
package extdata
// Expose internal types for testing
type MockAgents = mockAgents

View file

@ -21,7 +21,7 @@ var _ = Describe("Provider - AlbumImage", func() {
var mockAlbumRepo *mockAlbumRepo var mockAlbumRepo *mockAlbumRepo
var mockMediaFileRepo *mockMediaFileRepo var mockMediaFileRepo *mockMediaFileRepo
var mockAlbumAgent *mockAlbumInfoAgent var mockAlbumAgent *mockAlbumInfoAgent
var agentsCombined *mockCombinedAgents var agentsCombined *mockAgents
var ctx context.Context var ctx context.Context
var cancel context.CancelFunc var cancel context.CancelFunc
var originalAgentsConfig string var originalAgentsConfig string
@ -43,7 +43,7 @@ var _ = Describe("Provider - AlbumImage", func() {
mockAlbumAgent = newMockAlbumInfoAgent() mockAlbumAgent = newMockAlbumInfoAgent()
agentsCombined = &mockCombinedAgents{ agentsCombined = &mockAgents{
albumInfoAgent: mockAlbumAgent, albumInfoAgent: mockAlbumAgent,
} }

View file

@ -21,7 +21,7 @@ var _ = Describe("Provider - ArtistImage", func() {
var mockAlbumRepo *mockAlbumRepo var mockAlbumRepo *mockAlbumRepo
var mockMediaFileRepo *mockMediaFileRepo var mockMediaFileRepo *mockMediaFileRepo
var mockImageAgent *mockArtistImageAgent var mockImageAgent *mockArtistImageAgent
var agentsCombined *mockCombinedAgents var agentsCombined *mockAgents
var ctx context.Context var ctx context.Context
var cancel context.CancelFunc var cancel context.CancelFunc
var originalAgentsConfig string var originalAgentsConfig string
@ -43,8 +43,8 @@ var _ = Describe("Provider - ArtistImage", func() {
mockImageAgent = newMockArtistImageAgent() mockImageAgent = newMockArtistImageAgent()
// Use the mockCombinedAgents from helper, setting the specific agent // Use the mockAgents from helper, setting the specific agent
agentsCombined = &mockCombinedAgents{ agentsCombined = &mockAgents{
imageAgent: mockImageAgent, imageAgent: mockImageAgent,
} }
@ -281,7 +281,7 @@ type mockArtistImageAgent struct {
// Constructor for the mock agent // Constructor for the mock agent
func newMockArtistImageAgent() *mockArtistImageAgent { func newMockArtistImageAgent() *mockArtistImageAgent {
mock := new(mockArtistImageAgent) mock := new(mockArtistImageAgent)
// Set default AgentName if needed, although usually called via mockCombinedAgents // Set default AgentName if needed, although usually called via mockAgents
mock.On("AgentName").Return("mockImage").Maybe() mock.On("AgentName").Return("mockImage").Maybe()
return mock return mock
} }

View file

@ -184,102 +184,96 @@ func (m *mockSimilarArtistAgent) GetSimilarArtists(ctx context.Context, id, name
return nil, args.Error(1) return nil, args.Error(1)
} }
// mockCombinedAgents mocks the main Agents interface used by Provider // mockAgents mocks the main Agents interface used by Provider
type mockCombinedAgents struct { type mockAgents struct {
mock.Mock // Embed testify mock
topSongsAgent agents.ArtistTopSongsRetriever topSongsAgent agents.ArtistTopSongsRetriever
similarAgent agents.ArtistSimilarRetriever similarAgent agents.ArtistSimilarRetriever
imageAgent agents.ArtistImageRetriever imageAgent agents.ArtistImageRetriever
albumInfoAgent agents.AlbumInfoRetriever albumInfoAgent agents.AlbumInfoRetriever
agents.Interface // Embed to satisfy non-overridden methods bioAgent agents.ArtistBiographyRetriever // Added field for clarity
mbidAgent agents.ArtistMBIDRetriever // Added field for clarity
urlAgent agents.ArtistURLRetriever // Added field for clarity
agents.Interface // Embed to satisfy non-overridden methods
} }
func (m *mockCombinedAgents) AgentName() string { func (m *mockAgents) AgentName() string {
return "mockCombined" return "mockCombined"
} }
func (m *mockCombinedAgents) GetSimilarArtists(ctx context.Context, id, name, mbid string, limit int) ([]agents.Artist, error) { func (m *mockAgents) GetSimilarArtists(ctx context.Context, id, name, mbid string, limit int) ([]agents.Artist, error) {
if m.similarAgent != nil { if m.similarAgent != nil {
return m.similarAgent.GetSimilarArtists(ctx, id, name, mbid, limit) return m.similarAgent.GetSimilarArtists(ctx, id, name, mbid, limit)
} }
return nil, agents.ErrNotFound // Fallback to testify mock
args := m.Called(ctx, id, name, mbid, limit)
if args.Get(0) != nil {
return args.Get(0).([]agents.Artist), args.Error(1)
}
return nil, args.Error(1)
} }
func (m *mockCombinedAgents) GetArtistTopSongs(ctx context.Context, id, artistName, mbid string, count int) ([]agents.Song, error) { func (m *mockAgents) GetArtistTopSongs(ctx context.Context, id, artistName, mbid string, count int) ([]agents.Song, error) {
if m.topSongsAgent != nil { if m.topSongsAgent != nil {
return m.topSongsAgent.GetArtistTopSongs(ctx, id, artistName, mbid, count) return m.topSongsAgent.GetArtistTopSongs(ctx, id, artistName, mbid, count)
} }
return nil, agents.ErrNotFound // Fallback to testify mock
args := m.Called(ctx, id, artistName, mbid, count)
if args.Get(0) != nil {
return args.Get(0).([]agents.Song), args.Error(1)
}
return nil, args.Error(1)
} }
// --- Stubs for other Agents interface methods --- // --- Stubs for other Agents interface methods ---
func (m *mockCombinedAgents) GetAlbumInfo(ctx context.Context, name, artist, mbid string) (*agents.AlbumInfo, error) { func (m *mockAgents) GetAlbumInfo(ctx context.Context, name, artist, mbid string) (*agents.AlbumInfo, error) {
if m.albumInfoAgent != nil { if m.albumInfoAgent != nil {
return m.albumInfoAgent.GetAlbumInfo(ctx, name, artist, mbid) return m.albumInfoAgent.GetAlbumInfo(ctx, name, artist, mbid)
} }
if m.topSongsAgent != nil { // Fallback to testify mock
if ar, ok := m.topSongsAgent.(agents.AlbumInfoRetriever); ok { args := m.Called(ctx, name, artist, mbid)
return ar.GetAlbumInfo(ctx, name, artist, mbid) if args.Get(0) != nil {
} return args.Get(0).(*agents.AlbumInfo), args.Error(1)
} }
return nil, agents.ErrNotFound return nil, args.Error(1)
} }
func (m *mockCombinedAgents) GetArtistMBID(ctx context.Context, id string, name string) (string, error) { func (m *mockAgents) GetArtistMBID(ctx context.Context, id string, name string) (string, error) {
if m.topSongsAgent != nil { if m.mbidAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistMBIDRetriever); ok { return m.mbidAgent.GetArtistMBID(ctx, id, name)
return ar.GetArtistMBID(ctx, id, name)
}
} }
if m.similarAgent != nil { // Fallback to testify mock
if ar, ok := m.similarAgent.(agents.ArtistMBIDRetriever); ok { args := m.Called(ctx, id, name)
return ar.GetArtistMBID(ctx, id, name) return args.String(0), args.Error(1)
}
}
return "", agents.ErrNotFound
} }
func (m *mockCombinedAgents) GetArtistURL(ctx context.Context, id, name, mbid string) (string, error) { func (m *mockAgents) GetArtistURL(ctx context.Context, id, name, mbid string) (string, error) {
if m.topSongsAgent != nil { if m.urlAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistURLRetriever); ok { return m.urlAgent.GetArtistURL(ctx, id, name, mbid)
return ar.GetArtistURL(ctx, id, name, mbid)
}
} }
if m.similarAgent != nil { // Fallback to testify mock
if ar, ok := m.similarAgent.(agents.ArtistURLRetriever); ok { args := m.Called(ctx, id, name, mbid)
return ar.GetArtistURL(ctx, id, name, mbid) return args.String(0), args.Error(1)
}
}
return "", agents.ErrNotFound
} }
func (m *mockCombinedAgents) GetArtistBiography(ctx context.Context, id, name, mbid string) (string, error) { func (m *mockAgents) GetArtistBiography(ctx context.Context, id, name, mbid string) (string, error) {
if m.topSongsAgent != nil { if m.bioAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistBiographyRetriever); ok { return m.bioAgent.GetArtistBiography(ctx, id, name, mbid)
return ar.GetArtistBiography(ctx, id, name, mbid)
}
} }
if m.similarAgent != nil { // Fallback to testify mock
if ar, ok := m.similarAgent.(agents.ArtistBiographyRetriever); ok { args := m.Called(ctx, id, name, mbid)
return ar.GetArtistBiography(ctx, id, name, mbid) return args.String(0), args.Error(1)
}
}
return "", agents.ErrNotFound
} }
func (m *mockCombinedAgents) GetArtistImages(ctx context.Context, id, name, mbid string) ([]agents.ExternalImage, error) { func (m *mockAgents) GetArtistImages(ctx context.Context, id, name, mbid string) ([]agents.ExternalImage, error) {
if m.imageAgent != nil { if m.imageAgent != nil {
return m.imageAgent.GetArtistImages(ctx, id, name, mbid) return m.imageAgent.GetArtistImages(ctx, id, name, mbid)
} }
if m.topSongsAgent != nil { // Fallback to testify mock
if ar, ok := m.topSongsAgent.(agents.ArtistImageRetriever); ok { args := m.Called(ctx, id, name, mbid)
return ar.GetArtistImages(ctx, id, name, mbid) if args.Get(0) != nil {
} return args.Get(0).([]agents.ExternalImage), args.Error(1)
} }
if m.similarAgent != nil { return nil, args.Error(1)
if ar, ok := m.similarAgent.(agents.ArtistImageRetriever); ok {
return ar.GetArtistImages(ctx, id, name, mbid)
}
}
return nil, agents.ErrNotFound
} }

View file

@ -41,7 +41,7 @@ var _ = Describe("Provider - SimilarSongs", func() {
mockTopAgent = mockAgent mockTopAgent = mockAgent
mockSimilarAgent = mockAgent mockSimilarAgent = mockAgent
agentsCombined = &mockCombinedAgents{ agentsCombined = &mockAgents{
topSongsAgent: mockTopAgent, topSongsAgent: mockTopAgent,
similarAgent: mockSimilarAgent, similarAgent: mockSimilarAgent,
} }

View file

@ -41,7 +41,7 @@ var _ = Describe("Provider - TopSongs", func() {
mockTopSongsAgent = &mockArtistTopSongsAgent{} mockTopSongsAgent = &mockArtistTopSongsAgent{}
agentsCombined = &mockCombinedAgents{ agentsCombined = &mockAgents{
topSongsAgent: mockTopSongsAgent, topSongsAgent: mockTopSongsAgent,
similarAgent: nil, similarAgent: nil,
} }

View file

@ -21,38 +21,18 @@ func init() {
log.SetLevel(log.LevelDebug) log.SetLevel(log.LevelDebug)
} }
// MockAgents implements the extdata.Agents interface for testing
type MockAgents struct {
mock.Mock
agents.AlbumInfoRetriever
agents.ArtistBiographyRetriever
agents.ArtistMBIDRetriever
agents.ArtistImageRetriever
agents.ArtistSimilarRetriever
agents.ArtistTopSongsRetriever
agents.ArtistURLRetriever
}
func (m *MockAgents) GetAlbumInfo(ctx context.Context, albumName, artistName, mbid string) (*agents.AlbumInfo, error) {
args := m.Called(ctx, albumName, artistName, mbid)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*agents.AlbumInfo), args.Error(1)
}
var _ = Describe("Provider UpdateAlbumInfo", func() { var _ = Describe("Provider UpdateAlbumInfo", func() {
var ( var (
ctx context.Context ctx context.Context
p extdata.Provider p extdata.Provider
ds *tests.MockDataStore ds *tests.MockDataStore
ag *MockAgents ag *extdata.MockAgents
) )
BeforeEach(func() { BeforeEach(func() {
ctx = context.Background() ctx = context.Background()
ds = new(tests.MockDataStore) ds = new(tests.MockDataStore)
ag = new(MockAgents) ag = new(extdata.MockAgents)
p = extdata.NewProvider(ds, ag) p = extdata.NewProvider(ds, ag)
// Default config // Default config