<commit_before>"""
Tests for the tail implementation
"""

from tail import FileBasedTail

def test_tail_from_file():
    """Tests that tail works as advertised from a file"""

    from unittest.mock import mock_open, patch, Mock

    # The mock_data we are using for our test
    mock_data = """A
B
C
D
E
F
"""
    mocked_open = mock_open(read_data=mock_data)

    # mock_open does not support iteration by lines by default so
    # we must define the following:
    mocked_open.return_value.__iter__.return_value = mock_data.splitlines()


    # The file check in the class returns no value upon a valid file
    # the error states just raise exceptions.
    mocked_file_validity_check = Mock()

    # We need to patch the open found in the namespace of the module
    # where the function is defined
    with patch('builtins.open', mocked_open, create=True) as mocked_file_open:

        # We also need to patch the file checking because we are not dealing
        # with an actual file in the filesystem in this unit test
        with patch('tail.tail.check_file_validity', mocked_file_validity_check):
            res = FileBasedTail('Test_filename.txt').tail(3)

    mocked_file_validity_check.assert_called_once_with('Test_filename.txt')
    mocked_file_open.assert_called_once_with('Test_filename.txt', 'r')
    assert len(res) == 3
    assert res == ["D", "E", "F"]


def test_head_from_file():
    """Tests that tail works as advertised from a file"""

    from unittest.mock import mock_open, patch, Mock

    # The mock_data we are using for our test
    mock_data = """A
B
C
D
E
F
"""
    mocked_open = mock_open(read_data=mock_data)

    # mock_open does not support iteration by lines by default so
    # we must define the following:
    mocked_open.return_value.__iter__.return_value = mock_data.splitlines()


    # The file check in the class returns no value upon a valid file
    # the error states just raise exceptions.
    mocked_file_validity_check = Mock()

    # We need to patch the open found in the namespace of the module
    # where the function is defined
    with patch('builtins.open', mocked_open, create=True) as mocked_file_open:

        # We also need to patch the file checking because we are not dealing
        # with an actual file in the filesystem in this unit test
        with patch('tail.tail.check_file_validity', mocked_file_validity_check):
            res = FileBasedTail('Test_filename.txt').head(3)

    mocked_file_validity_check.assert_called_once_with('Test_filename.txt')
    mocked_file_open.assert_called_once_with('Test_filename.txt', 'r')
    assert len(res) == 3
    assert res == ["A", "B", "C"]<commit_msg>Give up on trying to mock a file open because tell isn't supported well in mock_open<commit_after>"""
Tests for the tail implementation
"""

from tail import FileBasedTail

def test_tail_from_file():
    """Test that head works as advertised.
    we create a temporary file because mocking the tell method in mock.mock_open
    doesn't work without substantial extra effort
    """

    import tempfile
    with tempfile.NamedTemporaryFile(mode='w') as fp:
        fp.write("""A
B
C
D
E
F
""")
        fp.seek(0)
        tail_obj = FileBasedTail(fp.name)
        res = tail_obj.tail(3)
    assert len(res) == 3
    assert res == ["D", "E", "F"]


def test_head_from_real_file():
    """Test that head works as advertised.
    we create a temporary file because mocking the tell method in mock.mock_open
    doesn't work without substantial extra effort
    """

    import tempfile
    with tempfile.NamedTemporaryFile(mode='w') as fp:
        fp.write("""A
B
C
D
E
F
""")
        fp.seek(0)
        tail_obj = FileBasedTail(fp.name)
        res = tail_obj.head(3)
    assert len(res) == 3
    assert res == ["A", "B", "C"]

{'fraction_non_alphanumeric': 0.08722002635046114, 'fraction_numerical': 0.002635046113306983, 'mean_word_length': 3.3582089552238807, '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': '203555', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1139, 'n_words': 408}