def test_navigate_12_lines_file(self): with BigFileHandler("test_12_lines_file.txt") as bfh: lines_1 = bfh.get_lines() self.assertEqual("line 1", lines_1[1]) self.assertEqual("line 11", lines_1[11]) bfh.down() lines_2 = bfh.get_lines() self.assertEqual("line 2", lines_2[2]) self.assertEqual("line 12", lines_2[12]) bfh.page_down() lines_3 = bfh.get_lines() self.assertEqual("line 2", lines_3[2]) self.assertEqual("line 12", lines_3[12]) bfh.page_up() lines_4 = bfh.get_lines() self.assertEqual("line 1", lines_4[1]) self.assertEqual("line 11", lines_4[11]) bfh.goto(2) lines_5 = bfh.get_lines() self.assertEqual("line 2", lines_5[2]) self.assertEqual("line 12", lines_5[12]) bfh.up() lines_6 = bfh.get_lines() self.assertEqual("line 1", lines_6[1]) self.assertEqual("line 11", lines_6[11])
def test_up__second_line__current_line_equals_one(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: bfh.goto(2) bfh.up() lines = bfh.get_lines() self.assertEqual(1, bfh.get_current_line()) self.assertEqual("line 3", lines[3])
def test_navigate_last_lines(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: bfh.goto(995) lines = bfh.get_lines() self.assertEqual("line 1000", lines[1000]) self.assertEqual("line 990", lines[990]) self.assertEqual(11, len(lines))
def test_goto__line_100__validate_buffer(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: bfh.goto(100) bfh.get_lines() self.assertIsNone(bfh.get_total_lines()) self.assertEqual(51, bfh.get_buffer_min_value()) self.assertEqual(150, bfh.get_buffer_max_value())
def test_goto__line_999__validate_buffer(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: bfh.goto(999) lines = bfh.get_lines() self.assertEqual(1000, bfh.get_total_lines()) self.assertEqual(1000, bfh.get_buffer_max_value()) self.assertEqual(950, bfh.get_buffer_min_value()) self.assertEqual("line 999", lines[999])
def test_navigate_1_line_file(self): with BigFileHandler("test_1_line_file.txt") as bfh: lines_1 = bfh.get_lines() self.assertEqual(1, len(lines_1)) bfh.down() lines_2 = bfh.get_lines() self.assertEqual(1, len(lines_2)) bfh.page_down() lines_3 = bfh.get_lines() self.assertEqual(1, len(lines_3)) bfh.page_down() lines_4 = bfh.get_lines() self.assertEqual(1, len(lines_4)) bfh.goto(2) lines_5 = bfh.get_lines() self.assertEqual(1, len(lines_5))
def test_buffer_reads(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: lines_1 = bfh.get_lines() self.assertEqual("line 1", lines_1[1]) self.assertEqual("line 11", lines_1[11]) bfh.page_down() # 12 position bfh.page_down() # 23 position bfh.page_down() # 34 position bfh.page_down() # 45 position bfh.page_down() # 56 position bfh.page_down() # 67 position lines_2 = bfh.get_lines() self.assertEqual("line 67", lines_2[67]) self.assertEqual("line 77", lines_2[77]) bfh.goto(659) lines_3 = bfh.get_lines() self.assertEqual("line 659", lines_3[659]) self.assertEqual("line 669", lines_3[669]) self.assertEqual(3, bfh.get_buffer_reads())
def test_navigate_5_lines_file(self): with BigFileHandler("test_5_lines_file.txt") as bfh: lines_1 = bfh.get_lines() self.assertEqual("line 1", lines_1[1]) self.assertEqual("line 5", lines_1[5]) self.assertEqual(5, len(lines_1)) bfh.down() lines_2 = bfh.get_lines() self.assertEqual("line 1", lines_2[1]) self.assertEqual("line 5", lines_2[5]) self.assertEqual(5, len(lines_2)) bfh.page_down() lines_3 = bfh.get_lines() self.assertEqual("line 1", lines_3[1]) self.assertEqual("line 5", lines_3[5]) self.assertEqual(5, len(lines_3)) bfh.page_up() lines_4 = bfh.get_lines() self.assertEqual("line 1", lines_4[1]) self.assertEqual("line 5", lines_4[5]) self.assertEqual(5, len(lines_4)) bfh.goto(2) lines_5 = bfh.get_lines() self.assertEqual("line 1", lines_5[1]) self.assertEqual("line 5", lines_5[5]) self.assertEqual(5, len(lines_5)) bfh.up() lines_6 = bfh.get_lines() self.assertEqual("line 1", lines_6[1]) self.assertEqual("line 5", lines_6[5]) self.assertEqual(5, len(lines_6))
def test_init__invalid_file(self): with self.assertRaises(BigFileVisualizerFatalError): BigFileHandler("non_existent_file.txt")
def test_init__empty_file(self): with self.assertRaises(BigFileVisualizerFatalError): BigFileHandler("empty_file.txt")
def test_init__valid_file(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: self.assertEqual("test_1000_lines_file.txt", bfh.get_file())
def test_goto__negative_line(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: bfh.goto(-1) bfh.get_lines() self.assertEqual(1, bfh.get_current_line())
def test_up__bof__current_line_equals_one(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: self.assertEqual(1, bfh.get_current_line()) bfh.up() self.assertEqual(1, bfh.get_current_line())
def test_get__next_lines(self): with BigFileHandler("test_1000_lines_file.txt") as bfh: lines = bfh.get_lines() for i in range(1, 11): self.assertEqual(f"line {i}", lines[i])
if __name__ == '__main__': parser = argparse.ArgumentParser( description='Bigfile line visualizer tool') parser.add_argument('--file', '-f', dest='file', help='file to be opened', required=True) args = parser.parse_args() try: with BigFileHandler(args.file) as bfh: print("***big file visualizer***") print_lines() while True: command, line = read_command() if command == "up": bfh.up() elif command == "down": bfh.down() elif command == "page_up": bfh.page_up() elif command == "page_down": bfh.page_down() elif command == "goto":