<commit_before>from unittest.mock import MagicMock, call
from functools import partial

from chainsmoke.loggable import log_it
from chainsmoke import chain


def simple_addition(x, y):
    return x + y


def simple_multiplication(x, y):
    return x * y


# set up a partial function with a name
add_two = partial(simple_addition, 2)
add_two.__name__ = 'add_two'

# and a partial function without
multiply_by_two = partial(simple_multiplication, 2)


def test_that_log_it_logs_inputi_and_output_correctly():
    mock_logger = MagicMock()
    result = chain(
        5,
        add_two,
        multiply_by_two,
        wrap_with=log_it(mock_logger)
    )

    mock_logger.assert_has_calls(
        [
            call('add_two called with args: (5,) and kwargs {}'),
            call('add_two returned result 7'),
            call('unknown function name; probably a lambda or partially applied function... '
                 'called with args: (7,) and kwargs {}'),
            call('unknown function name; probably a lambda or partially applied function... returned result 14')
        ])

<commit_msg>Added test for optional input and output strings in log_it.<commit_after>from unittest.mock import MagicMock, call
from functools import partial

from chainsmoke.loggable import log_it
from chainsmoke import chain


def simple_addition(x, y):
    return x + y


def simple_multiplication(x, y):
    return x * y


# set up a partial function with a name
add_two = partial(simple_addition, 2)
add_two.__name__ = 'add_two'

# and a partial function without
multiply_by_two = partial(simple_multiplication, 2)


def test_that_log_it_logs_inputi_and_output_correctly():
    mock_logger = MagicMock()
    result = chain(
        5,
        add_two,
        multiply_by_two,
        wrap_with=log_it(mock_logger)
    )

    mock_logger.assert_has_calls(
        [
            call('add_two called with args: (5,) and kwargs {}'),
            call('add_two returned result 7'),
            call('unknown function name; probably a lambda or partially applied function... '
                 'called with args: (7,) and kwargs {}'),
            call('unknown function name; probably a lambda or partially applied function... returned result 14')
        ])


def test_that_input_and_output_string_can_be_changed():
    mock_logger = MagicMock()
    result = chain(
        5,
        add_two,
        multiply_by_two,
        wrap_with=log_it(mock_logger,
                         input_string="{func_name}; {args}; {kwargs}",
                         output_string='{func_name}; {result}')
    )

    mock_logger.assert_has_calls(
        [
            call('add_two; (5,); {}'),
            call('add_two; 7'),
            call('unknown function name; probably a lambda or partially applied function...; (7,); {}'),
            call('unknown function name; probably a lambda or partially applied function...; 14')
        ])


{'fraction_non_alphanumeric': 0.09143835616438356, 'fraction_numerical': 0.007534246575342466, 'mean_word_length': 2.735294117647059, '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': '1742776', 'n_tokens_mistral': 869, 'n_tokens_neox': 838, 'n_words': 253}