<commit_before># coding=utf-8
# Filename: test_config.py
"""
Test suite for configuration related functions and classes.

"""
from __future__ import division, absolute_import, print_function

from km3pipe.testing import TestCase, StringIO, patch
from km3pipe.config import Config

__author__ = "<NAME>"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"


orig_import = __import__
try:
    __builtin__
except NameError:
    builtins_name = "builtins"
else:
    builtins_name = "__builtin__"

def import_mock(name, *args):
    if name == 'irods.session.iRODSSession':
        return "narf"
    return orig_import(name, *args)


CONFIGURATION = StringIO("\n".join((
    "[DB]",
    "username=foo",
    "password=<PASSWORD>",
    "timeout=10",
    )))

IRODS_CONFIGURATION = StringIO("\n".join((
    "[iRODS]",
    "user=foo",
    "password=<PASSWORD>",
    "host=what.ever",
    "port=1234",
    "zone=area51",
    )))

class TestConfig(TestCase):
    def setUp(self):
        self.config = Config(None)
        self.config._read_from_file(CONFIGURATION)
        CONFIGURATION.seek(0, 0)

    def test_db_credentials(self):
        self.assertEqual('foo', self.config.db_credentials[0])
        self.assertEqual('narf', self.config.db_credentials[1])

    def test_check_for_updates_defaults_to_true(self):
        self.assertTrue(self.config.check_for_updates)

    def test_time_zone_defaults_to_utc(self):
        self.assertEqual('UTC', self.config.time_zone._tzname)

    def test_slack_token_raises_error_by_default(self):
        with self.assertRaises(ValueError):
            self.config.slack_token

    def test_get_retrieves_correct_value(self):
        self.assertEqual("foo", self.config.get("DB", "username"))

    def test_get_returns_none_if_section_not_found(self):
        self.assertTrue(self.config.get("a", "b") is None)

    def test_get_returns_none_if_option_not_found(self):
        self.assertTrue(self.config.get("DB", "a") is None)

    def test_get_returns_float_if_option_is_numberlike(self):
        self.assertTrue(isinstance(self.config.get("DB", "timeout"), float))

    def test_create_irods_session_returns_none_if_irods_module_missing(self):
        session = self.config.create_irods_session()
        self.assertTrue(session is None)

#    TODO: this is a big complicated ;)
    @patch(builtins_name + '.__import__', side_effect=import_mock)
    def test_irods_session(self, mock_irods_session):
        session = self.config.create_irods_session()
#        self.assertTrue(import_mock.call_args())

#    TODO: this is a big complicated too ;)
    @patch(builtins_name + '.__import__', side_effect=import_mock)
    def test_irods_session_with_valid_config(self, mock_irods_session):
        config = Config(None)
        config._read_from_file(IRODS_CONFIGURATION)
        IRODS_CONFIGURATION.seek(0, 0)
        session = config.create_irods_session()
        self.assertTrue(session is None)
<commit_msg>Remove tests due to mock incompatibility with python27<commit_after># coding=utf-8
# Filename: test_config.py
"""
Test suite for configuration related functions and classes.

"""
from __future__ import division, absolute_import, print_function

from km3pipe.testing import TestCase, StringIO, patch
from km3pipe.config import Config

__author__ = "<NAME>"
__copyright__ = "Copyright 2016, <NAME> and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"


CONFIGURATION = StringIO("\n".join((
    "[DB]",
    "username=foo",
    "password=<PASSWORD>",
    "timeout=10",
    )))


class TestConfig(TestCase):
    def setUp(self):
        self.config = Config(None)
        self.config._read_from_file(CONFIGURATION)
        CONFIGURATION.seek(0, 0)

    def test_db_credentials(self):
        self.assertEqual('foo', self.config.db_credentials[0])
        self.assertEqual('narf', self.config.db_credentials[1])

    def test_check_for_updates_defaults_to_true(self):
        self.assertTrue(self.config.check_for_updates)

    def test_time_zone_defaults_to_utc(self):
        self.assertEqual('UTC', self.config.time_zone._tzname)

    def test_slack_token_raises_error_by_default(self):
        with self.assertRaises(ValueError):
            self.config.slack_token

    def test_get_retrieves_correct_value(self):
        self.assertEqual("foo", self.config.get("DB", "username"))

    def test_get_returns_none_if_section_not_found(self):
        self.assertTrue(self.config.get("a", "b") is None)

    def test_get_returns_none_if_option_not_found(self):
        self.assertTrue(self.config.get("DB", "a") is None)

    def test_get_returns_float_if_option_is_numberlike(self):
        self.assertTrue(isinstance(self.config.get("DB", "timeout"), float))

    def test_create_irods_session_returns_none_if_irods_module_missing(self):
        session = self.config.create_irods_session()
        self.assertTrue(session is None)

{'fraction_non_alphanumeric': 0.1153619776339023, 'fraction_numerical': 0.007455365901510693, 'mean_word_length': 4.202040816326531, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2333920', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1662, 'n_words': 294}