Skip to content

Commit

Permalink
refactor(modules/albums): remove deprecated methods from repository
Browse files Browse the repository at this point in the history
… and implement overloads in `service`
  • Loading branch information
Mnigos committed Apr 9, 2024
1 parent db3d199 commit d50bb13
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 463 deletions.
282 changes: 20 additions & 262 deletions src/modules/albums/albums.repository.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
import { DataSource, In } from 'typeorm'
import { Test } from '@nestjs/testing'
import { MockInstance } from 'vitest'

import { AlbumsRepository, relations } from './albums.repository'

import { ArtistsRepository } from '@modules/artists'
import { ImagesRepository } from '@modules/images'
import {
albumEntityMock,
albumMock,
albumsEntitiesMock,
artistEntitiesMock,
artistEntityMock,
imagesMock,
sdkAlbumMock,
sdkArtistsMock,
trackEntitiesMock,
} from '@common/mocks'
import { TracksRepository } from '@modules/tracks'
import { SpotifyAlbumsService } from '@modules/spotify/albums'
import { SdkAlbum } from '@common/types/spotify'

type GetAlbumMockInstance = MockInstance<
[id: string, adapt: false],
Promise<SdkAlbum>
>
type GetAlbumsMockInstance = MockInstance<
[ids: string[], adapt: false],
Promise<SdkAlbum[]>
>

describe('AlbumsRepository', () => {
const externalId = 'externalId'

let albumsRepository: AlbumsRepository
let imagesRepository: ImagesRepository
let artistsRepository: ArtistsRepository
let tracksRepository: TracksRepository
let spotifyAlbumsService: SpotifyAlbumsService

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand All @@ -49,39 +27,10 @@ describe('AlbumsRepository', () => {
createEntityManager: vi.fn(),
},
},
{
provide: ImagesRepository,
useValue: {
findOrCreateImages: vi.fn(),
},
},
{
provide: ArtistsRepository,
useValue: {
findOrCreateArtists: vi.fn(),
},
},
{
provide: TracksRepository,
useValue: {
createTracksFromExternalIds: vi.fn(),
},
},
{
provide: SpotifyAlbumsService,
useValue: {
getAlbum: vi.fn(),
getAlbums: vi.fn(),
},
},
],
}).compile()

albumsRepository = module.get(AlbumsRepository)
imagesRepository = module.get(ImagesRepository)
artistsRepository = module.get(ArtistsRepository)
tracksRepository = module.get(TracksRepository)
spotifyAlbumsService = module.get(SpotifyAlbumsService)
})

