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

import logging
import unittest
from json import dumps

import os
import tempfile
from requests.compat import urlparse

from pybel import BELGraph
from pybel.constants import *
from pybel.manager.cache import CacheManager
from pybel.parser.parse_bel import BelParser
from pybel.parser.parse_exceptions import *
from pybel.parser.utils import any_subdict_matches

log = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
owl_dir_path = os.path.join(dir_path, 'owl')
bel_dir_path = os.path.join(dir_path, 'bel')
belns_dir_path = os.path.join(dir_path, 'belns')
belanno_dir_path = os.path.join(dir_path, 'belanno')
beleq_dir_path = os.path.join(dir_path, 'beleq')

test_bel_simple = os.path.join(bel_dir_path, 'test_bel.bel')
test_bel_extensions = os.path.join(bel_dir_path, 'test_bel_owl_extension.bel')
test_bel_slushy = os.path.join(bel_dir_path, 'slushy.bel')
test_bel_thorough = os.path.join(bel_dir_path, 'thorough.bel')
test_bel_isolated = os.path.join(bel_dir_path, 'isolated.bel')
test_bel_misordered = os.path.join(bel_dir_path, 'misordered.bel')

test_owl_pizza = os.path.join(owl_dir_path, 'pizza_onto.owl')
test_owl_wine = os.path.join(owl_dir_path, 'wine.owl')
test_owl_ado = os.path.join(owl_dir_path, 'ado.owl')

test_an_1 = os.path.join(belanno_dir_path, 'test_an_1.belanno')

test_ns_1 = os.path.join(belns_dir_path, 'test_ns_1.belns')
test_ns_2 = os.path.join(belns_dir_path, 'test_ns_1_updated.belns')
test_ns_nocache = os.path.join(belns_dir_path, 'test_nocache.belns')

test_eq_1 = os.path.join(beleq_dir_path, 'disease-ontology.beleq')
test_eq_2 = os.path.join(beleq_dir_path, 'mesh-diseases.beleq')

test_citation_dict = {
    CITATION_TYPE: 'PubMed',
    CITATION_NAME: 'TestName',
    CITATION_REFERENCE: '1235813'
}
SET_CITATION_TEST = 'SET Citation = {{"{type}","{name}","{reference}"}}'.format(**test_citation_dict)
test_evidence_text = 'I read it on Twitter'
test_set_evidence = 'SET Evidence = "{}"'.format(test_evidence_text)

CHEBI_KEYWORD = 'CHEBI'
CHEBI_URL = OPENBEL_NAMESPACE_RESOURCES + 'chebi.belns'
CELL_LINE_KEYWORD = 'CellLine'
CELL_LINE_URL = OPENBEL_ANNOTATION_RESOURCES + 'cell-line.belanno'
HGNC_KEYWORD = 'HGNC'
HGNC_URL = OPENBEL_NAMESPACE_RESOURCES + 'hgnc-human-genes.belns'
MESH_DISEASES_KEYWORD = 'MeSHDisease'
MESH_DISEASES_URL = OPENBEL_ANNOTATION_RESOURCES + "mesh-diseases.belanno"

pizza_iri = 'http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl'
wine_iri = 'http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine'

AKT1 = (PROTEIN, 'HGNC', 'AKT1')
EGFR = (PROTEIN, 'HGNC', 'EGFR')
FADD = (PROTEIN, 'HGNC', 'FADD')
CASP8 = (PROTEIN, 'HGNC', 'CASP8')


def update_provenance(bel_parser):
    """Sticks provenance in a BEL parser
    
    :param pybel.parser.parse_bel.BelParser bel_parser: 
    :return: 
    """
    bel_parser.control_parser.citation.update(test_citation_dict)
    bel_parser.control_parser.evidence = test_evidence_text


def any_dict_matches(dict_of_dicts, query_dict):
    return any(query_dict == sd for sd in dict_of_dicts.values())


def assertHasNode(self, node, graph, **kwargs):
    """A helper function for checking if a node with the given properties is contained within a graph

    :param self: A Test Case
    :type self: unittest.TestCase
    :param node: 
    :param graph:
    :type graph: BELGraph
    :param kwargs:
    """
    self.assertTrue(graph.has_node(node), msg='{} not found in graph'.format(node))
    if kwargs:
        missing = set(kwargs) - set(graph.node[node])
        self.assertFalse(missing, msg="Missing {} in node data".format(', '.join(sorted(missing))))
        self.assertTrue(all(kwarg in graph.node[node] for kwarg in kwargs),
                        msg="Missing kwarg in node data")
        self.assertEqual(kwargs, {k: graph.node[node][k] for k in kwargs},
                         msg="Wrong values in node data")


