def setUp(self):
     self.case = VFLocationUploader()
class VFLocationUploaderTestCase(unittest.TestCase):

    def setUp(self):
        self.case = VFLocationUploader()

    def tearDown(self):
        del self.case

    def test_get_gene_name(self):
        string = "agn43|VFO:3000001| - (b2000) - CP4-44 prophage; antigen 43 (Ag43) phase-variable biofilm \
                  formation autotransporter [Escherichia coli str. MG1655 (K12)]"
        gene_name = self.case.get_gene_name(string)
        self.assertEqual(gene_name, "agn43")

    @mock.patch('superphy.upload.gene_location_upload.GeneLocationUploader.create_gene_location')
    @mock.patch('superphy.upload.gene_location_upload.GeneLocationUploader.check_gene_copy')
    @mock.patch('superphy.upload.gene_location_upload.GeneLocationUploader.get_num_gene_copies')
    @mock.patch('superphy.upload.gene_location_upload.NCBIXML.parse')
    @mock.patch('superphy.upload.gene_location_upload.open')
    def test_ncbixml_parse(self, mock_open, mock_parse, mock_copies, mock_check, mock_create):
        mock_handle = mock.MagicMock(spec=file)
        mock_open.return_value = mock.MagicMock(spec=file)

        ## Gene Location w/ 100% query and identity, incomplete genome
        mock_check.return_value = False
        mock_copies.return_value = 0
        mock_parse.return_value = [self.create_sample_record("aafA", "gnl|BL_ORD_ID|56 gi|606962173|gb|JHNV01000057.1| \
                Escherichia coli O119:H4 str. 03-3458 contig57, whole genome shotgun sequence", 0, 1146, 123, 123, "ATGC")]

        self.case.parse_result()
        mock_create.assert_called_once_with("aafA_JHNV01000057_0", "aafA", "JHNV01000057", '1146', '1268', "ATGC", False)

        ## Gene Location w/ 100% query and identity, complete genome
        mock_check.reset_mock()
        mock_parse.reset_mock()
        mock_create.reset_mock()
        mock_check.return_value = False
        mock_parse.return_value = [self.create_sample_record("bapF", "gnl|BL_ORD_ID|56 gi|606962173|gb|CP002729.1| \
                complete genome", 0, 1146, 1230, 1230, "CAT")]
        self.case.parse_result()
        mock_create.assert_called_once_with("bapF_CP002729_closed_0", "bapF", "CP002729_closed", '1146', '2375', "CAT", False)


    def test_get_reference_genes(self):
        """
        Assumes that there is no data uploaded to Blazegraph before executing these tests.
        """
        self.assertEqual(len(list(self.case.get_reference_genes())), 1)

    @mock.patch('superphy.upload.gene_location_upload.GeneLocationUploader.create_gene_location')
    @mock.patch('superphy.upload.gene_location_upload.NCBIXML.parse', autospec=True)
    @mock.patch('superphy.upload.gene_location_upload.open')
    def test_parse_result(self, mock_open, mock_parse, mock_create):
        mock_open.return_value = mock.MagicMock(spec=file)
        mock_parse.return_value = [self.create_sample_record("gaa", "gnl|BL_ORD_ID|56 gi|606962173|gb|JHNV01000056.1| \
                Escherichia coli O119:H4 str. 03-3458 contig56, whole genome shotgun sequence", 
                                            0, 1146, 123, 123, "ATGC")]

        self.case.parse_result()
        mock_create.assert_called_once_with("gaa_JHNV01000056_0", "gaa", "JHNV01000056", '1146', '1268', "ATGC", False)


    def create_sample_record(self, query, title, expect, start, score, ident, seq):
        """
        Helper function that creates Blast record handles for testing NCBI parse-related methods.
        """
        record = mock.MagicMock(spec=Record)
        entry = mock.MagicMock(spec=Record.Alignment)
        hsp = mock.MagicMock(spec=Record.HSP)

        record.query = query

        entry.title = title
        entry.hsps = [hsp]

        hsp.expect = expect
        hsp.sbjct_start = start
        hsp.score = score
        hsp.identities = ident
        hsp.sbjct = seq

        record.alignments = [entry]
        return record