test('should be defined', () => {
Expand All @@ -98,7 +47,7 @@ describe('AlbumsRepository', () => {
})

test('should find albums by external ids', async () => {
const externalIds = ['externalId']
const externalIds = [externalId]

const findSpy = vi
.spyOn(albumsRepository, 'find')
Expand Down Expand Up @@ -157,216 +106,25 @@ describe('AlbumsRepository', () => {
})
})

describe('createAlbum', () => {
test('should create album without artists', async () => {
const findOrCreateImagesSpy = vi
.spyOn(imagesRepository, 'findOrCreateImages')
.mockResolvedValue(imagesMock)
const findOrCreateArtistsSpy = vi
.spyOn(artistsRepository, 'findOrCreateArtists')
.mockResolvedValue(artistEntitiesMock)
const createTracksFromExternalIdsSpy = vi
.spyOn(tracksRepository, 'createTracksFromExternalIds')
.mockResolvedValue(trackEntitiesMock)
const createSpy = vi
.spyOn(albumsRepository, 'create')
.mockReturnValue(albumEntityMock)
const saveSpy = vi
.spyOn(albumsRepository, 'save')
.mockResolvedValue(albumEntityMock)

expect(await albumsRepository.createAlbum(sdkAlbumMock)).toEqual(
albumEntityMock
)
expect(findOrCreateImagesSpy).toHaveBeenCalledWith(albumMock.images)
expect(findOrCreateArtistsSpy).toHaveBeenCalledWith(sdkArtistsMock)
expect(createTracksFromExternalIdsSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
albumEntityMock
)
expect(createSpy).toHaveBeenCalled()
expect(saveSpy).toHaveBeenCalledWith(albumEntityMock)
})

test('should create album with artists', async () => {
const findOrCreateImagesSpy = vi
.spyOn(imagesRepository, 'findOrCreateImages')
.mockResolvedValue(imagesMock)
const findOrCreateArtistsSpy = vi
.spyOn(artistsRepository, 'findOrCreateArtists')
.mockResolvedValue(artistEntitiesMock)
const createTracksFromExternalIdsSpy = vi
.spyOn(tracksRepository, 'createTracksFromExternalIds')
.mockResolvedValue(trackEntitiesMock)
const createSpy = vi
.spyOn(albumsRepository, 'create')
.mockReturnValue(albumEntityMock)
const saveSpy = vi
.spyOn(albumsRepository, 'save')
.mockResolvedValue(albumEntityMock)

expect(
await albumsRepository.createAlbum(sdkAlbumMock, artistEntitiesMock)
).toEqual(albumEntityMock)
expect(findOrCreateImagesSpy).toHaveBeenCalledWith(albumMock.images)
expect(findOrCreateArtistsSpy).not.toHaveBeenCalled()
expect(createTracksFromExternalIdsSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
albumEntityMock
)
expect(createSpy).toHaveBeenCalled()
expect(saveSpy).toHaveBeenCalledWith(albumEntityMock)
})
})

describe('findOrCreateAlbumFromExternalId', () => {
test('should create tracks and return found album with tracks', async () => {
const foundAlbumMock = {
...albumEntityMock,
tracks: trackEntitiesMock,
}

const findAlbumByExternalIdSpy = vi
.spyOn(albumsRepository, 'findAlbumByExternalId')
.mockResolvedValue(foundAlbumMock)
const getAlbumSpy = (
vi.spyOn(
spotifyAlbumsService,
'getAlbum'
) as unknown as GetAlbumMockInstance
).mockResolvedValue(sdkAlbumMock)
const createTracksFromExternalIdsSpy = vi
.spyOn(tracksRepository, 'createTracksFromExternalIds')
.mockResolvedValue(trackEntitiesMock)
const createAlbumSpy = vi
.spyOn(albumsRepository, 'createAlbum')
.mockResolvedValue(albumEntityMock)

expect(
await albumsRepository.findOrCreateAlbumFromExternalId(externalId)
).toEqual(foundAlbumMock)
expect(findAlbumByExternalIdSpy).toHaveBeenCalledWith(externalId)
expect(getAlbumSpy).toHaveBeenCalledWith(externalId, false)
expect(createTracksFromExternalIdsSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
foundAlbumMock
)
expect(createAlbumSpy).not.toHaveBeenCalled()
})

test('should create album from external id', async () => {
const findAlbumByExternalIdSpy = vi
.spyOn(albumsRepository, 'findAlbumByExternalId')
.mockResolvedValue(null)
const getAlbumSpy = (
vi.spyOn(
spotifyAlbumsService,
'getAlbum'
) as unknown as GetAlbumMockInstance
).mockResolvedValue(sdkAlbumMock)
const creatAlbumSpy = vi
.spyOn(albumsRepository, 'createAlbum')
.mockResolvedValue(albumEntityMock)

expect(
await albumsRepository.findOrCreateAlbumFromExternalId(externalId)
).toEqual(albumEntityMock)
expect(findAlbumByExternalIdSpy).toHaveBeenCalledWith(externalId)
expect(getAlbumSpy).toHaveBeenCalledWith(externalId, false)
expect(creatAlbumSpy).toHaveBeenCalledWith(sdkAlbumMock)
})
})

describe('findOrCreateAlbumsFromExternalIds', () => {
test('should create albums from external ids', async () => {
const externalIds = [externalId]

const getAlbumsSpy = (
vi.spyOn(
spotifyAlbumsService,
'getAlbums'
) as unknown as GetAlbumsMockInstance
).mockResolvedValue([sdkAlbumMock])
const createAlbumSpy = vi
.spyOn(albumsRepository, 'createAlbum')
.mockResolvedValue(albumEntityMock)
const findAlbumsByExternalIdsSpy = vi
.spyOn(albumsRepository, 'findAlbumsByExternalIds')
.mockResolvedValue([])

expect(
await albumsRepository.findOrCreateAlbumsFromExternalIds(externalIds)
).toEqual([albumEntityMock])
expect(getAlbumsSpy).toHaveBeenCalledWith(externalIds, false)
expect(createAlbumSpy).toHaveBeenCalledWith(sdkAlbumMock, undefined)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledWith(externalIds)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledTimes(
externalIds.length
)
})

test('should create tracks and return found album with tracks', async () => {
const externalIds = [sdkAlbumMock.id]
const foundAlbumMock = {
...albumEntityMock,
tracks: trackEntitiesMock,
}

const getAlbumsSpy = (
vi.spyOn(
spotifyAlbumsService,
'getAlbums'
) as unknown as GetAlbumsMockInstance
).mockResolvedValue([sdkAlbumMock])
const findAlbumsByExternalIdsSpy = vi
.spyOn(albumsRepository, 'findAlbumsByExternalIds')
.mockResolvedValue([foundAlbumMock])
const createTracksFromExternalIdsSpy = vi
.spyOn(tracksRepository, 'createTracksFromExternalIds')
.mockResolvedValue(trackEntitiesMock)
const createAlbumSpy = vi.spyOn(albumsRepository, 'createAlbum')

expect(
await albumsRepository.findOrCreateAlbumsFromExternalIds(externalIds)
).toEqual([foundAlbumMock])
expect(getAlbumsSpy).toHaveBeenCalledWith(externalIds, false)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledWith(externalIds)
expect(createTracksFromExternalIdsSpy).toHaveBeenCalledWith(
trackEntitiesMock.map(track => track.id),
foundAlbumMock
)
expect(createAlbumSpy).not.toHaveBeenCalled()
})

test('should create albums from external ids with artists', async () => {
const externalIds = [externalId]
const artists = [artistEntityMock]

const getAlbumsSpy = (
vi.spyOn(
spotifyAlbumsService,
'getAlbums'
) as unknown as GetAlbumsMockInstance
).mockResolvedValue([sdkAlbumMock])
const createAlbumSpy = vi
.spyOn(albumsRepository, 'createAlbum')
.mockResolvedValue(albumEntityMock)
const findAlbumsByExternalIdsSpy = vi
.spyOn(albumsRepository, 'findAlbumsByExternalIds')
.mockResolvedValue([])
test('should create album', async () => {
const createSpy = vi
.spyOn(albumsRepository, 'create')
.mockReturnValue(albumEntityMock)
const saveSpy = vi
.spyOn(albumsRepository, 'save')
.mockResolvedValue(albumEntityMock)

expect(
await albumsRepository.findOrCreateAlbumsFromExternalIds(
externalIds,
artists
)
).toEqual([albumEntityMock])
expect(getAlbumsSpy).toHaveBeenCalledWith(externalIds, false)
expect(createAlbumSpy).toHaveBeenCalledWith(sdkAlbumMock, artists)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledWith(externalIds)
expect(findAlbumsByExternalIdsSpy).toHaveBeenCalledTimes(
externalIds.length
)
})
expect(
await albumsRepository.createAlbum({
...albumMock,
artists: artistEntitiesMock,
releaseDate: new Date(sdkAlbumMock.release_date),
externalId,
albumType: sdkAlbumMock.album_type,
images: imagesMock,
})
).toEqual(albumEntityMock)
expect(createSpy).toHaveBeenCalled()
expect(saveSpy).toHaveBeenCalledWith(albumEntityMock)
})
})
Loading

0 comments on commit d50bb13

Please sign in to comment.