def generate_songs(self, dto): """ Generate dto random songs and add them to the repository. dto must be a positive integer as string. dto="number" """ if int(dto) < 1: raise ValueError("Invalid number") def random_chr(j): v = ["a", "e", "i", "o", "u"] c = [ "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "w", "x", "y", "z" ] return v[trunc(random() * len(v))] if j % 2 == 0 else c[trunc(random() * len(c))] genres = ["Rock", "Pop", "Jazz", "Altele"] for i in range(int(dto)): artist_len1 = trunc(random() * 8 + 1) artist_len2 = trunc(random() * 8 + 1) title_len1 = trunc(random() * 16 + 1) title_len2 = trunc(random() * 16 + 1) artist = "".join([random_chr(i) for i in range(artist_len1)] + [" "] + [random_chr(i) for i in range(artist_len2)]) title = "".join([random_chr(i) for i in range(title_len1)] + [" "] + [random_chr(i) for i in range(title_len2)]) duration = trunc(random() * 500) genre = genres[trunc(random() * len(genres))] song = Song(title, artist, genre, duration) self._repository.add(song)
def test_add(): remove("test_repo.txt") repository = SongFileRepository("test_repo.txt") repository.add(Song("a", "b", "c", "d")) with open("test_repo.txt", "r") as file: line = file.readline().strip() assert line == "b,a,d,c"
def get_all(self): """Return a list of all the songs as Song instances.""" result = [] with open(self._filename, "r") as file: for line in file: song = Song("", "", "", "").from_string(line.strip()) result.append(song) return result
def add_song(self, dto): """ Convert dto to song and add it to repository after successful validation. If there is already a song with the artist-title combination in dto ValueError will be raised. dto="title,artist,genre,playtime" """ song = Song("", "", "", "").from_string(dto) if not self._validator.validate(song): raise ValueError("Invalid song details") self._repository.add(song)
def add(self, entity): """Add entity to repository. If there is a song with entity's title and author ValueError will be raised.""" with open(self._filename, "r") as file: for line in file: song = Song("", "", "", "").from_string(line.strip()) if song.get_artist() == entity.get_artist() and \ song.get_title() == entity.get_title(): raise ValueError("Song already exists") with open(self._filename, "a") as file: file.write("{}\n".format(entity.to_string()))
def mod_song(self, dto): """ Convert dto to song and after successful validation modify the playtime and genre of the song with artist-title combination found in dto. If there isn't a song with this combination ValueError will be raised. dto="title,artist,genre,playtime" """ song = Song("", "", "", "").from_string(dto) if not self._validator.validate(song): raise ValueError("Invalid song details") self._repository.mod(song)
def get(self, playlist_id: int) -> Playlist: cursor = self.database_connector.cursor() query = "SELECT * FROM Playlists WHERE playlist_id = %s" cursor.execute(query, (playlist_id, )) results = cursor.fetchone() if results is None: cursor.close() raise PlaylistNotFound() playlist_id, title = results playlist = Playlist() playlist.playlist_id = playlist_id playlist.title = title query = "SELECT user_id FROM Users_Playlists WHERE playlist_id = %s" cursor.execute(query, (playlist_id, )) user_id, = cursor.fetchone() playlist.user_id = user_id query = "SELECT s.song_id, s.album_id, s.artist_id, s.title, s.duration FROM Songs s, Playlist_Songs ps WHERE " \ "s.song_id = ps.song_id AND ps.playlist_id = %s" cursor.execute(query, (playlist_id, )) for (song_id, album_id, artist_id, title, duration) in cursor: song = Song() song.song_id = song_id song.artist_id = artist_id song.album_id = album_id song.title = title song.duration = duration playlist.songs.append(song) for song in playlist.songs: query = "SELECT artist_name FROM Artists WHERE artist_id = %s" cursor.execute(query, (song.artist_id, )) artist_name, = cursor.fetchone() song.artist_name = artist_name for song in playlist.songs: query = "SELECT title FROM Albums WHERE album_id = %s" cursor.execute(query, (song.album_id, )) title, = cursor.fetchone() song.album_name = title cursor.nextset() cursor.close() return playlist
def retrive(self, album_id: int) -> Album: cursor = self.database_connector.cursor() query = "SELECT * FROM Albums WHERE album_id = %s" cursor.execute(query, (album_id,)) album = Album() (album_id, title, year, artist_id, genre_id) = cursor.fetchone() album.album_id = album_id album.artist_id = artist_id album.year = year album.genre_id = genre_id album.title = title query = "SELECT genre_name FROM Genres WHERE genre_id = %s" cursor.execute(query, (album.genre_id,)) genre_name, = cursor.fetchone() album.genre_name = genre_name query = "SELECT artist_name FROM Artists WHERE artist_id = %s" cursor.execute(query, (album.artist_id,)) artist_name, = cursor.fetchone() album.artist_name = artist_name query = "SELECT * FROM Songs WHERE album_id = %s" cursor.execute(query, (album_id,)) songs = [] for (song_id, album_id, artist_id, title, duration) in cursor: song = Song() song.song_id = song_id song.artist_id = artist_id song.artist_name = album.artist_name song.album_id = album.album_id song.album_name = album.title song.title = title song.duration = duration song.genre_id = album.genre_id song.genre_name = album.genre_name songs.append(song) cursor.close() album.songs = songs return album
def mod(self, entity): """ Modify an existing entity. If the artist-title combination of entity is not found ValueError will be raised. """ found = False with open(self._filename, "r") as file: with open("temp.txt", "w") as temp_file: for line in file: target = song = Song("", "", "", "").from_string(line.strip()) if song.get_artist() == entity.get_artist() and \ song.get_title() == entity.get_title(): found, target = True, entity temp_file.write("{}\n".format(target.to_string())) remove(self._filename) rename("temp.txt", self._filename) if not found: raise ValueError("Song not found")
def test_from_string(): song = Song("", "", "", "").from_string("artist,title,120,Jazz") assert song.get_artist() == "artist" assert song.get_title() == "title" assert song.get_playtime() == "120" assert song.get_genre() == "Jazz"
def test_to_string(): assert Song("title", "artist", "Rock", "120").to_string() == "artist,title,120,Rock"