import unittest import tempfile from os.path import join from logFileModel import LogFileModel from line import Line class TestLogFileModel(unittest.TestCase): def setUp(self): self.test_dir = tempfile.TemporaryDirectory() self.tmpfile = join(self.test_dir.name, "my.log") self.model = LogFileModel(self.tmpfile) def tearDown(self): self.test_dir.cleanup() def write_str(self, string: str): with open(self.tmpfile, "w+b") as f: f.write(string.encode("utf8")) def test_load_from_beginning(self): self.write_str("1\n2\n3\n4\n5\n6\n7\n") expected_lines = ["1", "2", "3", "4", "5"] lines = self.model.data(0, 0, 5) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_load_from_middle_of_first_line(self): self.write_str("abc\ndef\nghi\njkl") expected_lines = ["def", "ghi", "jkl"] lines = self.model.data(1, 0, 3) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_read_from_newline_character(self): self.write_str("abc\ndef\nghi\njkl") expected_lines = ["def", "ghi"] lines = self.model.data(3, 0, 2) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_negative_byte_offset(self): self.write_str("abc\ndef\nghi\njkl") expected_lines = ["abc","def"] lines = self.model.data(-1, 0, 2) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_empty_last_line_is_ignored(self): self.write_str("1\n") expected_lines = ["1"] lines = self.model.data(0, 0, 5) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_load_more_lines_than_are_available(self): """ Wants to read 4 lines in a file with only 3 lines. Returns all three lines. """ self.write_str("abc\ndef\nghi") expected_lines = ["abc", "def", "ghi"] lines = self.model.data(0, 0, 4) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_read_behind_eof(self): """ Wants to read 4 lines from the middle of the second line. File has only 3 lines. Returns all 3 lines. """ text = "abc\ndef\nghi" self.write_str(text) expected_lines = ["abc", "def", "ghi"] lines = self.model.data(text.index("e"), 0, 4) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_read_after_scrolling_2_lines(self): """ """ text = "0___\n1___\n2___\n3___\n4___\n5___" self.write_str(text) expected_lines = ["3___", "4___"] lines = self.model.data(text.index("1___"), 2, 2) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_scroll_with_float(self): """ If lines to lines to return is a float, then the value is rounded up. Floats mean that the text area is such that a line is partially visible. """ text = "0___\n1___\n2___\n3___\n4___\n5___" self.write_str(text) expected_lines = ["3___","4___", "5___"] lines = self.model.data(text.index("1___"), 2, 2.1) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) def test_scroll_up_at_beginning_of_file(self): """ Scrolling up at beginning of file. Return """ text = "0___\n1___\n2___\n3___\n4___\n5___" self.write_str(text) expected_lines = ["0___", "1___"] lines = self.model.data(5, -2, 2) line_str = [l.line() for l in lines] self.assertEqual(expected_lines, line_str) if __name__ == '__main__': unittest.main()