예제 #1
0
def test_search_page(factory: Factory, db: Connection):
    for _ in range(5):
        factory.collection(conn=db)

    c1 = collection.search(db, page=1, per_page=1)[0]
    c2 = collection.search(db, page=2, per_page=1)[0]
    assert c1 != c2
예제 #2
0
def test_search_user(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    unused_usr, _ = factory.user(conn=db)

    col1 = factory.collection(type=CollectionType.PERSONAL, user=usr, conn=db)
    col2 = factory.collection(type=CollectionType.PERSONAL, user=usr, conn=db)

    col3 = factory.collection(type=CollectionType.PERSONAL, user=unused_usr, conn=db)
    col4 = factory.collection(conn=db)

    cols = collection.search(db, user_ids=[usr.id])
    ids = [c.id for c in cols]
    assert all(c.id in ids for c in [col1, col2])
    assert not any(c.id in ids for c in [col3, col4])
예제 #3
0
def test_from_name_and_type(factory: Factory, db: Connection):
    usr1, _ = factory.user(conn=db)
    col1 = factory.collection(
        name="test", type=CollectionType.SYSTEM, user=usr1, conn=db
    )

    usr2, _ = factory.user(conn=db)
    col2 = factory.collection(
        name="test", type=CollectionType.SYSTEM, user=usr2, conn=db
    )

    factory.collection(name="other", type=CollectionType.COLLAGE, conn=db)

    cols = collection.from_name_and_type("test", CollectionType.SYSTEM, conn=db)
    assert {c.id for c in cols} == {col1.id, col2.id}
예제 #4
0
def test_top_genres(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    releases = [factory.release(conn=db) for _ in range(4)]
    genres = [factory.collection(type=CollectionType.GENRE, conn=db) for _ in range(4)]

    for i, rls in enumerate(releases):
        collection.add_release(col, rls.id, db)
        for grn in genres[: i + 1]:
            collection.add_release(grn, rls.id, db)

    tg = collection.top_genres(col, db)

    for i in range(4):
        assert tg[i]["genre"].id == genres[i].id
        assert tg[i]["num_matches"] == 4 - i
예제 #5
0
def test_from_name_type_user_success(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    new_col = collection.from_name_type_user(col.name, col.type, db)
    assert new_col is not None

    assert col.name == new_col.name
    assert col.type == new_col.type
예제 #6
0
def test_add_release_failure(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    rls = factory.release(conn=db)
    collection.add_release(col, rls.id, db)

    with pytest.raises(AlreadyExists):
        collection.add_release(col, rls.id, db)
예제 #7
0
def test_release_collections_filter_type(factory: Factory, db: Connection):
    rls = factory.release(conn=db)
    usr, _ = factory.user(conn=db)

    cols = [
        factory.collection(type=CollectionType.SYSTEM, user=usr, conn=db)
        for _ in range(2)
    ]
    for _ in range(3):
        cols.append(factory.collection(type=CollectionType.COLLAGE, conn=db))

    for col in cols:
        collection.add_release(col, rls.id, db)

    out = release.collections(rls, db, type=CollectionType.SYSTEM)

    assert len(out) == 3
    assert {c.id for c in out if c.id != 1} == {c.id for c in cols[:2]}
예제 #8
0
def test_release_collections(factory: Factory, db: Connection):
    rls = factory.release(conn=db)
    cols = [factory.collection(conn=db) for _ in range(5)]

    for col in cols:
        collection.add_release(col, rls.id, db)

    out = release.collections(rls, db)
    assert {c.id for c in cols} == {c.id for c in out}
예제 #9
0
def test_search_types(factory: Factory, db: Connection):
    col = factory.collection(type=CollectionType.GENRE, conn=db)
    collections = collection.search(
        db,
        types=[CollectionType.SYSTEM, CollectionType.GENRE],
        search=col.name,
    )
    assert len(collections) == 1
    assert collections[0].id == col.id
예제 #10
0
def test_releases(factory: Factory, db: Connection):
    releases = [factory.release(conn=db) for _ in range(4)]
    col = factory.collection(conn=db)

    for rls in releases:
        collection.add_release(col, rls.id, db)

    out = collection.releases(col, db)
    assert {r.id for r in releases} == {r.id for r in out}
예제 #11
0
def test_del_release(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    rls = factory.release(conn=db)
    collection.add_release(col, rls.id, db)

    new_col = collection.del_release(col, rls.id, db)
    assert new_col.num_releases == col.num_releases - 1  # type: ignore

    releases = collection.releases(col, db)
    assert releases == []
예제 #12
0
def test_add_release(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    rls = factory.release(conn=db)

    new_col = collection.add_release(col, rls.id, db)
    assert new_col.num_releases == col.num_releases + 1  # type: ignore

    releases = collection.releases(col, db)
    assert len(releases) == new_col.num_releases  # type: ignore
    assert releases[0].id == rls.id
예제 #13
0
def test_image(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    img = factory.mock_image(conn=db)
    rls = factory.release(image_id=img.id, conn=db)

    collection.add_release(col, rls.id, db)

    new_img = collection.image(col, db)
    assert new_img is not None
    assert new_img.id == img.id
예제 #14
0
def test_query(factory: Factory, db: Connection):
    col = factory.collection(name="Country", conn=db)
    cursor = db.execute(
        """
        SELECT rowid FROM music__collections__fts
        WHERE music__collections__fts MATCH '"Country"'
        ORDER BY rank
        """
    )
    assert cursor.fetchone()[0] == col.id
예제 #15
0
def test_update_duplicate(factory: Factory, db: Connection):
    col1 = factory.collection(conn=db)
    col2 = factory.collection(conn=db)

    with pytest.raises(Duplicate) as e:
        collection.update(
            col2,
            conn=db,
            name=col1.name,
        )

    assert e.value.entity == col1
예제 #16
0
def test_search_filter_collections(factory: Factory, db: Connection):
    releases = [factory.release(conn=db) for _ in range(5)]

    col1 = factory.collection(conn=db)
    col2 = factory.collection(conn=db)

    for rls in releases[:3]:
        collection.add_release(col1, rls.id, db)
    for rls in releases[1:]:
        collection.add_release(col2, rls.id, db)

    out = release.search(db, collection_ids=[col1.id, col2.id])
    assert set(releases[1:3]) == set(out)
예제 #17
0
def test_top_genres(factory: Factory, db: Connection):
    ply = factory.playlist(conn=db)
    releases = [factory.release(conn=db) for _ in range(4)]
    genres = [
        factory.collection(type=CollectionType.GENRE, conn=db)
        for _ in range(4)
    ]

    for i, rls in enumerate(releases):
        trk = factory.track(release_id=rls.id, conn=db)
        pentry.create(ply.id, trk.id, db)
        for grn in genres[:i + 1]:
            collection.add_release(grn, rls.id, db)

    tg = playlist.top_genres(ply, db)

    for i in range(4):
        assert tg[i]["genre"].id == genres[i].id
        assert tg[i]["num_matches"] == 4 - i
예제 #18
0
def test_top_genres(factory: Factory, db: Connection):
    art = factory.artist(conn=db)
    releases = [
        factory.release(
            artists=[{"artist_id": art.id, "role": ArtistRole.MAIN}],
            conn=db,
        )
        for _ in range(4)
    ]
    genres = [factory.collection(type=CollectionType.GENRE, conn=db) for _ in range(4)]

    for i, rls in enumerate(releases):
        for col in genres[: i + 1]:
            collection.add_release(col, rls.id, db)

    tg = artist.top_genres(art, db)

    for i in range(4):
        assert tg[i]["genre"].id == genres[i].id
        assert tg[i]["num_matches"] == 4 - i
예제 #19
0
def test_image_nonexistent(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    assert collection.image(col, db) is None
예제 #20
0
def test_from_id_success(factory: Factory, db: Connection):
    col_id = factory.collection(conn=db).id
    col = collection.from_id(col_id, db)
    assert col is not None
    assert col.id == col_id
예제 #21
0
def test_exists(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    assert collection.exists(col.id, db)
예제 #22
0
def test_create_duplicate(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    with pytest.raises(Duplicate):
        collection.create(col.name, col.type, db)
예제 #23
0
def test_search_all(factory: Factory, db: Connection):
    cols = {factory.collection(conn=db) for _ in range(5)}
    assert cols == set(collection.search(db))
예제 #24
0
def test_from_name_type_user_with_user_id(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    col = factory.collection(conn=db, type=CollectionType.PERSONAL, user=usr)
    new_col = collection.from_name_type_user(col.name, col.type, db, usr.id)
    assert col == new_col
예제 #25
0
def test_update_fields(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    new_col = collection.update(col, conn=db, name="New Name")
    assert new_col == collection.from_id(col.id, db)
    assert new_col.name == "New Name"
예제 #26
0
def test_unstar(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    col = factory.collection(conn=db, starred_for_user=usr.id)
    collection.unstar(col, usr.id, db)
    assert not collection.starred(col, usr.id, db) is True
예제 #27
0
def test_del_release_failure(factory: Factory, db: Connection):
    col = factory.collection(conn=db)
    rls = factory.release(conn=db)

    with pytest.raises(DoesNotExist):
        collection.del_release(col, rls.id, db)
예제 #28
0
def test_update_immutable(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    col = factory.collection(type=CollectionType.SYSTEM, user=usr, conn=db)

    with pytest.raises(Immutable):
        collection.update(col, conn=db, name="New Name")
예제 #29
0
def test_user(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    col = factory.collection(user=usr, type=CollectionType.SYSTEM, conn=db)
    col_user = collection.user(col, conn=db)
    assert col_user is not None
    assert usr.id == col_user.id
예제 #30
0
def test_star(factory: Factory, db: Connection):
    usr, _ = factory.user(conn=db)
    col = factory.collection(conn=db)
    collection.star(col, usr.id, db)
    assert collection.starred(col, usr.id, db) is True