# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):
# 
# Path: ariba/megares_zip_parser.py
# class Error (Exception): pass
# class MegaresZipParser:
#     def __init__(self, zip_url, outprefix):
#     def _extract_files(cls, zip_file, outdir):
#     def _csv_to_dict(cls, infile, delimiter, expected_columns, key_column):
#     def _load_annotations_file(cls, infile):
#     def _load_header_mappings_file(cls, infile):
#     def _write_files(cls, outprefix, sequences, annotations, header_mappings):
#     def run(self):


import unittest
import filecmp
import os
import pyfastaq
from ariba import common, megares_zip_parser

modules_dir = os.path.dirname(os.path.abspath(megares_zip_parser.__file__))
data_dir = os.path.join(modules_dir, 'tests', 'data')


class TestMegaresZipParser(unittest.TestCase):
    def test_extract_files_ok(self):
        '''test _extract_files when all ok'''
        zip_file = os.path.join(data_dir, 'megares_zip_parse_extract_files_ok.zip')
        tmp_dir = 'tmp.test_megares_extract_files_ok'
        got = megares_zip_parser.MegaresZipParser._extract_files(zip_file, tmp_dir)
        common_dir = os.path.join('megares_zip_parse_extract_files_ok', 'megares_v1.01')
        expected = {
            'annotations': os.path.join(common_dir, 'megares_annotations_v1.01.csv'),
            'fasta': os.path.join(common_dir, 'megares_database_v1.01.fasta'),
            'header_mappings': os.path.join(common_dir, 'megares_to_external_header_mappings_v1.01.tsv')
        }

        self.assertEqual(expected, got)

        for filename in expected.values():
            self.assertTrue(os.path.exists(os.path.join(tmp_dir, filename)))

        common.rmtree(tmp_dir)


    def test_extract_files_one_missing(self):
        '''test _extract_files when one missing'''
        zip_file = os.path.join(data_dir, 'megares_zip_parse_extract_files_one_missing.zip')
        tmp_dir = 'tmp.test_megares_extract_files_one_missing'

# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):
# 
# Path: ariba/megares_zip_parser.py
# class Error (Exception): pass
# class MegaresZipParser:
#     def __init__(self, zip_url, outprefix):
#     def _extract_files(cls, zip_file, outdir):
#     def _csv_to_dict(cls, infile, delimiter, expected_columns, key_column):
#     def _load_annotations_file(cls, infile):
#     def _load_header_mappings_file(cls, infile):
#     def _write_files(cls, outprefix, sequences, annotations, header_mappings):
#     def run(self):


# Path: ariba/tests/megares_zip_parser_test.py
import unittest
import filecmp
import os
import pyfastaq
from ariba import common, megares_zip_parser


modules_dir = os.path.dirname(os.path.abspath(megares_zip_parser.__file__))
data_dir = os.path.join(modules_dir, 'tests', 'data')


class TestMegaresZipParser(unittest.TestCase):
    def test_extract_files_ok(self):
        '''test _extract_files when all ok'''
        zip_file = os.path.join(data_dir, 'megares_zip_parse_extract_files_ok.zip')
        tmp_dir = 'tmp.test_megares_extract_files_ok'
        got = megares_zip_parser.MegaresZipParser._extract_files(zip_file, tmp_dir)
        common_dir = os.path.join('megares_zip_parse_extract_files_ok', 'megares_v1.01')
        expected = {
            'annotations': os.path.join(common_dir, 'megares_annotations_v1.01.csv'),
            'fasta': os.path.join(common_dir, 'megares_database_v1.01.fasta'),
            'header_mappings': os.path.join(common_dir, 'megares_to_external_header_mappings_v1.01.tsv')
        }

        self.assertEqual(expected, got)

        for filename in expected.values():
            self.assertTrue(os.path.exists(os.path.join(tmp_dir, filename)))

        common.rmtree(tmp_dir)


    def test_extract_files_one_missing(self):
        '''test _extract_files when one missing'''
        zip_file = os.path.join(data_dir, 'megares_zip_parse_extract_files_one_missing.zip')
        tmp_dir = 'tmp.test_megares_extract_files_one_missing'

        with self.assertRaises(megares_zip_parser.Error):