import pytest
from topk_sdk import error

from . import ProjectContext


def test_list_datasets(ctx: ProjectContext):
    create_resp = ctx.client.datasets().create(ctx.scope("test"))
    d = create_resp.dataset
    response = ctx.client.datasets().list()
    assert d in response.datasets


def test_create_dataset(ctx: ProjectContext):
    create_resp = ctx.client.datasets().create(ctx.scope("test"))
    d = create_resp.dataset
    response = ctx.client.datasets().list()
    assert d in response.datasets


def test_create_duplicate_dataset(ctx: ProjectContext):
    ctx.client.datasets().create(ctx.scope("test"))

    with pytest.raises(error.DatasetAlreadyExistsError):
        ctx.client.datasets().create(ctx.scope("test"))


def test_delete_non_existent_dataset(ctx: ProjectContext):
    with pytest.raises(error.DatasetNotFoundError):
        ctx.client.datasets().delete(ctx.scope("test"))


def test_delete_dataset(ctx: ProjectContext):
    d = ctx.client.datasets().create(ctx.scope("test")).dataset
    ctx.client.datasets().delete(ctx.scope("test"))

    response = ctx.client.datasets().list()
    assert d not in response.datasets


def test_get_dataset(ctx: ProjectContext):
    # Test getting non-existent dataset
    with pytest.raises(error.DatasetNotFoundError):
        ctx.client.datasets().get(ctx.scope("test"))

    # Create dataset
    create_resp = ctx.client.datasets().create(ctx.scope("test"))
    d = create_resp.dataset

    # Get dataset
    response = ctx.client.datasets().get(ctx.scope("test"))
    assert response.dataset == d
