示例#1
0
def resolve_star_artist(_, info: GraphQLResolveInfo, id: int) -> artist.T:
    art = artist.from_id(id, info.context.db)
    if not art:
        raise NotFound(f"Artist {id} does not exist.")

    artist.star(art, info.context.user.id, info.context.db)
    return art
示例#2
0
async def test_unstar_artist(db: Connection, graphql_query, snapshot):
    # Artist 4 should be initially starred.
    art = artist.from_id(4, db)
    assert art is not None
    assert artist.starred(art, user_id=1, conn=db)

    query = """
        mutation {
            unstarArtist(id: 4) {
                ...ArtistFields
            }
        }
    """
    success, data = await graphql_query(query)
    assert success is True
    snapshot.assert_match(data)

    art = artist.from_id(4, db)
    assert art is not None
    assert not artist.starred(art, user_id=1, conn=db)
示例#3
0
def resolve_update_artist(
    _,
    info: GraphQLResolveInfo,
    id: int,
    **changes,
) -> artist.T:
    art = artist.from_id(id, info.context.db)
    if not art:
        raise NotFound(f"Artist {id} does not exist.")

    return artist.update(art, info.context.db, **convert_keys_case(changes))
示例#4
0
async def test_star_artist(db: Connection, graphql_query, snapshot):
    query = """
        mutation {
            starArtist(id: 3) {
                ...ArtistFields
            }
        }
    """
    success, data = await graphql_query(query)
    assert success is True
    snapshot.assert_match(data)

    art = artist.from_id(3, db)
    assert art is not None
    assert artist.starred(art, user_id=1, conn=db)
示例#5
0
async def test_update_artist(db: Connection, graphql_query, snapshot):
    query = """
        mutation {
            updateArtist(id: 4, name: "New Name") {
                ...ArtistFields
            }
        }
    """
    success, data = await graphql_query(query)
    assert success is True
    snapshot.assert_match(data)

    art = artist.from_id(4, db)
    assert art is not None
    assert art.name == "New Name"
示例#6
0
async def test_create_artist(db: Connection, graphql_query, snapshot):
    query = """
        mutation {
            createArtist(name: "New Artist") {
                ...ArtistFields
            }
        }
    """
    success, data = await graphql_query(query)
    assert success is True
    snapshot.assert_match(data)

    art = artist.from_id(data["data"]["createArtist"]["id"], db)
    assert art is not None
    assert art.name == "New Artist"
示例#7
0
def resolve_del_artist_from_release(
    _,
    info: GraphQLResolveInfo,
    releaseId: int,
    artistId: int,
    role: ArtistRole,
) -> dict:
    rls = release.from_id(releaseId, info.context.db)
    if not rls:
        raise NotFound(f"Release {releaseId} does not exist.")

    rls = release.del_artist(rls, artistId, role, info.context.db)
    art = artist.from_id(artistId, info.context.db)

    return {"release": rls, "artist": art}
示例#8
0
def resolve_del_artist_from_track(
    _,
    info: GraphQLResolveInfo,
    trackId: int,
    artistId: int,
    role: ArtistRole,
) -> dict:
    trk = track.from_id(trackId, info.context.db)
    if not trk:
        raise NotFound("Track does not exist.")

    trk = track.del_artist(trk, artistId, role, info.context.db)
    art = artist.from_id(artistId, info.context.db)

    return {"track": trk, "track_artist": {"role": role, "artist": art}}
示例#9
0
def _fetch_or_create_artist(name: str, conn: Connection) -> artist.T:
    """
    Try to fetch an artist from the database with the given name. If one doesn't exist,
    create it. If ``name`` is empty or ``None``, return the Unknown Artist (ID: 1).

    :param name: The name of the artist.
    :param conn: A connection to the database.
    :return: The fetched/created artist.
    """
    if not name:
        art = artist.from_id(1, conn)
        assert art is not None
        return art

    try:
        return artist.create(name, conn)
    except Duplicate as e:
        return e.entity
示例#10
0
def resolve_artist(obj: Any, info: GraphQLResolveInfo, id: int) -> artist.T:
    if art := artist.from_id(id, info.context.db):
        return art
示例#11
0
def test_fetch_or_create_artist_duplicate(factory: Factory, db: Connection):
    art = factory.artist(conn=db)
    assert artist.from_id(art.id, db) == _fetch_or_create_artist(art.name, db)
示例#12
0
def test_fetch_or_create_artist_unknown(db: Connection):
    assert artist.from_id(1, db) == _fetch_or_create_artist("", db)