예제 #1
0
def test_add_album_existing_with_new_album_artist():
    db_album = mixer.blend(AlbumDb, album_artist=None)
    assert not db_album.album_artist
    album = album_logic.add(name=db_album.name,
                            artist="test",
                            return_existing=True)
    assert orm.count(a for a in AlbumDb) == 1
    assert album is not None
    assert db_album.id == album.id
    assert db_album.album_artist.name == "test"
예제 #2
0
파일: song.py 프로젝트: MrRikkert/PyMusic
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
        ],
    )
예제 #3
0
def test_add_album_existing_artist():
    artist = mixer.blend(ArtistDb)
    album_logic.add(name="album", artist=artist.name)
    assert orm.count(a for a in AlbumDb) == 1
    assert orm.count(a for a in ArtistDb) == 1
예제 #4
0
def test_add_album_with_artist():
    album_logic.add(name="album", artist="artist")
    assert orm.count(a for a in AlbumDb) == 1
    assert orm.count(a for a in ArtistDb) == 1
예제 #5
0
def test_add_album_correct_cased_alt_name():
    album = album_logic.add(name="Album disc 1")
    assert orm.count(a for a in AlbumDb) == 1
    assert album.name_alt == "Album"
    assert album.name == "album disc 1"
예제 #6
0
def test_add_album_without_artist():
    album_logic.add(name="album")
    assert orm.count(a for a in AlbumDb) == 1
예제 #7
0
def test_get_album_by_name_case_difference():
    album_logic.add(name="album", artist="artist")
    album = album_logic.get_by_name(name="album")
    assert album is not None
예제 #8
0
def test_add_album_existing_album_with_return_existing():
    db_album = mixer.blend(AlbumDb)
    album = album_logic.add(name=db_album.name, return_existing=True)
    assert orm.count(a for a in AlbumDb) == 1
    assert album is not None
    assert db_album.id == album.id
예제 #9
0
def test_add_album_existing_album():
    album = mixer.blend(AlbumDb)
    with pytest.raises(IntegrityError):
        album_logic.add(name=album.name)