# Path: egasub/submission/submittable/unaligned.py
# class Unaligned(Experiment):
#     pass
# 
# Path: egasub/submission/submittable/alignment.py
# class Alignment(Analysis):
#     def __init__(self, path):
#         super(Alignment, self).__init__(path)
# 
#         if not re.match(r'^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+){0,1}$', self.submission_dir):
#             raise Exception("Submission directory should be named as <sample alias> or <sample alias>.<lane label>, sample alias and lane label may only contain letter, digit, underscore (_) or dash (-)")
# 
# 
#     def local_validate(self, ega_enums):
#         super(Alignment, self).local_validate(ega_enums)
#         # Analysis type validation, 0 - Reference Alignment (BAM)
#         if not str(self.analysis.analysis_type_id) == "0":
#             self._add_local_validation_error("analysis",self.analysis.alias,"analysisTypes", "Invalid value '%s', analysisTypeId must be '0' for alignment data." % self.analysis.analysis_type_id)
# 
# Path: egasub/submission/submittable/base.py
# def _get_md5sum(md5sum_file):
#     try:
#         checksum = open(md5sum_file, 'r').readline().rstrip()
#     except:
#         raise Md5sumFileError("Please make sure md5sum file '%s' exist" % md5sum_file)
# 
#     if not re.findall(r"^[a-fA-F\d]{32}$", checksum):
#         raise Md5sumFileError("Please make sure md5sum file '%s' contain valid md5sum string" % md5sum_file)
#     return checksum.lower()


from egasub.submission.submittable import _get_md5sum, Alignment, Unaligned
from click.testing import CliRunner
import hashlib
import os
import yaml
        md5_file = "fileName.md5"
        f = open(os.path.join(submission_dir,md5_file),"wb")
        f.write(m.hexdigest())
        f.close()

        file_path = "experiment.yaml"
        with open(os.path.join(submission_dir,file_path),"w") as outfile:
            yaml.dump(dict(
                sample = dict(alias="an alias"),
                experiment = dict(title="a title"),
                run = dict(runFileTypeId="ID"),
                files=[dict(fileName="fileName", checksumMethod="md5")])
                      , outfile)

        unaligned = Unaligned(submission_dir)

        _type = "type"
        _alias = "alias"
        _field = "field"
        _message = "error message"

        unaligned._add_local_validation_error(_type,_alias,_field,_message)
        unaligned._add_ftp_file_validation_error(_field,_message)

        assert len(unaligned.local_validation_errors) == 1
        assert unaligned.local_validation_errors[0]['object_alias'] == _alias
        assert unaligned.local_validation_errors[0]['field'] == _field
        assert unaligned.local_validation_errors[0]['object_type'] == _type
        assert unaligned.local_validation_errors[0]['error'] == _message
        assert len(unaligned.ftp_file_validation_errors) == 1

# Path: egasub/submission/submittable/unaligned.py
# class Unaligned(Experiment):
#     pass
# 
# Path: egasub/submission/submittable/alignment.py
# class Alignment(Analysis):
#     def __init__(self, path):
#         super(Alignment, self).__init__(path)
# 
#         if not re.match(r'^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+){0,1}$', self.submission_dir):
#             raise Exception("Submission directory should be named as <sample alias> or <sample alias>.<lane label>, sample alias and lane label may only contain letter, digit, underscore (_) or dash (-)")
# 
# 
#     def local_validate(self, ega_enums):
#         super(Alignment, self).local_validate(ega_enums)
#         # Analysis type validation, 0 - Reference Alignment (BAM)
#         if not str(self.analysis.analysis_type_id) == "0":
#             self._add_local_validation_error("analysis",self.analysis.alias,"analysisTypes", "Invalid value '%s', analysisTypeId must be '0' for alignment data." % self.analysis.analysis_type_id)
# 
# Path: egasub/submission/submittable/base.py
# def _get_md5sum(md5sum_file):
#     try:
#         checksum = open(md5sum_file, 'r').readline().rstrip()
#     except:
#         raise Md5sumFileError("Please make sure md5sum file '%s' exist" % md5sum_file)
# 
#     if not re.findall(r"^[a-fA-F\d]{32}$", checksum):
#         raise Md5sumFileError("Please make sure md5sum file '%s' contain valid md5sum string" % md5sum_file)
#     return checksum.lower()


# Path: tests/submission/test_submittable.py
from egasub.submission.submittable import _get_md5sum, Alignment, Unaligned
from click.testing import CliRunner
import hashlib
import os
import yaml

        md5_file = "fileName.md5"
        f = open(os.path.join(submission_dir,md5_file),"wb")
        f.write(m.hexdigest())
        f.close()

        file_path = "experiment.yaml"
        with open(os.path.join(submission_dir,file_path),"w") as outfile:
            yaml.dump(dict(
                sample = dict(alias="an alias"),
                experiment = dict(title="a title"),
                run = dict(runFileTypeId="ID"),
                files=[dict(fileName="fileName", checksumMethod="md5")])
                      , outfile)

        unaligned = Unaligned(submission_dir)

        _type = "type"
        _alias = "alias"
        _field = "field"
        _message = "error message"

        unaligned._add_local_validation_error(_type,_alias,_field,_message)
        unaligned._add_ftp_file_validation_error(_field,_message)

        assert len(unaligned.local_validation_errors) == 1
        assert unaligned.local_validation_errors[0]['object_alias'] == _alias
        assert unaligned.local_validation_errors[0]['field'] == _field
        assert unaligned.local_validation_errors[0]['object_type'] == _type
        assert unaligned.local_validation_errors[0]['error'] == _message
        assert len(unaligned.ftp_file_validation_errors) == 1

        assert unaligned.ftp_file_validation_errors[0]['field'] == _field