Fixing tests
Some checks failed
Music Collection CI Workflow / test (./backend) (push) Successful in 29s
Music Collection CI Workflow / test (./frontend) (push) Successful in 36s
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-collection-backend, ./backend) (push) Successful in 51s
Music Collection CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/music-collection-frontend, ./frontend) (push) Failing after 1m29s
Music Collection CI Workflow / deploy (push) Has been skipped

This commit is contained in:
Phill Pover 2025-04-07 04:05:42 +01:00
parent e687852cfa
commit 4f389f70af
5 changed files with 24 additions and 25 deletions

View File

@ -59,15 +59,15 @@ describe('AlbumController', () => {
})
describe('create', () => {
it('should return the Album ID', async () => {
expect(await controller.create(mockCreateAlbumDto)).toStrictEqual(mockAlbum);
it('should return the Album', async () => {
expect(await controller.create(mockCreateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' });
})
})
describe('update', () => {
it('should return the success message', async () => {
it('should return the Album', async () => {
mockUpdateAlbumDto.id = 1;
expect(await controller.update(1, mockUpdateAlbumDto)).toStrictEqual(mockAlbum);
expect(await controller.update(1, mockUpdateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' });
})
})

View File

@ -27,7 +27,7 @@ describe('AlbumService', () => {
findOneBy: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }),
create: jest.fn().mockResolvedValue(mockAlbum),
save: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }),
update: jest.fn().mockResolvedValue(mockAlbum),
update: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }),
delete: jest.fn().mockResolvedValue(mockDeleteResult),
},
},
@ -55,15 +55,15 @@ describe('AlbumService', () => {
})
describe('create', () => {
it('should return the Album ID', async () => {
expect(await service.create(mockCreateAlbumDto)).toStrictEqual(mockAlbum);
it('should return the Album', async () => {
expect(await service.create(mockCreateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' });
})
})
describe('update', () => {
it('should return the success message', async () => {
it('should return the Album', async () => {
mockUpdateAlbumDto.id = 1;
expect(await service.update(1, mockUpdateAlbumDto)).toStrictEqual(mockAlbum);
expect(await service.update(1, mockUpdateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' });
})
})

View File

@ -44,15 +44,14 @@ export class AlbumService {
async update(id: number, updateAlbumDto: UpdateAlbumDto): Promise<Album | null> {
if (id === updateAlbumDto.id) {
const album = this.albumRepository.findOneBy({ id: updateAlbumDto.id });
if (!album)
const albumToUpdate = await this.albumRepository.findOneBy({ id: updateAlbumDto.id });
if (!albumToUpdate)
return null;
const savedAlbum = await this.albumRepository.update({ id: updateAlbumDto.id }, {
title: updateAlbumDto.title,
artist: updateAlbumDto.artist,
genre: updateAlbumDto.genre
});
albumToUpdate.title = updateAlbumDto.title;
albumToUpdate.artist = updateAlbumDto.artist;
albumToUpdate.genre = updateAlbumDto.genre;
const savedAlbum = await this.albumRepository.save(albumToUpdate);
return savedAlbum;
} else {
return null;

View File

@ -22,13 +22,13 @@ export class SongController {
@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createSongDto: CreateSongDto): Promise<number> {
async create(@Body() createSongDto: CreateSongDto): Promise<Song | null> {
return this.songService.create(createSongDto);
}
@Put(':id')
@UsePipes(new ValidationPipe({ transform: true }))
async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise<string> {
async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise<Song | null> {
return this.songService.update(id, updateSongDto);
}

View File

@ -27,7 +27,7 @@ export class SongService {
return this.songRepository.findOneBy({ id: id });
}
async create(createSongDto: CreateSongDto): Promise<number> {
async create(createSongDto: CreateSongDto): Promise<Song | null> {
const album = await this.albumRepository.findOneBy({id: createSongDto.albumId});
if (album) {
const song = new Song();
@ -36,28 +36,28 @@ export class SongService {
song.trackNumber = createSongDto.trackNumber;
song.album = album;
const savedSong = await this.songRepository.save(song);
return savedSong.id;
return savedSong;
} else {
throw new Error(`Unable to find Album with ID ${createSongDto.albumId}`);
}
}
async update(id: number, updateSongDto: UpdateSongDto): Promise<string> {
async update(id: number, updateSongDto: UpdateSongDto): Promise<Song | null> {
if (id === updateSongDto.id) {
const album = await this.albumRepository.findOneBy({id: updateSongDto.albumId});
const songToUpdate = await this.songRepository.findOneBy({ id: updateSongDto.id });
if (!songToUpdate || !album)
return "Song not found";
return null;
songToUpdate.id = updateSongDto.id;
songToUpdate.title = updateSongDto.title;
songToUpdate.duration = updateSongDto.duration;
songToUpdate.trackNumber = updateSongDto.trackNumber;
songToUpdate.album = album;
await this.songRepository.update({ id: updateSongDto.id }, songToUpdate);
return "Song updated successfully";
const song = await this.songRepository.save(songToUpdate);
return song;
} else {
return "Song ID does not match posted data"
return null;
}
}