# Path: loom/util.py
# class LoomError(Exception):
#     pass
# 
# @contextlib.contextmanager
# def tempdir(cleanup_on_error=True):
#     oldwd = os.getcwd()
#     wd = tempfile.mkdtemp()
#     try:
#         os.chdir(wd)
#         yield wd
#         cleanup_on_error = True
#     finally:
#         os.chdir(oldwd)
#         if cleanup_on_error:
#             shutil.rmtree(wd)
# 
# Path: loom/test/util.py
# def for_each_dataset(fun):
#     @functools.wraps(fun)
#     def test_one(dataset):
#         paths = loom.store.get_paths(dataset, sample_count=2)
#         for key, path in loom.store.iter_paths(dataset, paths):
#             if not os.path.exists(path):
#                 raise ValueError(
#                     'missing {} at {},\n  first `python -m loom.datasets test`'
#                     .format(key, path))
#         kwargs = {'name': dataset}
#         kwargs.update(paths['query'])
#         kwargs.update(paths['consensus'])
#         kwargs.update(paths['samples'][0])
#         kwargs.update(paths['ingest'])
#         kwargs.update(paths)
#         fun(**kwargs)
# 
#     @functools.wraps(fun)
#     def test_all():
#         for dataset in loom.datasets.TEST_CONFIGS:
#             yield test_one, dataset
# 
#     return test_all
# 
# CLEANUP_ON_ERROR = int(os.environ.get('CLEANUP_ON_ERROR', 1))


import os
import csv
import mock
import numpy.random
import loom.store
import loom.format
import loom.datasets
import loom.tasks
from nose.tools import raises
from distributions.io.stream import open_compressed, json_load, json_dump
from loom.util import LoomError, tempdir
from loom.test.util import for_each_dataset, CLEANUP_ON_ERROR
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


GARBAGE = 'XXX garbage XXX'


def csv_load(filename):
    with open_compressed(filename) as f:
        reader = csv.reader(f)
        return list(reader)


def csv_dump(data, filename):
    with open_compressed(filename, 'w') as f:
        writer = csv.writer(f)
        for row in data:
            writer.writerow(row)


@for_each_dataset
@raises(LoomError)
def test_missing_schema_error(name, rows_csv, **unused):

# Path: loom/util.py
# class LoomError(Exception):
#     pass
# 
# @contextlib.contextmanager
# def tempdir(cleanup_on_error=True):
#     oldwd = os.getcwd()
#     wd = tempfile.mkdtemp()
#     try:
#         os.chdir(wd)
#         yield wd
#         cleanup_on_error = True
#     finally:
#         os.chdir(oldwd)
#         if cleanup_on_error:
#             shutil.rmtree(wd)
# 
# Path: loom/test/util.py
# def for_each_dataset(fun):
#     @functools.wraps(fun)
#     def test_one(dataset):
#         paths = loom.store.get_paths(dataset, sample_count=2)
#         for key, path in loom.store.iter_paths(dataset, paths):
#             if not os.path.exists(path):
#                 raise ValueError(
#                     'missing {} at {},\n  first `python -m loom.datasets test`'
#                     .format(key, path))
#         kwargs = {'name': dataset}
#         kwargs.update(paths['query'])
#         kwargs.update(paths['consensus'])
#         kwargs.update(paths['samples'][0])
#         kwargs.update(paths['ingest'])
#         kwargs.update(paths)
#         fun(**kwargs)
# 
#     @functools.wraps(fun)
#     def test_all():
#         for dataset in loom.datasets.TEST_CONFIGS:
#             yield test_one, dataset
# 
#     return test_all
# 
# CLEANUP_ON_ERROR = int(os.environ.get('CLEANUP_ON_ERROR', 1))


# Path: loom/test/test_ingest.py
import os
import csv
import mock
import numpy.random
import loom.store
import loom.format
import loom.datasets
import loom.tasks
from nose.tools import raises
from distributions.io.stream import open_compressed, json_load, json_dump
from loom.util import LoomError, tempdir
from loom.test.util import for_each_dataset, CLEANUP_ON_ERROR

# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


GARBAGE = 'XXX garbage XXX'


def csv_load(filename):
    with open_compressed(filename) as f:
        reader = csv.reader(f)
        return list(reader)


def csv_dump(data, filename):
    with open_compressed(filename, 'w') as f:
        writer = csv.writer(f)
        for row in data:
            writer.writerow(row)


@for_each_dataset
@raises(LoomError)
def test_missing_schema_error(name, rows_csv, **unused):

    with tempdir(cleanup_on_error=CLEANUP_ON_ERROR) as store: