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

import os
import shutil

from click.testing import CliRunner

from dtoolcore import DataSet

from . import chdir_fixture, tmp_dir_fixture  # NOQA
from . import SAMPLE_FILES_DIR


def test_dataset_freeze_functional(chdir_fixture):  # NOQA
    from dtool_create.dataset import freeze
    runner = CliRunner()

    # Create an empty dataset
    dataset_name = "my_dataset"
    dataset = DataSet(dataset_name, data_directory="data")
    dataset.persist_to_path(".")

    # Add some files to it.
    dest_dir = os.path.join(".", dataset.data_directory, "sample_files")
    shutil.copytree(SAMPLE_FILES_DIR, dest_dir)

    # At this point the manifest has not been updated.
    assert len(dataset.identifiers) == 0

    result = runner.invoke(freeze, ["."])
    assert result.exit_code == 0

    # Manifest has been updated.
    assert len(dataset.identifiers) == 2

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

import os
import shutil

from click.testing import CliRunner

from dtoolcore import DataSet, ProtoDataSet

from . import chdir_fixture, tmp_dir_fixture  # NOQA
from . import SAMPLE_FILES_DIR


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

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

    # At this point we have a proto dataset
    dataset_abspath = os.path.abspath(dataset_name)
    dataset_uri = "disk:{}".format(dataset_abspath)
    dataset = ProtoDataSet.from_uri(dataset_uri)

    # Add a file to the proto dataset.
    sample_file_abspath = os.path.join(dataset_abspath, "data", "hello.txt")
    with open(sample_file_abspath, "w") as fh:
        fh.write("hello world")

    result = runner.invoke(freeze, [dataset_uri])
    assert result.exit_code == 0


    # Now we have a dataset.
    dataset = DataSet.from_uri(dataset_uri)

    # Manifest has been updated.
    assert len(dataset.identifiers) == 1

Fix the freeze functional test
Fix the freeze functional test

  """Test the ``dtool dataset create`` command."""
  
  import os
  import shutil
  
  from click.testing import CliRunner
  
- from dtoolcore import DataSet
+ from dtoolcore import DataSet, ProtoDataSet
  
  from . import chdir_fixture, tmp_dir_fixture  # NOQA
  from . import SAMPLE_FILES_DIR
  
  
  def test_dataset_freeze_functional(chdir_fixture):  # NOQA
-     from dtool_create.dataset import freeze
+     from dtool_create.dataset import create, freeze
      runner = CliRunner()
  
-     # Create an empty dataset
      dataset_name = "my_dataset"
-     dataset = DataSet(dataset_name, data_directory="data")
-     dataset.persist_to_path(".")
- 
-     # Add some files to it.
-     dest_dir = os.path.join(".", dataset.data_directory, "sample_files")
-     shutil.copytree(SAMPLE_FILES_DIR, dest_dir)
- 
-     # At this point the manifest has not been updated.
-     assert len(dataset.identifiers) == 0
- 
-     result = runner.invoke(freeze, ["."])
+     result = runner.invoke(create, [dataset_name])
      assert result.exit_code == 0
  
+     # At this point we have a proto dataset
+     dataset_abspath = os.path.abspath(dataset_name)
+     dataset_uri = "disk:{}".format(dataset_abspath)
+     dataset = ProtoDataSet.from_uri(dataset_uri)
+ 
+     # Add a file to the proto dataset.
+     sample_file_abspath = os.path.join(dataset_abspath, "data", "hello.txt")
+     with open(sample_file_abspath, "w") as fh:
+         fh.write("hello world")
+ 
+     result = runner.invoke(freeze, [dataset_uri])
+     assert result.exit_code == 0
+ 
+ 
+     # Now we have a dataset.
+     dataset = DataSet.from_uri(dataset_uri)
+ 
      # Manifest has been updated.
-     assert len(dataset.identifiers) == 2
+     assert len(dataset.identifiers) == 1
  
Fix the freeze functional test
## Code Before:
"""Test the ``dtool dataset create`` command."""

import os
import shutil

from click.testing import CliRunner

from dtoolcore import DataSet

from . import chdir_fixture, tmp_dir_fixture  # NOQA
from . import SAMPLE_FILES_DIR


def test_dataset_freeze_functional(chdir_fixture):  # NOQA
    from dtool_create.dataset import freeze
    runner = CliRunner()

    # Create an empty dataset
    dataset_name = "my_dataset"
    dataset = DataSet(dataset_name, data_directory="data")
    dataset.persist_to_path(".")

    # Add some files to it.
    dest_dir = os.path.join(".", dataset.data_directory, "sample_files")
    shutil.copytree(SAMPLE_FILES_DIR, dest_dir)

    # At this point the manifest has not been updated.
    assert len(dataset.identifiers) == 0

    result = runner.invoke(freeze, ["."])
    assert result.exit_code == 0

    # Manifest has been updated.
    assert len(dataset.identifiers) == 2

## Instruction:
Fix the freeze functional test
## Code After:
"""Test the ``dtool dataset create`` command."""

import os
import shutil

from click.testing import CliRunner

from dtoolcore import DataSet, ProtoDataSet

from . import chdir_fixture, tmp_dir_fixture  # NOQA
from . import SAMPLE_FILES_DIR


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

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

    # At this point we have a proto dataset
    dataset_abspath = os.path.abspath(dataset_name)
    dataset_uri = "disk:{}".format(dataset_abspath)
    dataset = ProtoDataSet.from_uri(dataset_uri)

    # Add a file to the proto dataset.
    sample_file_abspath = os.path.join(dataset_abspath, "data", "hello.txt")
    with open(sample_file_abspath, "w") as fh:
        fh.write("hello world")

    result = runner.invoke(freeze, [dataset_uri])
    assert result.exit_code == 0


    # Now we have a dataset.
    dataset = DataSet.from_uri(dataset_uri)

    # Manifest has been updated.
    assert len(dataset.identifiers) == 1
