<commit_before># -*- coding: utf-8 -*-

import logging
import unittest
import os
from pyctd.manager.models import GeneAltGeneId, Pathway, DiseasePathway, Disease, Chemical, ChemGeneIxn, Gene,\
    ChemicalDisease, GeneDiseasePubmed, GeneDiseaseOmim, GeneDisease, ChemGeneIxnGeneForm, ChemGeneIxnPubmed, \
    ChemGeneIxnInteractionAction, ChemicalDiseasePubmedid, ChemicalDiseaseOmim, ChemicalSynonym, ChemicalDrugbank, \
    ChemicalTreenumber, ChemicalParentid, GeneUniprot, GeneSynonym, GenePharmgkb, GenePathway, GeneBiogrid, \
    DiseaseSynonym, DiseaseSlimmapping, DiseaseAltdiseaseid, Action, ChemGoEnriched, ChemicalParenttreenumber, \
    ChemPathwayEnriched, ExposureEvent


import pyctd
import shutil
from pyctd.manager.database import table_conf
from pyctd.constants import PYCTD_DATA_DIR
from pyctd.manager.defaults import sqlalchemy_connection_string_4_tests

log = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
test_data_location = "data"
connection = sqlalchemy_connection_string_4_tests


def download_urls(dummy1, dummy2):
    """
    overwrites pyctd.manager.database.DbManager.download_urls in TestImport
    :param dummy1: dummy parameter
    :param dummy2: dummy parameter
    """
    file_names = [x['file_name'] for x in list(table_conf.tables.values())]
    for file_name in file_names:
        test_file_path = os.path.join(dir_path, test_data_location, file_name)
        destination_path = os.path.join(PYCTD_DATA_DIR, file_name)
        shutil.copy(test_file_path, destination_path)


