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

import logging
import unittest

import pybel
from pybel.canonicalize import postpend_location, decanonicalize_node
from pybel.constants import GOCC_LATEST, FUNCTION, GOCC_KEYWORD
from tests.constants import test_bel_simple, mock_bel_resources, test_bel_thorough, BelReconstitutionMixin, \
    test_bel_isolated

log = logging.getLogger('pybel')
logging.getLogger('pybel.parser.modifiers.truncation').setLevel(50)


class TestCanonicalizeHelper(unittest.TestCase):
    def test_postpend_location_failure(self):
        with self.assertRaises(ValueError):
            postpend_location('', dict(name='failure'))

    def test_decanonicalize_node_failure(self):
        with self.assertRaises(ValueError):
            class NotGraph:
                node = None

            x = NotGraph()

            test_node = ('nope', 'nope', 'nope')
            x.node = {test_node: {FUNCTION: 'nope'}}
            decanonicalize_node(x, test_node)


class TestCanonicalize(BelReconstitutionMixin, unittest.TestCase):
    def canonicalize_helper(self, test_path, reconstituted, **kwargs):
        original = pybel.from_path(test_path, **kwargs)

        reconstituted(original)

        original_lines = pybel.to_bel_lines(original)
        reloaded = pybel.from_lines(original_lines)

        original.namespace_url[GOCC_KEYWORD] = GOCC_LATEST

        self.assertEqual(original.document, reloaded.document)
        self.assertEqual(original.namespace_owl, reloaded.namespace_owl)
        self.assertEqual(original.namespace_url, reloaded.namespace_url)
        self.assertEqual(original.namespace_pattern, reloaded.namespace_pattern)
        self.assertEqual(original.annotation_url, reloaded.annotation_url)
        self.assertEqual(original.annotation_owl, reloaded.annotation_owl)
        self.assertEqual(original.annotation_list, reloaded.annotation_list)

        self.assertEqual(set(original.nodes()), set(reloaded.nodes()))
        self.assertEqual(set(original.edges()), set(reloaded.edges()))

        reconstituted(reloaded)

    @mock_bel_resources
    def test_simple(self, mock_get):
        self.canonicalize_helper(test_bel_simple, reconstituted=self.bel_simple_reconstituted)

    # @mock_bel_resources
    # @mock_parse_owl_rdf
    # @mock_parse_owl_pybel
    # def test_canonicalize_4(self, m1, m2, m3):
    #    self.canonicalize_helper(test_bel_extensions)

    @mock_bel_resources
    def test_thorough(self, mock_get):
        self.canonicalize_helper(test_bel_thorough, reconstituted=self.bel_thorough_reconstituted, allow_nested=True)

    @mock_bel_resources
    def test_isolated(self, mock_get):
        self.canonicalize_helper(test_bel_isolated, reconstituted=self.bel_isolated_reconstituted)<commit_msg>Refactor test<commit_after># -*- coding: utf-8 -*-

import logging
import unittest

import pybel
from pybel.canonicalize import postpend_location, decanonicalize_node
from pybel.constants import GOCC_LATEST, FUNCTION, GOCC_KEYWORD
from tests.constants import test_bel_simple, mock_bel_resources, test_bel_thorough, BelReconstitutionMixin, \
    test_bel_isolated

log = logging.getLogger('pybel')
logging.getLogger('pybel.parser.modifiers.truncation').setLevel(50)


class TestCanonicalizeHelper(unittest.TestCase):
    def test_postpend_location_failure(self):
        with self.assertRaises(ValueError):
            postpend_location('', dict(name='failure'))

    def test_decanonicalize_node_failure(self):
        class NotGraph:
            node = None

        x = NotGraph()

        test_node = ('nope', 'nope', 'nope')
        x.node = {test_node: {FUNCTION: 'nope'}}

        with self.assertRaises(ValueError):
            decanonicalize_node(x, test_node)


class TestCanonicalize(BelReconstitutionMixin, unittest.TestCase):
    def canonicalize_helper(self, test_path, reconstituted, **kwargs):
        original = pybel.from_path(test_path, **kwargs)

        reconstituted(original)

        original_lines = pybel.to_bel_lines(original)
        reloaded = pybel.from_lines(original_lines)

        original.namespace_url[GOCC_KEYWORD] = GOCC_LATEST

        self.assertEqual(original.document, reloaded.document)
        self.assertEqual(original.namespace_owl, reloaded.namespace_owl)
        self.assertEqual(original.namespace_url, reloaded.namespace_url)
        self.assertEqual(original.namespace_pattern, reloaded.namespace_pattern)
        self.assertEqual(original.annotation_url, reloaded.annotation_url)
        self.assertEqual(original.annotation_owl, reloaded.annotation_owl)
        self.assertEqual(original.annotation_list, reloaded.annotation_list)

        self.assertEqual(set(original.nodes()), set(reloaded.nodes()))
        self.assertEqual(set(original.edges()), set(reloaded.edges()))

        reconstituted(reloaded)

    @mock_bel_resources
    def test_simple(self, mock_get):
        self.canonicalize_helper(test_bel_simple, reconstituted=self.bel_simple_reconstituted)

    @mock_bel_resources
    def test_thorough(self, mock_get):
        self.canonicalize_helper(test_bel_thorough, reconstituted=self.bel_thorough_reconstituted, allow_nested=True)

    @mock_bel_resources
    def test_isolated(self, mock_get):
        self.canonicalize_helper(test_bel_isolated, reconstituted=self.bel_isolated_reconstituted)
{'fraction_non_alphanumeric': 0.09259954921111946, 'fraction_numerical': 0.0018782870022539444, 'mean_word_length': 4.31437125748503, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1420806', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1575, 'n_words': 239}