def test_return_line_contract_violation(self): """BufferWorkSpace (of a completely empty file) would result in contract violation for return_line.""" with patch("file_read_backwards.buffer_work_space._get_file_size" ) as _get_file_size_mock: fp_mock = Mock() _get_file_size_mock.return_value = 0 b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) with self.assertRaises(AssertionError): b.return_line()
def test_return_line_with_buffer_space_with_two_new_lines(self): """With two new lines as its sole contents, the buffer space is expected to return b''.""" with patch("file_read_backwards.buffer_work_space._get_file_size" ) as _get_file_size_mock: fp_mock = Mock() _get_file_size_mock.return_value = 1024 for n in new_lines_bytes: b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) b.read_position = 1024 - (len(n) * 2) b.read_buffer = n * 2 expected_result = b"" r = b.return_line() self.assertEqual(r, expected_result)
def test_return_line_with_buffer_space_with_fully_read_in_contents_at_its_last_line( self): """With some bytestrings in between 2 new lines, expect to have the bytestrings in the middle.""" with patch("file_read_backwards.buffer_work_space._get_file_size" ) as _get_file_size_mock: fp_mock = Mock() _get_file_size_mock.return_value = 1024 for n in new_lines_bytes: b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) b.read_position = 0 b.read_buffer = b"LastLineYay" expected_result = b"LastLineYay" r = b.return_line() self.assertEqual(r, expected_result)
def test_return_line_with_buffer_space_with_some_contents_between_two_new_lines( self): """With some bytestrings in between 2 new lines, expect to have the bytestrings in the middle.""" with patch("file_read_backwards.buffer_work_space._get_file_size" ) as _get_file_size_mock: fp_mock = Mock() _get_file_size_mock.return_value = 1024 for n in new_lines_bytes: b = BufferWorkSpace(fp_mock, chunk_size=io.DEFAULT_BUFFER_SIZE) b.read_position = 1024 - (len(n) * 2) b.read_buffer = n + b"Something" + n expected_result = b"Something" r = b.return_line() self.assertEqual(r, expected_result)