class TestImport(unittest.TestCase):
    def setUp(self):
        pyctd.manager.database.DbManager.download_urls = download_urls
        pyctd.update(connection=connection)
        self.query = pyctd.manager.query.QueryManager(connection=connection)
        self.session = pyctd.manager.database.BaseDbManager(connection=connection).session

    def test_number_of_inserts(self):
        models = [
            (GeneAltGeneId, 6),
            (Pathway, 3),
            (DiseasePathway, 6),
            (Disease, 3),
            (Chemical, 3),
            (ChemGeneIxn, 6),
            (Gene, 3),
            (ChemicalDisease, 6),
            (GeneDiseasePubmed, 12),
            (GeneDiseaseOmim, 12),
            (GeneDisease, 6),
            (ChemGeneIxnGeneForm, 12),
            (ChemGeneIxnPubmed, 12),
            (ChemGeneIxnInteractionAction, 6),
            (ChemicalDiseasePubmedid, 12),
            (ChemicalDiseaseOmim, 12),
            (ChemicalSynonym, 6),
            (ChemicalDrugbank, 6),
            (ChemicalTreenumber, 6),
            (ChemicalParentid, 6),
            (GeneUniprot, 6),
            (GeneSynonym, 6),
            (GenePharmgkb, 6),
            (GenePathway, 6),
            (GeneBiogrid, 6),
            (DiseaseSynonym, 6),
            (DiseaseSlimmapping, 6),
            (DiseaseAltdiseaseid, 6),
            (Action, 3),
            (ChemGoEnriched, 3),
            (ChemicalParenttreenumber, 6),
            (ChemPathwayEnriched, 3),
            (ExposureEvent, 3)
        ]
        for model, num_of_results in models:
            self.assertEqual(num_of_results, self.session.query(model).count())

    def test_get_disease(self):
        r = self.query.get_disease(limit=1)[0]
        self.assertEqual([x.alt_disease_id for x in r.alt_disease_ids], ['AltDiseaseID1_1', 'AltDiseaseID1_2'])
        self.assertEqual(r.definition, 'Definition1')
        self.assertEqual(r.disease_id, 'DiseaseID1')
        self.assertEqual(r.disease_name, 'DiseaseName1')
        self.assertEqual(r.parent_ids, 'ParentID1_1|ParentID1_2')
        self.assertEqual(r.parent_tree_numbers, 'ParentTreeNumber1_1|ParentTreeNumber1_2')
        self.assertEqual([x.slim_mapping for x in r.slim_mappings], ['SlimMapping1_1', 'SlimMapping1_2'])
        self.assertEqual([x.synonym for x in r.synonyms], ['Synonym1_1', 'Synonym1_2'])
        self.assertEqual(r.tree_numbers, 'TreeNumber1_1|TreeNumber1_2')
        self.session.commit()

    def test_get_gene(self):
        r = self.query.get_gene(limit=1)[0]
        self.assertEqual([x.alt_gene_id for x in r.alt_gene_ids], [1, 2])
        self.assertEqual([x.biogrid_id for x in r.biogrid_ids], [1, 2])
        self.assertEqual(r.gene_id, 1)
        self.assertEqual(r.gene_name, 'GeneName1')
        self.assertEqual(r.gene_symbol, 'GeneSymbol1')
        self.assertEqual([x.pharmgkb_id for x in r.pharmgkb_ids], ['PharmGKBID1_1', 'PharmGKBID1_2'])
        self.assertEqual([x.synonym for x in r.synonyms] ,['Synonym1_1', 'Synonym1_2'])
        self.assertEqual([x.uniprot_id for x in r.uniprot_ids], ['UniProtID1_1', 'UniProtID1_2'])
        self.session.commit()

    def test_get_pathway(self):
        r = self.query.get_pathway()[0]
        self.assertEqual(r.pathway_id, 'PathwayID:1')
        self.assertEqual(r.pathway_name, 'PathwayName1')
        self.session.commit()

    def test_get_chemicals(self):
        chemical = self.query.get_chemical()[0]
        self.assertEqual(chemical.chemical_id, 'ChemicalID1')
        self.assertEqual(chemical.chemical_name, 'ChemicalName1')
        self.assertEqual(chemical.cas_rn, 'CasRN1')
        self.session.commit()

    def test_get_chem_gene_interaction_actions(self):
        r = self.query.get_chem_gene_interaction_actions()[0]
        #self.assertEqual()

    def test_gene_forms(self):
        r = self.query.gene_forms
        self.assertEqual(r, ['GeneForm1_1',<commit_msg>test_pathway corrected<commit_after># -*- coding: utf-8 -*-

import logging
import unittest
import os
from pyctd.manager.models import GeneAltGeneId, Pathway, DiseasePathway, Disease, Chemical, ChemGeneIxn, Gene,\
    ChemicalDisease, GeneDiseasePubmed, GeneDiseaseOmim, GeneDisease, ChemGeneIxnGeneForm, ChemGeneIxnPubmed, \
    ChemGeneIxnInteractionAction, ChemicalDiseasePubmedid, ChemicalDiseaseOmim, ChemicalSynonym, ChemicalDrugbank, \
    ChemicalTreenumber, ChemicalParentid, GeneUniprot, GeneSynonym, GenePharmgkb, GenePathway, GeneBiogrid, \
    DiseaseSynonym, DiseaseSlimmapping, DiseaseAltdiseaseid, Action, ChemGoEnriched, ChemicalParenttreenumber, \
    ChemPathwayEnriched, ExposureEvent


import pyctd
import shutil
from pyctd.manager.database import table_conf
from pyctd.constants import PYCTD_DATA_DIR
from pyctd.manager.defaults import sqlalchemy_connection_string_4_tests

log = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
test_data_location = "data"
connection = sqlalchemy_connection_string_4_tests


def download_urls(dummy1, dummy2):
    """
    overwrites pyctd.manager.database.DbManager.download_urls in TestImport
    :param dummy1: dummy parameter
    :param dummy2: dummy parameter
    """
    file_names = [x['file_name'] for x in list(table_conf.tables.values())]
    for file_name in file_names:
        test_file_path = os.path.join(dir_path, test_data_location, file_name)
        destination_path = os.path.join(PYCTD_DATA_DIR, file_name)
        shutil.copy(test_file_path, destination_path)


class TestImport(unittest.TestCase):
    def setUp(self):
        pyctd.manager.database.DbManager.download_urls = download_urls
        pyctd.update(connection=connection)
        self.query = pyctd.manager.query.QueryManager(connection=connection)
        self.session = pyctd.manager.database.BaseDbManager(connection=connection).session

    def test_number_of_inserts(self):
        models = [
            (GeneAltGeneId, 6),
            (Pathway, 3),
            (DiseasePathway, 6),
            (Disease, 3),
            (Chemical, 3),
            (ChemGeneIxn, 6),
            (Gene, 3),
            (ChemicalDisease, 6),
            (GeneDiseasePubmed, 12),
            (GeneDiseaseOmim, 12),
            (GeneDisease, 6),
            (ChemGeneIxnGeneForm, 12),
            (ChemGeneIxnPubmed, 12),
            (ChemGeneIxnInteractionAction, 6),
            (ChemicalDiseasePubmedid, 12),
            (ChemicalDiseaseOmim, 12),
            (ChemicalSynonym, 6),
            (ChemicalDrugbank, 6),
            (ChemicalTreenumber, 6),
            (ChemicalParentid, 6),
            (GeneUniprot, 6),
            (GeneSynonym, 6),
            (GenePharmgkb, 6),
            (GenePathway, 6),
            (GeneBiogrid, 6),
            (DiseaseSynonym, 6),
            (DiseaseSlimmapping, 6),
            (DiseaseAltdiseaseid, 6),
            (Action, 3),
            (ChemGoEnriched, 3),
            (ChemicalParenttreenumber, 6),
            (ChemPathwayEnriched, 3),
            (ExposureEvent, 3)
        ]
        for model, num_of_results in models:
            self.assertEqual(num_of_results, self.session.query(model).count())

    def test_get_disease(self):
        r = self.query.get_disease(limit=1)[0]
        self.assertEqual([x.alt_disease_id for x in r.alt_disease_ids], ['AltDiseaseID1_1', 'AltDiseaseID1_2'])
        self.assertEqual(r.definition, 'Definition1')
        self.assertEqual(r.disease_id, 'DiseaseID1')
        self.assertEqual(r.disease_name, 'DiseaseName1')
        self.assertEqual(r.parent_ids, 'ParentID1_1|ParentID1_2')
        self.assertEqual(r.parent_tree_numbers, 'ParentTreeNumber1_1|ParentTreeNumber1_2')
        self.assertEqual([x.slim_mapping for x in r.slim_mappings], ['SlimMapping1_1', 'SlimMapping1_2'])
        self.assertEqual([x.synonym for x in r.synonyms], ['Synonym1_1', 'Synonym1_2'])
        self.assertEqual(r.tree_numbers, 'TreeNumber1_1|TreeNumber1_2')
        self.session.commit()

    def test_get_gene(self):
        r = self.query.get_gene(limit=1)[0]
        self.assertEqual([x.alt_gene_id for x in r.alt_gene_ids], [1, 2])
        self.assertEqual([x.biogrid_id for x in r.biogrid_ids], [1, 2])
        self.assertEqual(r.gene_id, 1)
        self.assertEqual(r.gene_name, 'GeneName1')
        self.assertEqual(r.gene_symbol, 'GeneSymbol1')
        self.assertEqual([x.pharmgkb_id for x in r.pharmgkb_ids], ['PharmGKBID1_1', 'PharmGKBID1_2'])
        self.assertEqual([x.synonym for x in r.synonyms] ,['Synonym1_1', 'Synonym1_2'])
        self.assertEqual([x.uniprot_id for x in r.uniprot_ids], ['UniProtID1_1', 'UniProtID1_2'])
        self.session.commit()

    def test_get_pathway(self):
        r = self.query.get_pathway()[0]
        self.assertEqual(r.pathway_id, 'PathwayID1')
        self.assertEqual(r.pathway_name, 'PathwayName1')
        self.session.commit()

    def test_get_chemicals(self):
        chemical = self.query.get_chemical()[0]
        self.assertEqual(chemical.chemical_id, 'ChemicalID1')
        self.assertEqual(chemical.chemical_name, 'ChemicalName1')
        self.assertEqual(chemical.cas_rn, 'CasRN1')
        self.session.commit()

    def test_get_chem_gene_interaction_actions(self):
        r = self.query.get_chem_gene_interaction_actions()[0]
        #self.assertEqual()

    def test_gene_forms(self):
        r = self.query.gene_forms
        self.assertEqual(r, ['GeneForm1_1',
{'fraction_non_alphanumeric': 0.11008418740849195, 'fraction_numerical': 0.019399707174231333, 'mean_word_length': 3.1586757990867578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6112435', 'n_tokens_mistral': 3930, 'n_tokens_neox': 3579, 'n_words': 610}