<commit_before>"""
Copyright (C) 2016  Genome Research Ltd.

Author: <NAME> <<EMAIL>>

This program is part of meta-check

meta-check is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

This file has been created on Jun 10, 2016.
"""
import unittest

from collections import defaultdict
from mcheck.metadata.irods_metadata.file_metadata import IrodsSeqFileMetadata
from mcheck.metadata.file_header_metadata.header_metadata import SAMFileHeaderMetadata
from mcheck.metadata.seqscape_metadata.seqscape_metadata import SeqscapeMetadata
from mcheck.checks.mchecks_by_comparison import FileMetadataComparison
from mcheck.results.checks_results import RESULT

class FileMetadataComparisonTest(unittest.TestCase):

    def test_find_differences_when_there_are_diffs_1(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S1'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S9'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result,{'samples': {'name': set(['S1'])}})

    def test_find_differences_when_there_are_diffs2(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S9'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result,{'samples': {'name': set(['S9'])}})


    def test_find_differences_when_there_are_diff_type_ids(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S1']), 'accession_number': set(['Acc1'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result, {})

    def test_find_differences_when_many_diffs(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S9']), 'accession_number': set(['Acc9'])},
                                                libraries={'internal_id': set(['123'])}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={'internal_id': set(['444'])}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples', 'libraries', 'studies'])
        self.assertEqual(result,{'samples': {'name': set(['S9'])}, 'libraries': {'internal_id': set(['123'])}})


    def test_find_differences_when_ok(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(), 'internal_id': set()},
                                              libraries={}, studies={})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S1']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={}, studies={})
        result = FileMetadataComparison._find_differences(irods_metadata, header_metadata, ['samples'])
        self.assertDictEqual(result, {})


    def test_find_differences_when_entity_type_not_found_in_meta(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(), 'internal_id': set()},
                                              libraries={}, studies={})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S1']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={}, studies={})
        self.assertRaises(AttributeError, FileMetadataComparison._find_differences, irods_metadata, header_metadata, ['nonexisting_entity'])
        # TODO: Comparison should throw an error as a CheckResult if AttributeError occurs (the attribute doesn't exist


<commit_msg>Added more tests to mchecks_by_comparison<commit_after>"""
Copyright (C) 2016  Genome Research Ltd.

Author: <NAME> <<EMAIL>>

This program is part of meta-check

meta-check is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

This file has been created on Jun 10, 2016.
"""
import unittest

from collections import defaultdict
from mcheck.metadata.irods_metadata.file_metadata import IrodsSeqFileMetadata
from mcheck.metadata.file_header_metadata.header_metadata import SAMFileHeaderMetadata
from mcheck.metadata.seqscape_metadata.seqscape_metadata import SeqscapeMetadata
from mcheck.checks.mchecks_by_comparison import FileMetadataComparison
from mcheck.results.checks_results import RESULT

class FileMetadataComparisonTest(unittest.TestCase):

    def test_find_differences_when_there_are_diffs_h_vs_ss1(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S1'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S9'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result,{'samples': {'name': set(['S1'])}})


    def test_find_differences_when_there_are_diffs_h_vs_ss2(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S9'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result,{'samples': {'name': set(['S9'])}})


    def test_find_differences_when_there_are_diff_type_ids_h_vs_ss(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam', samples={'name': set(['S1']), 'accession_number': set(['Acc1'])}, libraries={}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples'])
        self.assertEqual(result, {})


    def test_find_differences_when_many_diffs_h_vs_ss(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S9']), 'accession_number': set(['Acc9'])},
                                                libraries={'internal_id': set(['123'])}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S1'])}, libraries={'internal_id': set(['444'])}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples', 'libraries', 'studies'])
        self.assertEqual(result,{'samples': {'name': set(['S9'])}, 'libraries': {'internal_id': set(['123'])}})


    def test_find_differences_when_no_diffs_h_vs_ss(self):
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S9']), 'accession_number': set(['Acc9'])},
                                                libraries={'internal_id': set(['123'])}, studies={})
        seqscape_metadata = SeqscapeMetadata(samples={'name': set(['S9']), 'accession_number': set(['Acc9'])},
                                             libraries={'internal_id': set(['123'])}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, seqscape_metadata, ['samples', 'libraries', 'studies'])
        self.assertEqual(result,{})


    def test_find_differences_when_no_diffs_i_vs_h(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(), 'internal_id': set()},
                                              libraries={}, studies={})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S1']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={}, studies={})
        result = FileMetadataComparison._find_differences(irods_metadata, header_metadata, ['samples'])
        self.assertDictEqual(result, {})


    def test_find_differences_when_diff_id_types_i_vs_h(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(['EGA1']), 'internal_id': set('1')},
                                              libraries={'name': '123', 'internal_id': '123'},
                                              studies={'name': "Crohns disease", 'accession_number': 'EGAS4', 'internal_id': '4'})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S1']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={'internal_id': 123}, studies={})
        result = FileMetadataComparison._find_differences(irods_metadata, header_metadata, ['samples'])
        self.assertDictEqual(result, {})


    def test_find_differences_when_diffs_i_vs_h(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(['EGA1']), 'internal_id': set('1')},
                                              libraries={'name': '123', 'internal_id': '123'},
                                              studies={'name': "Crohns disease", 'accession_number': 'EGAS4', 'internal_id': '4'})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S100']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={'internal_id': 456}, studies={})
        result = FileMetadataComparison._find_differences(irods_metadata, header_metadata, ['samples'])
        self.assertDictEqual(result, {'samples': {'name': set(['S1'])}})

    def test_find_differences_when_diffs_h_vs_i(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(['EGA1']), 'internal_id': set('1')},
                                              libraries={'name': '123', 'internal_id': '123'},
                                              studies={'name': "Crohns disease", 'accession_number': 'EGAS4', 'internal_id': '4'})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S100']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={'internal_id': 456}, studies={})
        result = FileMetadataComparison._find_differences(header_metadata, irods_metadata, ['samples'])
        self.assertDictEqual(result, {'samples': {'name': set(['S100'])}})


    def test_find_differences_when_entity_type_not_found_in_meta(self):
        irods_metadata = IrodsSeqFileMetadata('/seq/123.bam',
                                              samples={'name': set(['S1']), 'accession_number': set(), 'internal_id': set()},
                                              libraries={}, studies={})
        header_metadata = SAMFileHeaderMetadata('/seq/123.bam', '123/bam',
                                                samples={'name': set(['S1']), 'accession_number' : set(), 'internal_id': set()},
                                                libraries={}, studies={})
        self.assertRaises(AttributeError, FileMetadataComparison._find_differences, irods_metadata, header_metadata, ['nonexisting_entity'])
        # TODO: Comparison should throw an error as a CheckResult if AttributeError occurs (the attribute doesn't exist



{'fraction_non_alphanumeric': 0.1402292138112271, 'fraction_numerical': 0.018833491495729614, 'mean_word_length': 2.98371619656877, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 76, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4885615', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3531, 'n_words': 789}