<commit_before>"""
Test base helpers.
"""

# Python std lib
import os
import sys
import unittest

# third party libs
try:
    import jinja2
    HAS_JINJA = True
except ImportError:
    HAS_JINJA = False

try:
    import textfsm
    HAS_TEXTFSM = True
except ImportError:
    HAS_TEXTFSM = False

try:
    from lxml import etree as ET
    HAS_LXML = True
except ImportError:
    HAS_LXML = False

# NAPALM base
import napalm_base.helpers
import napalm_base.exceptions
from napalm_base.base import NetworkDriver


class TestBaseHelpers(unittest.TestCase):
    """Test helpers functions."""

    @classmethod
    def setUpClass(cls):
        cls.network_driver = FakeNetworkDriver()
        # neded when calling helpers

    def test_load_template(self):

        """
        Tests the helper function ```load_template```:

            * check if raises TemplateNotImplemented when trying to use inexisting template
            * check if can load empty template
            * check if raises TemplateRenderException when template is not correctly formatted
            * check if can load correct template
            * check if can load correct template even if wrong custom path specified
        """

        self.assertTrue(HAS_JINJA)  # firstly check if jinja2 is installed

        _NTP_PEERS_LIST = [
            '172.17.17.1',
            '172.17.17.2'
        ]
        _TEMPLATE_VARS = {
            'peers': _NTP_PEERS_LIST
        }

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__empty_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(napalm_base.exceptions.TemplateRenderException,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__completely_wrong_template__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          **_TEMPLATE_VARS))

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          template_path='/this/path/does/not/exist',
                                                          **_TEMPLATE_VARS))

        install_dir = os.path.dirname(os.path.abspath(sys.modules[self.network_driver.__module__].__file__))
        custom_path = os.path.join(install_dir, 'custom/path/base')<commit_msg>Test also inexisting template from custom path<commit_after>"""
Test base helpers.
"""

# Python std lib
import os
import sys
import unittest

# third party libs
try:
    import jinja2
    HAS_JINJA = True
except ImportError:
    HAS_JINJA = False

try:
    import textfsm
    HAS_TEXTFSM = True
except ImportError:
    HAS_TEXTFSM = False

try:
    from lxml import etree as ET
    HAS_LXML = True
except ImportError:
    HAS_LXML = False

# NAPALM base
import napalm_base.helpers
import napalm_base.exceptions
from napalm_base.base import NetworkDriver


class TestBaseHelpers(unittest.TestCase):
    """Test helpers functions."""

    @classmethod
    def setUpClass(cls):
        cls.network_driver = FakeNetworkDriver()
        # neded when calling helpers

    def test_load_template(self):

        """
        Tests the helper function ```load_template```:

            * check if raises TemplateNotImplemented when trying to use inexisting template
            * check if can load empty template
            * check if raises TemplateRenderException when template is not correctly formatted
            * check if can load correct template
            * check if can load correct template even if wrong custom path specified
            * check if raises TemplateNotImplemented when trying to use inexisting template in custom path
            * check if can load correct template from custom path
        """

        self.assertTrue(HAS_JINJA)  # firstly check if jinja2 is installed

        _NTP_PEERS_LIST = [
            '172.17.17.1',
            '172.17.17.2'
        ]
        _TEMPLATE_VARS = {
            'peers': _NTP_PEERS_LIST
        }

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__empty_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(napalm_base.exceptions.TemplateRenderException,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__completely_wrong_template__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          **_TEMPLATE_VARS))

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          template_path='/this/path/does/not/exist',
                                                          **_TEMPLATE_VARS))

        install_dir = os.path.dirname(os.path.abspath(sys.modules[self.network_driver.__module__].__file__))
        custom_path = os.path.join(install_dir, 'test/custom/path/base')

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          template_path=custom_path,
                          **_TEMPLATE_VARS)
{'fraction_non_alphanumeric': 0.06397459165154265, 'fraction_numerical': 0.0054446460980036296, 'mean_word_length': 1.6169370795409577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6167656', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1656, 'n_words': 353}