def assertHasEdge(self, u, v, graph, permissive=True, **kwargs):
    """A helper function for checking if an edge with the given properties is contained within a graph

    :param self: A TestCase
    :type self: unittest.TestCase
    :param u: source node
    :type u: tuple
    :param v: target node
    :type v: tuple
    :param graph: underlying graph
    :type graph: BELGraph
    :param kwargs: splat the data to match
    """
    self.assertTrue(graph.has_edge(u, v), msg='Edge ({}, {}) not in graph'.format(u, v))

    if not kwargs:
        return

    if permissive:
        matches = any_subdict_matches(graph.edge[u][v], kwargs)
    else:
        matches = any_dict_matches(graph.edge[u][v], kwargs)

    msg = 'No edge ({}, {}) with correct properties. expected:\n {}\nbut got:\n{}'.format(
        u,
        v,
        dumps(kwargs, indent=2, sort_keys=True),
        dumps(graph.edge[u][v], indent=2, sort_keys=True)
    )
    self.assertTrue(matches, msg=msg)


def identifier(namespace, name):
    return {NAMESPACE: namespace, NAME: name}


def default_identifier(name):
    """Convenience function for building a default namespace/name pair"""
    return identifier(BEL_DEFAULT_NAMESPACE, name)


class TestGraphMixin(unittest.TestCase):
    def assertHasNode(self, g, n, **kwargs):
        """Helper for asserting node membership
        
        :param g: Graph 
        :param n: Node
        :param kwargs: 
        """
        assertHasNode(self, n, g, **kwargs)

    def assertHasEdge(self, g, u, v, **kwargs):
        """Helper for asserting edge membership
        
        :param g: Graph
        :param u: Source node
        :param v: Target node
        :param kwargs: 
        """
        assertHasEdge(self, u, v, g, **kwargs)


class TemporaryCacheMixin(unittest.TestCase):
    def setUp(self):
        self.fd, self.path = tempfile.mkstemp()
        self.connection = 'sqlite:///' + self.path
        log.info('Test generated connection string %s', self.connection)

        self.manager = CacheManager(connection=self.connection)
        self.manager.create_all()

    def tearDown(self):
        self.manager.session.close()
        if not self.test_connection:
            os.close(self.fd)
            os.remove(self.path)
<commit_msg>Remove other typo<commit_after># -*- coding: utf-8 -*-

import logging
import unittest
from json import dumps

import os
import tempfile
from requests.compat import urlparse

from pybel import BELGraph
from pybel.constants import *
from pybel.manager.cache import CacheManager
from pybel.parser.parse_bel import BelParser
from pybel.parser.parse_exceptions import *
from pybel.parser.utils import any_subdict_matches

log = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
owl_dir_path = os.path.join(dir_path, 'owl')
bel_dir_path = os.path.join(dir_path, 'bel')
belns_dir_path = os.path.join(dir_path, 'belns')
belanno_dir_path = os.path.join(dir_path, 'belanno')
beleq_dir_path = os.path.join(dir_path, 'beleq')

test_bel_simple = os.path.join(bel_dir_path, 'test_bel.bel')
test_bel_extensions = os.path.join(bel_dir_path, 'test_bel_owl_extension.bel')
test_bel_slushy = os.path.join(bel_dir_path, 'slushy.bel')
test_bel_thorough = os.path.join(bel_dir_path, 'thorough.bel')
test_bel_isolated = os.path.join(bel_dir_path, 'isolated.bel')
test_bel_misordered = os.path.join(bel_dir_path, 'misordered.bel')

test_owl_pizza = os.path.join(owl_dir_path, 'pizza_onto.owl')
test_owl_wine = os.path.join(owl_dir_path, 'wine.owl')
test_owl_ado = os.path.join(owl_dir_path, 'ado.owl')

test_an_1 = os.path.join(belanno_dir_path, 'test_an_1.belanno')

test_ns_1 = os.path.join(belns_dir_path, 'test_ns_1.belns')
test_ns_2 = os.path.join(belns_dir_path, 'test_ns_1_updated.belns')
test_ns_nocache = os.path.join(belns_dir_path, 'test_nocache.belns')

test_eq_1 = os.path.join(beleq_dir_path, 'disease-ontology.beleq')
test_eq_2 = os.path.join(beleq_dir_path, 'mesh-diseases.beleq')

test_citation_dict = {
    CITATION_TYPE: 'PubMed',
    CITATION_NAME: 'TestName',
    CITATION_REFERENCE: '1235813'
}
SET_CITATION_TEST = 'SET Citation = {{"{type}","{name}","{reference}"}}'.format(**test_citation_dict)
test_evidence_text = 'I read it on Twitter'
test_set_evidence = 'SET Evidence = "{}"'.format(test_evidence_text)

