Example #1
0
def test_read_invalid_json_type(tmp_path):
    make_album(tmp_path, "[]")

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert str(
        ex_info.value
    ) == f"Invalid index format in '{tmp_path}/index.json': Album is not an object"
Example #2
0
def test_read_invalid_json(tmp_path):
    make_album(tmp_path, "not json")

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert str(
        ex_info.value
    ) == f"Could not parse JSON in '{tmp_path}/index.json': Expecting value at 1:1"
Example #3
0
def test_read_invalid_album_name(tmp_path):
    make_album(tmp_path, {"name": 23})

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert (
        str(ex_info.value) ==
        f"Invalid index format in '{tmp_path}/index.json': Attribute 'name' in album is not a string"
    )
Example #4
0
def test_read_missing_album_name(tmp_path):
    make_album(tmp_path, "{}")

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert (
        str(ex_info.value) ==
        f"Invalid index format in '{tmp_path}/index.json': Attribute 'name' is missing in album"
    )
Example #5
0
def test_read_missing_track_path(tmp_path):
    make_album(tmp_path, '{"name": "foo", "tracks": [{"length": 101}]}')

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert (
        str(ex_info.value) ==
        f"Invalid index format in '{tmp_path}/index.json': Attribute 'path' is missing in track 1 of disc 1"
    )
Example #6
0
def test_read_invalid_tracks_type(tmp_path):
    make_album(tmp_path, '{"name": "foo", "tracks": 12}')

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert (
        str(ex_info.value) ==
        f"Invalid index format in '{tmp_path}/index.json': Attribute 'tracks' in album is not an array"
    )
Example #7
0
def test_read_album_without_tracks(tmp_path):
    index = {"name": "John Doe - One Day"}
    make_album(tmp_path, index)

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert len(result.tracks) == 0
Example #8
0
def test_read_album_path(tmp_path):
    index = {"name": "foo"}
    make_album(tmp_path, index)

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert result.path == tmp_path
Example #9
0
def test_read_with_complete_metadata(tmp_path):
    index = {
        "name":
        "John Doe - One Day",
        "artist":
        "John Doe",
        "title":
        "One Day",
        "musicbrainz_id":
        "000-0",
        "discs": [{
            "path":
            "01",
            "tracks": [{
                "path": "01.ogg",
                "title": "Another Dreadful Morning",
                "artist": "John Doe",
                "musicbrainz_id": "000-1",
                "length": 101,
            }],
        }],
    }
    make_album(tmp_path, index)

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert result.title == "One Day"
    assert result.artists == ["John Doe"]
    assert result.musicbrainz_id == "000-0"
    assert result.tracks[0].title == "Another Dreadful Morning"
    assert result.tracks[0].artists == ["John Doe"]
    assert result.tracks[0].musicbrainz_id == "000-1"
    assert result.tracks[0].duration_ms == 101000
Example #10
0
def test_read_with_multiple_discs_with_path(tmp_path):
    index = {
        "name":
        "John Doe - One Day",
        "discs": [
            {
                "path": "01",
                "tracks": [{
                    "path": "01.ogg"
                }]
            },
            {
                "path": "02",
                "tracks": [{
                    "path": "01.ogg"
                }, {
                    "path": "02.ogg"
                }]
            },
        ],
    }
    make_album(tmp_path, index)

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert len(result.tracks) == 3
    assert result.tracks[0].disc_no == 1
    assert result.tracks[0].track_no == 1
    assert result.tracks[0].path == tmp_path / "01/01.ogg"
    assert result.tracks[1].disc_no == 2
    assert result.tracks[1].track_no == 1
    assert result.tracks[1].path == tmp_path / "02/01.ogg"
    assert result.tracks[2].disc_no == 2
    assert result.tracks[2].track_no == 2
    assert result.tracks[2].path == tmp_path / "02/02.ogg"
Example #11
0
def test_read_album_defaults(tmp_path):
    make_album(tmp_path, '{"name": "foo"}')

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert result.title == ""
    assert result.artists == []
    assert result.musicbrainz_id == ""
Example #12
0
def test_read_track_defaults(tmp_path):
    make_album(tmp_path, '{"name": "foo", "tracks": [{"path": "01.ogg"}]}')

    result = AlbumIndex.read_from_file(tmp_path / "index.json")

    assert result.tracks[0].title == ""
    assert result.tracks[0].artists == []
    assert result.tracks[0].musicbrainz_id == ""
Example #13
0
def test_read_negative_track_length(tmp_path):
    make_album(
        tmp_path,
        {
            "name": "foo",
            "tracks": [{
                "path": "01.ogg",
                "length": -23
            }]
        },
    )

    with raises(IndexFileError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert (
        str(ex_info.value) ==
        f"Invalid index format in '{tmp_path}/index.json': Attribute 'length' in track 1 of disc 1 is negative"
    )
Example #14
0
def test_read_file_not_found(tmp_path):
    with raises(FileNotFoundError) as ex_info:
        AlbumIndex.read_from_file(tmp_path / "index.json")

    assert "No such file or directory" in str(ex_info.value)
    assert str(tmp_path / "index.json") in str(ex_info.value)