def add(name: str, artist: str = None, return_existing: bool = False) -> AlbumDb: """Add album to the database ## Arguments: - `name`: `str`: - Name of the album - `artist`: `str`, optional: - Album artist, will add artist to the database if it doesn't exist and use the existing artist if it does. Defaults to `None`. - `return_existing`: `bool`, optional: - Return existing database object when found or not. Defaults to `False`. ## Raises: - `IntegrityError`: - If the album already exists and `return_existing` is `False` ## Returns: - `AlbumDb`: - The created album, or existing album when `return_existing` is `True` and it already exists """ existing = get_by_name(name=name) if existing is not None: if not return_existing: raise IntegrityError("album already exists") if not existing.album_artist and artist: existing.album_artist = artist_logic.add(artist, return_existing=True) return existing return AlbumDb( name=name.lower(), name_alt=clean_album(name), album_artist=artist_logic.add(artist, return_existing=True) if artist is not None else None, )
def add( song: SongIn, return_existing: bool = False, update_existing: bool = False, replace_existing_tags: bool = False, ) -> SongDb: """Add song to the database ## Arguments: - `song`: `SongIn`: - The song you want to add - `return_existing`: `bool`, optional: - Return existing database object when found or not. Defaults to `False`. - `update_existing`: `bool`, optional: - Update the existing song when found or not. Only updates the `albums` and `tags` properties `return_existing` also needs to be `True` for this to work. Defaults to `False`. - `replace_existing_tags`: `bool`, optional: - Remove all `tag` relationships from the song and the new ones. `update_existing` also need to be `True` for this to work. Defaults to `False`. ## Raises: - `IntegrityError`: - If the song already exists and `return_existing` is `False` ## Returns: - `SongDb`: - The created song, or existing song when `return_existing` is `true` """ artists = clean_artist(song.artist) artists = artist_logic.split(artists) existing = get(title=song.title, artists=artists) if existing is not None: if not return_existing: raise IntegrityError("Song already exists") elif update_existing: existing.albums.add( album_logic.add(name=song.album, artist=song.album_artist, return_existing=True)) if not existing.length and song.length: existing.length = song.length if replace_existing_tags: existing.tags.clear() for tag in song.tags: existing.tags.add( tag_logic.add(tag_type=tag.tag_type, value=tag.value, return_existing=True)) return existing return SongDb( title=song.title.lower(), title_alt=song.title, length=song.length, tags=[ tag_logic.add(tag.tag_type, tag.value, return_existing=True) for tag in song.tags ], albums=album_logic.add(name=song.album, artist=song.album_artist, return_existing=True), artists=[ artist_logic.add(artist, return_existing=True) for artist in artists ], )
def test_add_artist_cleaned_name(): artist = artist_logic.add("hallo (cv. test)") assert orm.count(a for a in ArtistDb) == 1 assert artist.name == "hallo"
def test_add_artist_cased_alt_name(): artist = artist_logic.add("Hallo") assert orm.count(a for a in ArtistDb) == 1 assert artist.name == "hallo" assert artist.name_alt == "Hallo"
def test_add_artist(): artist_logic.add("hallo") assert orm.count(a for a in ArtistDb) == 1
def test_get_artist_by_name_case_difference(): db_artist = artist_logic.add("Artist") artist = artist_logic.get_by_name(name="artist") assert artist is not None assert db_artist.name == artist.name
def test_add_artist_existing_with_return_existing(): db_artist = mixer.blend(ArtistDb) artist = artist_logic.add(db_artist.name, return_existing=True) assert orm.count(a for a in ArtistDb) == 1 assert artist is not None assert db_artist.id == artist.id
def test_add_artist_existing(): db_artist = mixer.blend(ArtistDb) with pytest.raises(IntegrityError): artist_logic.add(db_artist.name)