"""Test the ``dtool dataset create`` command."""

import os

from click.testing import CliRunner

from dtoolcore import DataSet

from . import chdir_fixture, tmp_dir_fixture  # NOQA


def test_dataset_create_functional(chdir_fixture):  # NOQA
    from dtool_create.dataset import create
    runner = CliRunner()

    dataset_name = "my_dataset"
    result = runner.invoke(create, [dataset_name])
    assert result.exit_code == 0

    # Test that the dataset has been created.
    dataset = DataSet.from_path(dataset_name)

    # Test that the dataset name is correct.
    assert dataset.name == dataset_name



def test_dataset_create_fails_on_directory_exists(chdir_fixture):  # NOQA
    from dtool_create.dataset import create
    runner = CliRunner()

    dataset_name = "my_dataset"
    os.mkdir(dataset_name)
    result = runner.invoke(create, [dataset_name])
    assert result.exit_code != 0
    assert result.output.startswith("Usage")
    assert result.output.find("File/directory already exists") != -1


def test_dataset_create_can_work_outside_current_directory(tmp_dir_fixture):  # NOQA
    from dtool_create.dataset import create
    runner = CliRunner()

    dataset_name = "my_dataset"
    dataset_path = os.path.join(tmp_dir_fixture, dataset_name)
    result = runner.invoke(create, [dataset_path])
    assert result.exit_code == 0

    # Test that the dataset has been created.
    dataset = DataSet.from_path(dataset_path)

    # Test that the dataset name is correct.
    assert dataset.name == dataset_name

@@ -112,16 +112,30 @@
 e import
+ ProtoDataSet,
  DataSet
@@ -449,32 +449,38 @@
 # Test that the 
+proto 
 dataset has been
@@ -504,41 +504,148 @@
 aset
- = DataSet.from_path(dataset_name
+_abspath = os.path.abspath(dataset_name)%0A    dataset_uri = %22disk:%7B%7D%22.format(dataset_abspath)%0A    dataset = ProtoDataSet.from_uri(dataset_uri
 )%0A%0A 
@@ -1526,19 +1526,73 @@
  dataset
- = 
+_uri = %22disk:%7B%7D%22.format(dataset_path)%0A    dataset = Proto
 DataSet.
@@ -1596,20 +1596,19 @@
 et.from_
-path
+uri
 (dataset
@@ -1608,20 +1608,19 @@
 dataset_
-path
+uri
 )%0A%0A    #

Fix dtool create tests