def setUp(self): self.matrix = [ [4, 1, 7, 3, 6, 9, 8, 2, 5], [6, 3, 2, 1, 5, 8, 9, 4, 7], [9, 5, 8, 7, 2, 4, 3, 1, 6], [8, 2, 5, 4, 3, 7, 1, 6, 9], [7, 9, 1, 5, 8, 6, 4, 3, 2], [3, 4, 6, 9, 1, 2, 7, 5, 8], [2, 8, 9, 6, 4, 3, 5, 7, 1], [5, 7, 3, 2, 9, 1, 6, 8, 4], [1, 6, 4, 8, 7, 5, 2, 9, 3] ] self.txt_content_expected = [ "417369825\n", "632158947\n", "958724316\n", "825437169\n", "791586432\n", "346912758\n", "289643571\n", "573291684\n", "164875293\n" ] self.file_actual = "export_actual.txt" self.file_expected = "export_expected.txt" with open(self.file_expected, 'w') as rawfile: for row in self.txt_content_expected: rawfile.write(row) self.content_sudoku_import = [ "400000805\n", "030000000\n", "000700000\n", "020000060\n", "000080400\n", "000010000\n", "000603070\n", "500200000\n", "104000000\n" ] self.file_sudoku_import = "sudoku_import.txt" with open(self.file_sudoku_import, 'w') as rawfile: for row in self.content_sudoku_import: rawfile.write(row) self.file_import = FileHandlerTXT(self.file_sudoku_import, 'r')
class TestFileHandlerTXT(unittest.TestCase): def setUp(self): self.matrix = [ [4, 1, 7, 3, 6, 9, 8, 2, 5], [6, 3, 2, 1, 5, 8, 9, 4, 7], [9, 5, 8, 7, 2, 4, 3, 1, 6], [8, 2, 5, 4, 3, 7, 1, 6, 9], [7, 9, 1, 5, 8, 6, 4, 3, 2], [3, 4, 6, 9, 1, 2, 7, 5, 8], [2, 8, 9, 6, 4, 3, 5, 7, 1], [5, 7, 3, 2, 9, 1, 6, 8, 4], [1, 6, 4, 8, 7, 5, 2, 9, 3] ] self.txt_content_expected = [ "417369825\n", "632158947\n", "958724316\n", "825437169\n", "791586432\n", "346912758\n", "289643571\n", "573291684\n", "164875293\n" ] self.file_actual = "export_actual.txt" self.file_expected = "export_expected.txt" with open(self.file_expected, 'w') as rawfile: for row in self.txt_content_expected: rawfile.write(row) self.content_sudoku_import = [ "400000805\n", "030000000\n", "000700000\n", "020000060\n", "000080400\n", "000010000\n", "000603070\n", "500200000\n", "104000000\n" ] self.file_sudoku_import = "sudoku_import.txt" with open(self.file_sudoku_import, 'w') as rawfile: for row in self.content_sudoku_import: rawfile.write(row) self.file_import = FileHandlerTXT(self.file_sudoku_import, 'r') def tearDown(self): self.file_import.file.close() try: remove(self.file_expected) except: pass try: remove(self.file_actual) except: pass try: remove(self.file_sudoku_import) except: pass def test_export_matrix_to_txt(self): txthandler = FileHandlerTXT(self.file_actual, 'w') txthandler.export_file(self.matrix) with open(self.file_actual) as file: txt_content_actual = file.readlines() self.assertEqual(self.txt_content_expected, txt_content_actual) def test_export_txt_and_close(self): txthandler = FileHandlerTXT(self.file_actual, 'w') txthandler.export_file(self.matrix) self.assertTrue(txthandler.file.closed) def test_exporting_to_txt_file_in_read_mode(self): txthandler = FileHandlerTXT(self.file_expected) self.assertRaises(IOError, txthandler.export_file, self.matrix) def test_file_to_import_is_not_empty(self): notemptyfile = self.file_import.not_empty() self.assertEqual(True, notemptyfile) def test_file_imported_will_create_a_list_with_9_elements(self): listlenght = len(self.file_import.import_file()) self.assertEqual(9, listlenght) def test_file_import_will_creat_a_9x9_matrix(self): matrix_imported =self.file_import.import_file() matrix_expected = [[4,0,0,0,0,0,8,0,5], [0,3,0,0,0,0,0,0,0], [0,0,0,7,0,0,0,0,0], [0,2,0,0,0,0,0,6,0], [0,0,0,0,8,0,4,0,0], [0,0,0,0,1,0,0,0,0], [0,0,0,6,0,3,0,7,0], [5,0,0,2,0,0,0,0,0], [1,0,4,0,0,0,0,0,0]] self.assertEqual(matrix_imported, matrix_expected)
def test_export_txt_and_close(self): txthandler = FileHandlerTXT(self.file_actual, 'w') txthandler.export_file(self.matrix) self.assertTrue(txthandler.file.closed)
def test_export_matrix_to_txt(self): txthandler = FileHandlerTXT(self.file_actual, 'w') txthandler.export_file(self.matrix) with open(self.file_actual) as file: txt_content_actual = file.readlines() self.assertEqual(self.txt_content_expected, txt_content_actual)