import os
import pytest
import hashlib
from utils.search_result_parsing import ProteomicSearchResultParser


def md5sum(filename):
    data = open(filename, 'rb').read()
    return hashlib.md5(data).hexdigest()


def get_tmppdir(tmpdir, filename):
    p = tmpdir.join(filename)
    return p


test_data = [('tests/integration_tests/pd_parser/job/proteome_discoverer_test.xlsx',
              'protein_groups_1.yaml',
              'tests/integration_tests/pd_parser/expected_results/correct_protein_groups_1.yaml'),
             ('tests/integration_tests/pd_parser/job/PD_test2.xlsx',
              'protein_groups_2.yaml',
              'tests/integration_tests/pd_parser/expected_results/correct_protein_groups_2.yaml')]
decoy_exp = 'REV_$'
style = 'styles/proteome_discoverer_2_5.yaml'
required_sample_descriptors = []

@pytest.mark.parametrize("xlsx, output_yaml, correct_pg_yaml", test_data)
def test_pg_yaml_from_pd(tmpdir, xlsx, output_yaml, correct_pg_yaml):
    test_result = get_tmppdir(tmpdir, output_yaml)
    protein_parser = ProteomicSearchResultParser(xlsx, style, test_result, decoy_exp,
                                                 required_sample_descriptors)
    protein_parser.write_protein_groups_yaml()
    assert md5sum(test_result) == md5sum(correct_pg_yaml)