# Path: snakemakelib/bio/ngs/tools/sratools.py
# def register_metadata(metadata_file, config):
#     """Read an SRA project file and register metadata in sml_config. Will
#     issue a warning if file does not exists.
# 
#     Args: 
#       metadata - file name
#       config - configuration to update
#     """
#     metadata_list = []
#     import sys
#     if metadata_file in sys.argv:
#         return config
#     try:
#         with open(metadata_file, "r") as fh:
#             reader = csv.DictReader(fh.readlines())
#         metadata_list = [row for row in reader]
#         run2sample = {row["Run"]:row["SampleName"] for row in metadata_list}
#         config_default = {
#             'bio.ngs.settings' : {
#                 'sampleinfo' : metadata_file
#             },
#             'bio.ngs.tools.sratools': {
#                 '_datadir': os.path.dirname(metadata_file),
#                 '_run2sample' : run2sample,
#                 '_metadata' : metadata_list
#             },
#         }
#         update_config(config_default, config)
#         config = config_default
#         
#     except Exception:
#         raise Exception("""
# 
#         no metadata file '{metadata}' found
# 
#         please initiate analysis by running 'snakemake {metadata}'
# 
#         """.format(metadata=metadata_file))
#     return config


import unittest
import logging
import io
import mock
from nose.tools import raises
from snakemakelib.bio.ngs.tools.sratools import register_metadata
# Copyright (C) 2015 by Per Unneberg
# pylint: disable=R0904

logger = logging.getLogger(__name__)

class TestSraTools(unittest.TestCase):
    """Test sratools functionality"""
    def setUp(self):
        self.metadata = io.StringIO("\n".join(["SampleName,Run",
                                               "Sample1,Run1S1",
                                               "Sample1,Run2S1",
                                               "Sample2,Run1S2"]))

    @raises(Exception)
    def test_register_metadata(self):
        """Test registering metadata for non-existent file"""
        register_metadata("foo.csv", config = {})

    @mock.patch('snakemakelib.bio.ngs.tools.sratools.update_config')
    @mock.patch('builtins.open')
    def test_read(self, mock_open, mock_update):
        """Test reading mock indata and check update arguments"""
        mock_open.return_value = self.metadata

# Path: snakemakelib/bio/ngs/tools/sratools.py
# def register_metadata(metadata_file, config):
#     """Read an SRA project file and register metadata in sml_config. Will
#     issue a warning if file does not exists.
# 
#     Args: 
#       metadata - file name
#       config - configuration to update
#     """
#     metadata_list = []
#     import sys
#     if metadata_file in sys.argv:
#         return config
#     try:
#         with open(metadata_file, "r") as fh:
#             reader = csv.DictReader(fh.readlines())
#         metadata_list = [row for row in reader]
#         run2sample = {row["Run"]:row["SampleName"] for row in metadata_list}
#         config_default = {
#             'bio.ngs.settings' : {
#                 'sampleinfo' : metadata_file
#             },
#             'bio.ngs.tools.sratools': {
#                 '_datadir': os.path.dirname(metadata_file),
#                 '_run2sample' : run2sample,
#                 '_metadata' : metadata_list
#             },
#         }
#         update_config(config_default, config)
#         config = config_default
#         
#     except Exception:
#         raise Exception("""
# 
#         no metadata file '{metadata}' found
# 
#         please initiate analysis by running 'snakemake {metadata}'
# 
#         """.format(metadata=metadata_file))
#     return config


# Path: snakemakelib/bio/ngs/tools/tests/test_sratools.py
import unittest
import logging
import io
import mock
from nose.tools import raises
from snakemakelib.bio.ngs.tools.sratools import register_metadata

# Copyright (C) 2015 by Per Unneberg
# pylint: disable=R0904

logger = logging.getLogger(__name__)

class TestSraTools(unittest.TestCase):
    """Test sratools functionality"""
    def setUp(self):
        self.metadata = io.StringIO("\n".join(["SampleName,Run",
                                               "Sample1,Run1S1",
                                               "Sample1,Run2S1",
                                               "Sample2,Run1S2"]))

    @raises(Exception)
    def test_register_metadata(self):
        """Test registering metadata for non-existent file"""
        register_metadata("foo.csv", config = {})

    @mock.patch('snakemakelib.bio.ngs.tools.sratools.update_config')
    @mock.patch('builtins.open')
    def test_read(self, mock_open, mock_update):
        """Test reading mock indata and check update arguments"""
        mock_open.return_value = self.metadata

        cfg = register_metadata("foo/bar", config = {})