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

View file

@ -21,7 +21,7 @@ var _ = Describe("Provider - ArtistImage", func() {
var mockAlbumRepo *mockAlbumRepo
var mockMediaFileRepo *mockMediaFileRepo
var mockImageAgent *mockArtistImageAgent
var agentsCombined *mockCombinedAgents
var agentsCombined *mockAgents
var ctx context.Context
var cancel context.CancelFunc
var originalAgentsConfig string
@ -43,8 +43,8 @@ var _ = Describe("Provider - ArtistImage", func() {
mockImageAgent = newMockArtistImageAgent()
// Use the mockCombinedAgents from helper, setting the specific agent
agentsCombined = &mockCombinedAgents{
// Use the mockAgents from helper, setting the specific agent
agentsCombined = &mockAgents{
imageAgent: mockImageAgent,
}
@ -281,7 +281,7 @@ type mockArtistImageAgent struct {
// Constructor for the mock agent
func newMockArtistImageAgent() *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()
return mock
}

View file

@ -184,102 +184,96 @@ func (m *mockSimilarArtistAgent) GetSimilarArtists(ctx context.Context, id, name
return nil, args.Error(1)
}
// mockCombinedAgents mocks the main Agents interface used by Provider
type mockCombinedAgents struct {
// mockAgents mocks the main Agents interface used by Provider
type mockAgents struct {
mock.Mock // Embed testify mock
topSongsAgent agents.ArtistTopSongsRetriever
similarAgent agents.ArtistSimilarRetriever
imageAgent agents.ArtistImageRetriever
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"
}
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 {
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 {
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 ---
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 {
return m.albumInfoAgent.GetAlbumInfo(ctx, name, artist, mbid)
}
if m.topSongsAgent != nil {
if ar, ok := m.topSongsAgent.(agents.AlbumInfoRetriever); ok {
return ar.GetAlbumInfo(ctx, name, artist, mbid)
}
// Fallback to testify mock
args := m.Called(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) {
if m.topSongsAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistMBIDRetriever); ok {
return ar.GetArtistMBID(ctx, id, name)
}
func (m *mockAgents) GetArtistMBID(ctx context.Context, id string, name string) (string, error) {
if m.mbidAgent != nil {
return m.mbidAgent.GetArtistMBID(ctx, id, name)
}
if m.similarAgent != nil {
if ar, ok := m.similarAgent.(agents.ArtistMBIDRetriever); ok {
return ar.GetArtistMBID(ctx, id, name)
}
}
return "", agents.ErrNotFound
// Fallback to testify mock
args := m.Called(ctx, id, name)
return args.String(0), args.Error(1)
}
func (m *mockCombinedAgents) GetArtistURL(ctx context.Context, id, name, mbid string) (string, error) {
if m.topSongsAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistURLRetriever); ok {
return ar.GetArtistURL(ctx, id, name, mbid)
}
func (m *mockAgents) GetArtistURL(ctx context.Context, id, name, mbid string) (string, error) {
if m.urlAgent != nil {
return m.urlAgent.GetArtistURL(ctx, id, name, mbid)
}
if m.similarAgent != nil {
if ar, ok := m.similarAgent.(agents.ArtistURLRetriever); ok {
return ar.GetArtistURL(ctx, id, name, mbid)
}
}
return "", agents.ErrNotFound
// Fallback to testify mock
args := m.Called(ctx, id, name, mbid)
return args.String(0), args.Error(1)
}
func (m *mockCombinedAgents) GetArtistBiography(ctx context.Context, id, name, mbid string) (string, error) {
if m.topSongsAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistBiographyRetriever); ok {
return ar.GetArtistBiography(ctx, id, name, mbid)
}
func (m *mockAgents) GetArtistBiography(ctx context.Context, id, name, mbid string) (string, error) {
if m.bioAgent != nil {
return m.bioAgent.GetArtistBiography(ctx, id, name, mbid)
}
if m.similarAgent != nil {
if ar, ok := m.similarAgent.(agents.ArtistBiographyRetriever); ok {
return ar.GetArtistBiography(ctx, id, name, mbid)
}
}
return "", agents.ErrNotFound
// Fallback to testify mock
args := m.Called(ctx, id, name, mbid)
return args.String(0), args.Error(1)
}
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 {
return m.imageAgent.GetArtistImages(ctx, id, name, mbid)
}
if m.topSongsAgent != nil {
if ar, ok := m.topSongsAgent.(agents.ArtistImageRetriever); ok {
return ar.GetArtistImages(ctx, id, name, mbid)
}
// Fallback to testify mock
args := m.Called(ctx, id, name, mbid)
if args.Get(0) != nil {
return args.Get(0).([]agents.ExternalImage), args.Error(1)
}
if m.similarAgent != nil {
if ar, ok := m.similarAgent.(agents.ArtistImageRetriever); ok {
return ar.GetArtistImages(ctx, id, name, mbid)
}
}
return nil, agents.ErrNotFound
return nil, args.Error(1)
}

View file

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

View file

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

View file

@ -21,38 +21,18 @@ func init() {
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 (
ctx context.Context
p extdata.Provider
ds *tests.MockDataStore
ag *MockAgents
ag *extdata.MockAgents
)
BeforeEach(func() {
ctx = context.Background()
ds = new(tests.MockDataStore)
ag = new(MockAgents)
ag = new(extdata.MockAgents)
p = extdata.NewProvider(ds, ag)
// Default config