From 49b339542468d4ab7da6a02b6ca7f05f0cb19a75 Mon Sep 17 00:00:00 2001 From: Moritz Weber Date: Sat, 28 Mar 2020 14:35:41 +0100 Subject: [PATCH] preparation for song entity --- analysis_options.yaml | 2 +- lib/domain/entities/song.dart | 13 +++-- .../repositories/music_data_repository.dart | 6 +-- lib/domain/usecases/get_albums.dart | 6 +-- lib/domain/usecases/get_songs.dart | 17 ++++++ lib/system/models/song_model.dart | 23 ++++++++ .../music_data_repository_impl.dart | 7 +++ test/domain/usecases/get_albums_test.dart | 4 +- test/domain/usecases/get_songs_test.dart | 54 +++++++++++++++++++ .../datasources/local_music_fetcher_test.dart | 2 +- .../moor_music_data_source_test.dart | 2 +- test/system/models/album_model_test.dart | 22 ++++---- test/system/models/song_model_test.dart | 24 +++++++++ .../music_data_repository_impl_test.dart | 30 ++++++----- test/test_constants.dart | 24 +++++++-- 15 files changed, 194 insertions(+), 42 deletions(-) create mode 100644 lib/domain/usecases/get_songs.dart create mode 100644 lib/system/models/song_model.dart create mode 100644 test/domain/usecases/get_songs_test.dart create mode 100644 test/system/models/song_model_test.dart diff --git a/analysis_options.yaml b/analysis_options.yaml index 196fbf4..3aad48f 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -49,7 +49,7 @@ linter: - always_put_control_body_on_new_line # - always_put_required_named_parameters_first # we prefer having parameters in the same order as fields https://github.com/flutter/flutter/issues/10219 - always_require_non_null_named_parameters - - always_specify_types + ##### - always_specify_types - annotate_overrides # - avoid_annotating_with_dynamic # conflicts with always_specify_types # - avoid_as # required for implicit-casts: true diff --git a/lib/domain/entities/song.dart b/lib/domain/entities/song.dart index 9ed3504..153bda7 100644 --- a/lib/domain/entities/song.dart +++ b/lib/domain/entities/song.dart @@ -1,6 +1,16 @@ import 'package:equatable/equatable.dart'; +import 'package:meta/meta.dart'; class Song extends Equatable { + Song({ + @required this.title, + @required this.album, + @required this.artist, + @required this.path, + this.trackNumber, + this.albumArtPath, + }); + final String title; final String album; final String artist; @@ -8,9 +18,6 @@ class Song extends Equatable { final String path; final String albumArtPath; - Song(this.title, this.album, this.artist, this.trackNumber, this.path, - this.albumArtPath); - @override List get props => [title, album, artist]; } diff --git a/lib/domain/repositories/music_data_repository.dart b/lib/domain/repositories/music_data_repository.dart index ec5a477..83656fe 100644 --- a/lib/domain/repositories/music_data_repository.dart +++ b/lib/domain/repositories/music_data_repository.dart @@ -1,11 +1,11 @@ import 'package:dartz/dartz.dart'; -import 'package:mosh/core/error/failures.dart'; +import '../../core/error/failures.dart'; import '../entities/album.dart'; -// import '../entities/artist.dart'; +import '../entities/song.dart'; abstract class MusicDataRepository { + Future>> getSongs(); Future>> getAlbums(); - // Future> getAlbumsByArtist(Artist artist); Future updateDatabase(); } \ No newline at end of file diff --git a/lib/domain/usecases/get_albums.dart b/lib/domain/usecases/get_albums.dart index ddfe504..5d011e6 100644 --- a/lib/domain/usecases/get_albums.dart +++ b/lib/domain/usecases/get_albums.dart @@ -5,11 +5,11 @@ import '../../core/usecase.dart'; import '../entities/album.dart'; import '../repositories/music_data_repository.dart'; -class GetAlbums implements UseCase, Null> { - final MusicDataRepository musicDataRepository; - +class GetAlbums implements UseCase, void> { GetAlbums(this.musicDataRepository); + final MusicDataRepository musicDataRepository; + @override Future>> call([_]) async { return await musicDataRepository.getAlbums(); diff --git a/lib/domain/usecases/get_songs.dart b/lib/domain/usecases/get_songs.dart new file mode 100644 index 0000000..909c8a0 --- /dev/null +++ b/lib/domain/usecases/get_songs.dart @@ -0,0 +1,17 @@ +import 'package:dartz/dartz.dart'; + +import '../../core/error/failures.dart'; +import '../../core/usecase.dart'; +import '../entities/song.dart'; +import '../repositories/music_data_repository.dart'; + +class GetSongs implements UseCase, void> { + GetSongs(this.musicDataRepository); + + final MusicDataRepository musicDataRepository; + + @override + Future>> call([_]) async { + return await musicDataRepository.getSongs(); + } +} diff --git a/lib/system/models/song_model.dart b/lib/system/models/song_model.dart new file mode 100644 index 0000000..cd53a58 --- /dev/null +++ b/lib/system/models/song_model.dart @@ -0,0 +1,23 @@ +import 'package:mosh/domain/entities/song.dart'; +import 'package:meta/meta.dart'; + +class SongModel extends Song { + SongModel({ + this.id, + @required String title, + @required String album, + @required String artist, + @required String path, + int trackNumber, + String albumArtPath, + }) : super( + title: title, + album: album, + artist: artist, + path: path, + trackNumber: trackNumber, + albumArtPath: albumArtPath, + ); + + final int id; +} diff --git a/lib/system/repositories/music_data_repository_impl.dart b/lib/system/repositories/music_data_repository_impl.dart index df9a478..0673ef2 100644 --- a/lib/system/repositories/music_data_repository_impl.dart +++ b/lib/system/repositories/music_data_repository_impl.dart @@ -1,5 +1,6 @@ import 'package:dartz/dartz.dart'; import 'package:meta/meta.dart'; +import 'package:mosh/domain/entities/song.dart'; import '../../core/error/failures.dart'; import '../../domain/entities/album.dart'; @@ -35,4 +36,10 @@ class MusicDataRepositoryImpl implements MusicDataRepository { } } } + + @override + Future>> getSongs() { + // TODO: implement getSongs + return null; + } } diff --git a/test/domain/usecases/get_albums_test.dart b/test/domain/usecases/get_albums_test.dart index fa7a7f0..937fd07 100644 --- a/test/domain/usecases/get_albums_test.dart +++ b/test/domain/usecases/get_albums_test.dart @@ -17,8 +17,8 @@ void main() { }); final tAlbums = [ - Album(title: "Back in Black", artist: "AC/DC"), - Album(title: "Twilight Of The Thunder God", artist: "Amon Amarth"), + Album(title: 'Back in Black', artist: 'AC/DC'), + Album(title: 'Twilight Of The Thunder God', artist: 'Amon Amarth'), ]; test( diff --git a/test/domain/usecases/get_songs_test.dart b/test/domain/usecases/get_songs_test.dart new file mode 100644 index 0000000..7b7e462 --- /dev/null +++ b/test/domain/usecases/get_songs_test.dart @@ -0,0 +1,54 @@ +import 'package:dartz/dartz.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:mosh/domain/entities/song.dart'; +import 'package:mosh/domain/repositories/music_data_repository.dart'; +import 'package:mosh/domain/usecases/get_songs.dart'; + +import '../../test_constants.dart'; + +class MockMusicDataRepository extends Mock implements MusicDataRepository {} + +void main() { + GetSongs usecase; + MockMusicDataRepository mockMusicDataRepository; + + setUp(() { + mockMusicDataRepository = MockMusicDataRepository(); + usecase = GetSongs(mockMusicDataRepository); + }); + + final tSongs = [ + Song( + album: ALBUM_TITLE_3, + artist: ARTIST_3, + title: SONG_TITLE_3, + path: PATH_3, + albumArtPath: ALBUM_ART_PATH_3, + trackNumber: TRACKNUMBER_3, + ), + Song( + album: ALBUM_TITLE_4, + artist: ARTIST_4, + title: SONG_TITLE_4, + path: PATH_4, + albumArtPath: ALBUM_ART_PATH_4, + trackNumber: TRACKNUMBER_4, + ), + ]; + + test( + 'should get all songs from the repository', + () async { + // arrange + when(mockMusicDataRepository.getSongs()) + .thenAnswer((_) async => Right(tSongs)); + // act + final result = await usecase(); + // assert + expect(result, Right(tSongs)); + verify(mockMusicDataRepository.getSongs()); + verifyNoMoreInteractions(mockMusicDataRepository); + }, + ); +} diff --git a/test/system/datasources/local_music_fetcher_test.dart b/test/system/datasources/local_music_fetcher_test.dart index 1937137..614f4a8 100644 --- a/test/system/datasources/local_music_fetcher_test.dart +++ b/test/system/datasources/local_music_fetcher_test.dart @@ -23,7 +23,7 @@ void main() { setUp(() { mockAlbumInfo = MockAlbumInfo(); - when(mockAlbumInfo.title).thenReturn(TITLE_1); + when(mockAlbumInfo.title).thenReturn(ALBUM_TITLE_1); when(mockAlbumInfo.albumArt).thenReturn(ALBUM_ART_PATH_1); when(mockAlbumInfo.artist).thenReturn(ARTIST_1); when(mockAlbumInfo.firstYear).thenReturn(FIRST_YEAR_1.toString()); diff --git a/test/system/datasources/moor_music_data_source_test.dart b/test/system/datasources/moor_music_data_source_test.dart index e523e86..d5f561d 100644 --- a/test/system/datasources/moor_music_data_source_test.dart +++ b/test/system/datasources/moor_music_data_source_test.dart @@ -14,7 +14,7 @@ void main() { MoorMusicDataSource.withQueryExecutor(VmDatabase.memory()); albumModel = AlbumModel( - title: TITLE_1, + title: ALBUM_TITLE_1, artist: ARTIST_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, diff --git a/test/system/models/album_model_test.dart b/test/system/models/album_model_test.dart index e482c08..3b14b87 100644 --- a/test/system/models/album_model_test.dart +++ b/test/system/models/album_model_test.dart @@ -11,7 +11,7 @@ class MockAlbumInfo extends Mock implements AlbumInfo {} void main() { final tAlbumModel = AlbumModel( - title: TITLE_1, + title: ALBUM_TITLE_1, artist: ARTIST_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, @@ -32,14 +32,14 @@ void main() { // arrange final expected = MoorAlbum( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, ); final albumModel = AlbumModel( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, ); @@ -58,14 +58,14 @@ void main() { // arrange final moorAlbum = MoorAlbum( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, ); final expected = AlbumModel( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, year: YEAR_1, ); @@ -82,13 +82,13 @@ void main() { // arrange final moorAlbum = MoorAlbum( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, year: YEAR_1, ); final expected = AlbumModel( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, year: YEAR_1, ); // act @@ -104,13 +104,13 @@ void main() { // arrange final moorAlbum = MoorAlbum( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, ); final expected = AlbumModel( artist: ARTIST_1, - title: TITLE_1, + title: ALBUM_TITLE_1, albumArtPath: ALBUM_ART_PATH_1, ); // act @@ -126,7 +126,7 @@ void main() { setUp(() { mockAlbumInfo = MockAlbumInfo(); - when(mockAlbumInfo.title).thenReturn(TITLE_1); + when(mockAlbumInfo.title).thenReturn(ALBUM_TITLE_1); when(mockAlbumInfo.albumArt).thenReturn(ALBUM_ART_PATH_1); when(mockAlbumInfo.artist).thenReturn(ARTIST_1); when(mockAlbumInfo.firstYear).thenReturn(FIRST_YEAR_1.toString()); @@ -139,7 +139,7 @@ void main() { () async { // arrange final expected = AlbumModel( - title: TITLE_1, + title: ALBUM_TITLE_1, artist: ARTIST_1, albumArtPath: ALBUM_ART_PATH_1, year: FIRST_YEAR_1, diff --git a/test/system/models/song_model_test.dart b/test/system/models/song_model_test.dart new file mode 100644 index 0000000..8f63316 --- /dev/null +++ b/test/system/models/song_model_test.dart @@ -0,0 +1,24 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mosh/domain/entities/song.dart'; +import 'package:mosh/system/models/song_model.dart'; + +import '../../test_constants.dart'; + +void main() { + final tSongModel = SongModel( + title: SONG_TITLE_3, + album: ALBUM_TITLE_3, + artist: ARTIST_3, + path: PATH_3, + trackNumber: TRACKNUMBER_3, + albumArtPath: ALBUM_ART_PATH_3, + ); + + test( + 'should be subclass of Album entity', + () async { + // assert + expect(tSongModel, isA()); + }, + ); +} diff --git a/test/system/repositories/music_data_repository_impl_test.dart b/test/system/repositories/music_data_repository_impl_test.dart index a7395a5..ce98809 100644 --- a/test/system/repositories/music_data_repository_impl_test.dart +++ b/test/system/repositories/music_data_repository_impl_test.dart @@ -29,20 +29,7 @@ void main() { musicDataSource: mockMusicDataSource, ); - tAlbumList = [ - AlbumModel( - artist: ARTIST_1, - title: TITLE_1, - albumArtPath: ALBUM_ART_PATH_1, - year: YEAR_1, - ), - AlbumModel( - artist: ARTIST_2, - title: TITLE_2, - albumArtPath: ALBUM_ART_PATH_2, - year: YEAR_2, - ), - ]; + tAlbumList = setupAlbumList(tAlbumList); tEmptyList = []; }); @@ -130,3 +117,18 @@ void main() { ); }); } + +List setupAlbumList(List tAlbumList) => [ + AlbumModel( + artist: ARTIST_1, + title: ALBUM_TITLE_1, + albumArtPath: ALBUM_ART_PATH_1, + year: YEAR_1, + ), + AlbumModel( + artist: ARTIST_2, + title: ALBUM_TITLE_2, + albumArtPath: ALBUM_ART_PATH_2, + year: YEAR_2, + ), + ]; diff --git a/test/test_constants.dart b/test/test_constants.dart index 654ccb4..8365b69 100644 --- a/test/test_constants.dart +++ b/test/test_constants.dart @@ -1,4 +1,6 @@ -const String TITLE_1 = 'Back in Black'; + +// Albums +const String ALBUM_TITLE_1 = 'Back in Black'; const String ARTIST_1 = 'AC/DC'; const String ALBUM_ART_PATH_1 = '/music/acdc/backinblack.jpg'; const int YEAR_1 = 1980; @@ -6,7 +8,23 @@ const int FIRST_YEAR_1 = 1979; const int LAST_YEAR_1 = 1980; const int NUM_SONGS_1 = 10; -const String TITLE_2 = 'Twilight Of The Thunder God'; +const String ALBUM_TITLE_2 = 'Twilight Of The Thunder God'; const String ARTIST_2 = 'Amon Amarth'; const String ALBUM_ART_PATH_2 = '/music/amon-amarth/twilight.jpg'; -const int YEAR_2 = 2008; \ No newline at end of file +const int YEAR_2 = 2008; + + +// Songs +const String SONG_TITLE_3 = 'Bottom Feeder'; +const String ALBUM_TITLE_3 = 'Ire'; +const String ARTIST_3 = 'Parkway Drive'; +const String PATH_3 = '/music/parkwaydrive/bottom_feeder.mp3'; +const String ALBUM_ART_PATH_3 = '/music/parkwaydrive/ire.jpg'; +const int TRACKNUMBER_3 = 7; + +const String SONG_TITLE_4 = 'Black Flame'; +const String ALBUM_TITLE_4 = 'Black Flame'; +const String ARTIST_4 = 'Bury Tomorrow'; +const String PATH_4 = '/music/burytomorrow/blackflame.mp3'; +const String ALBUM_ART_PATH_4 = '/music/parkwaydrive/blackflame.jpg'; +const int TRACKNUMBER_4 = 3; \ No newline at end of file