CHEBI_KEYWORD = 'CHEBI'
CHEBI_URL = OPENBEL_NAMESPACE_RESOURCES + 'chebi.belns'
CELL_LINE_KEYWORD = 'CellLine'
CELL_LINE_URL = OPENBEL_ANNOTATION_RESOURCES + 'cell-line.belanno'
HGNC_KEYWORD = 'HGNC'
HGNC_URL = OPENBEL_NAMESPACE_RESOURCES + 'hgnc-human-genes.belns'
MESH_DISEASES_KEYWORD = 'MeSHDisease'
MESH_DISEASES_URL = OPENBEL_ANNOTATION_RESOURCES + "mesh-diseases.belanno"

pizza_iri = 'http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl'
wine_iri = 'http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine'

AKT1 = (PROTEIN, 'HGNC', 'AKT1')
EGFR = (PROTEIN, 'HGNC', 'EGFR')
FADD = (PROTEIN, 'HGNC', 'FADD')
CASP8 = (PROTEIN, 'HGNC', 'CASP8')


def update_provenance(bel_parser):
    """Sticks provenance in a BEL parser
    
    :param pybel.parser.parse_bel.BelParser bel_parser: 
    :return: 
    """
    bel_parser.control_parser.citation.update(test_citation_dict)
    bel_parser.control_parser.evidence = test_evidence_text


def any_dict_matches(dict_of_dicts, query_dict):
    return any(query_dict == sd for sd in dict_of_dicts.values())


def assertHasNode(self, node, graph, **kwargs):
    """A helper function for checking if a node with the given properties is contained within a graph

    :param self: A Test Case
    :type self: unittest.TestCase
    :param node: 
    :param graph:
    :type graph: BELGraph
    :param kwargs:
    """
    self.assertTrue(graph.has_node(node), msg='{} not found in graph'.format(node))
    if kwargs:
        missing = set(kwargs) - set(graph.node[node])
        self.assertFalse(missing, msg="Missing {} in node data".format(', '.join(sorted(missing))))
        self.assertTrue(all(kwarg in graph.node[node] for kwarg in kwargs),
                        msg="Missing kwarg in node data")
        self.assertEqual(kwargs, {k: graph.node[node][k] for k in kwargs},
                         msg="Wrong values in node data")


def assertHasEdge(self, u, v, graph, permissive=True, **kwargs):
    """A helper function for checking if an edge with the given properties is contained within a graph

    :param self: A TestCase
    :type self: unittest.TestCase
    :param u: source node
    :type u: tuple
    :param v: target node
    :type v: tuple
    :param graph: underlying graph
    :type graph: BELGraph
    :param kwargs: splat the data to match
    """
    self.assertTrue(graph.has_edge(u, v), msg='Edge ({}, {}) not in graph'.format(u, v))

    if not kwargs:
        return

    if permissive:
        matches = any_subdict_matches(graph.edge[u][v], kwargs)
    else:
        matches = any_dict_matches(graph.edge[u][v], kwargs)

    msg = 'No edge ({}, {}) with correct properties. expected:\n {}\nbut got:\n{}'.format(
        u,
        v,
        dumps(kwargs, indent=2, sort_keys=True),
        dumps(graph.edge[u][v], indent=2, sort_keys=True)
    )
    self.assertTrue(matches, msg=msg)


def identifier(namespace, name):
    return {NAMESPACE: namespace, NAME: name}


def default_identifier(name):
    """Convenience function for building a default namespace/name pair"""
    return identifier(BEL_DEFAULT_NAMESPACE, name)


class TestGraphMixin(unittest.TestCase):
    def assertHasNode(self, g, n, **kwargs):
        """Helper for asserting node membership
        
        :param g: Graph 
        :param n: Node
        :param kwargs: 
        """
        assertHasNode(self, n, g, **kwargs)

    def assertHasEdge(self, g, u, v, **kwargs):
        """Helper for asserting edge membership
        
        :param g: Graph
        :param u: Source node
        :param v: Target node
        :param kwargs: 
        """
        assertHasEdge(self, u, v, g, **kwargs)


class TemporaryCacheMixin(unittest.TestCase):
    def setUp(self):
        self.fd, self.path = tempfile.mkstemp()
        self.connection = 'sqlite:///' + self.path
        log.info('Test generated connection string %s', self.connection)

        self.manager = CacheManager(connection=self.connection)
        self.manager.create_all()

    def tearDown(self):
        self.manager.session.close()
        os.close(self.fd)
        os.remove(self.path)

{'fraction_non_alphanumeric': 0.1251923232650417, 'fraction_numerical': 0.005668475180176533, 'mean_word_length': 3.8641197321780227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '764391', 'n_tokens_mistral': 4482, 'n_tokens_neox': 4287, 'n_words': 939}