def setUp(self): self.txt_handler = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.path_name = os.path.dirname(file) + "/" # create an file to test the existing file case my_file = open(self.path_name + 'test_exist_file.txt', 'w') my_file.write("te") my_file.close()
def setUp(self): self.txt_handler = TXTHandler() self.txt_file = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.pathname = os.path.dirname(file) # create an file to test the existing file case my_file = open(self.pathname + '/test_exist_file.txt', 'w') my_file.write("te") my_file.close() # Valid txt format 9x9 self.txt = open ('valid_sudoku_0.txt', 'rb') self.valid_txt = self.txt.read() # Invalid txt format 9x9 self.txt = open ('invalid_sudoku_0.txt', 'rb') self.invalid_txt = self.txt.read() self.size = len(self.valid_txt)
def setUp(self): self.txt_handler = TXTHandler() self.txt_file = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.pathname = os.path.dirname(file) # create an file to test the existing file case my_file = open(self.pathname + '/test_exist_file.txt', 'w') my_file.write("te") my_file.close() # Valid txt format 9x9 self.txt = open('valid_sudoku_0.txt', 'rb') self.valid_txt = self.txt.read() # Invalid txt format 9x9 self.txt = open('invalid_sudoku_0.txt', 'rb') self.invalid_txt = self.txt.read() self.size = len(self.valid_txt)
class TestTXTHandler(unittest.TestCase): def setUp(self): self.txt_handler = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.path_name = os.path.dirname(file) + "/" # create an file to test the existing file case my_file = open(self.path_name + 'test_exist_file.txt', 'w') my_file.write("te") my_file.close() def tearDown(self): os.remove(self.path_name + 'test_exist_file.txt') def test_the_name_file_increments_when_the_same_file_exist(self): file_name = self.txt_handler.get_file_name(self.path_name, 'test_exist_file.txt', 0) self.assertEquals('test_exist_file1.txt', file_name) def test_the_name_file_is_the_same_when_the_file_not_exist(self): file_name = self.txt_handler.get_file_name(self.path_name, 'test_not_exist_file.txt', 0) self.assertEquals('test_not_exist_file.txt', file_name) def test_write_sudoku_returns_true_file_has_been_write(self): sudoku_was_written = self.txt_handler.write_sudoku(self.path_name, 'test_not_exist_file.txt', "test") os.remove(self.path_name + 'test_not_exist_file.txt') self.assertTrue(sudoku_was_written) def test_write_sudoku_writes_the_specified_line(self): line_to_write = "test\n" + \ "this is a test" self.txt_handler.write_sudoku(self.path_name , 'test_write_specified_line.txt', line_to_write) file_written = open(self.path_name + 'test_write_specified_line.txt', "r") content = file_written.readlines() line_written = "" for line in content : line_written = line_written + line file_written.close() os.remove(self.path_name + 'test_write_specified_line.txt') self.assertEquals(line_written,line_to_write)
def __init__(self): """ Contructor class ExportSudoku class in charge to export sudokus data to differents outputs such as txt , cmdline """ # option value when the export must be in cmd line self.export_to_cmd_line = "cmd line" # option value when the export must be in cmd line self.export_to_txt = "txt" # handler to write to txt files self.txt_handler = TXTHandler()
def test_read_sudoku_for_invalid_txt_using_separator_dot(self): txt_file = TXTHandler() self.expected = '3.657..2\n' + \ '..913.76.\n' + \ '487...531\n' + \ '26..15.8.\n' + \ '...86..25\n' + \ '.5.7.2.4.\n' + \ '.389.725.\n' + \ '6.2..1.74\n' result = self.txt_file.get_sudoku_data('invalid_sudoku_dot.txt') self.assertEquals(self.expected, result[0][0])
class ExportSudoku: def __init__(self): """ Contructor class ExportSudoku class in charge to export sudokus data to differents outputs such as txt , cmdline """ # option value when the export must be in cmd line self.export_to_cmd_line = "cmd line" # option value when the export must be in cmd line self.export_to_txt = "txt" # handler to write to txt files self.txt_handler = TXTHandler() def export_sudoku(self, sudoku_matrix, type_file, path, name_file): """ Export the sudoku to a given export type such as txt file Return True if the sudoku was successfully exported otherwise return False Keyword arguments: sudoku_matrix -- the matrix which contains the sudoku data to export type_file -- the type where the file will be exported e.g txt path -- the path where the sudoku will be exported name_export -- the name of the file where the sudoku will be exported """ sudoku_matrix = self.get_format_sudoku(sudoku_matrix) sudoku_exported = False if type_file == self.export_to_cmd_line : print sudoku_matrix sudoku_exported = True if type_file == self.export_to_txt: sudoku_exported = self.txt_handler.write_sudoku(path, name_file, sudoku_matrix) return sudoku_exported def get_format_sudoku(self, sudoku_matrix): """ Return a line string which contain the sudoku with the expected format to be exported Keyword arguments: sudoku_matrix -- the matrix which contains the sudoku data to export """ size_sudoku = len(sudoku_matrix) line_to_export = "" row = " " sep = "" for i in range (0, size_sudoku): for j in range(0, size_sudoku): if (j % 3 == 0 and j > 0): row = row + "|"+" " row = row + sudoku_matrix[i][j] + " " if (i % 3 == 0 and i > 0): for h in range (0, size_sudoku): if (h % 3 == 0 and h > 0): sep = sep + " + -" else: sep = sep + " -" line_to_export = line_to_export + sep + "\n" sep = "" line_to_export = line_to_export+ row + "\n" row = " " return line_to_export
class TestTXTHandler(unittest.TestCase): def setUp(self): self.txt_handler = TXTHandler() self.txt_file = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.pathname = os.path.dirname(file) # create an file to test the existing file case my_file = open(self.pathname + '/test_exist_file.txt', 'w') my_file.write("te") my_file.close() # Valid txt format 9x9 self.txt = open ('valid_sudoku_0.txt', 'rb') self.valid_txt = self.txt.read() # Invalid txt format 9x9 self.txt = open ('invalid_sudoku_0.txt', 'rb') self.invalid_txt = self.txt.read() self.size = len(self.valid_txt) def tearDown(self): os.remove(self.pathname + '/test_exist_file.txt') def test_write_sudoku_returns_true_file_has_been_write(self): res = self.txt_handler.write_sudoku(self.pathname + "/", 'test_not_exist_file.txt', "test") os.remove(self.pathname + '/test_not_exist_file.txt') self.assertTrue(res) def test_write_sudoku_writes_the_specified_line(self): line_to_write = "test\n" + \ "this is a test" self.txt_handler.write_sudoku(self.pathname + "/", \ 'test_write_specified_line.txt', line_to_write) file_written = open(self.pathname+"/" + 'test_write_specified_line.txt', "r") content = file_written.readlines() res = "" for l in content : res = res + l file_written.close() os.remove(self.pathname + '/test_write_specified_line.txt') self.assertEquals(res,line_to_write) def test_read_sudoku_for_valid_txt_using_separator_0(self): self.expected = '316578492\n' + \ '529134768\n' + \ '487629531\n' + \ '263415987\n' + \ '974863125\n' + \ '851792643\n' + \ '138947256\n' + \ '692351874\n' + \ '745286319\n' result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_valid_size_using_0(self): self.expected_size = 9 result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_invalid_txt_using_separator_0(self): self.expected = '31657849\n' + \ '529134768\n' + \ '487629531\n' + \ '263415987\n' + \ '974863125\n' + \ '851792643\n' + \ '138947256\n' + \ '692351874\n' result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_invalid_size_using_0(self): self.expected_size = -1 result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_valid_txt_using_separator_dot(self): self.expected = '3.657.4.2\n' + \ '..913.76.\n' + \ '487...531\n' + \ '26..15.8.\n' + \ '...86..25\n' + \ '.5.7.2.4.\n' + \ '.389.725.\n' + \ '6.2..1.74\n' + \ '745.6.1..\n' result = self.txt_file.get_sudoku_data('valid_sudoku_dot.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_valid_size_using_dot(self): self.expected_size = 9 result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_invalid_txt_using_separator_dot(self): txt_file = TXTHandler() self.expected = '3.657..2\n' + \ '..913.76.\n' + \ '487...531\n' + \ '26..15.8.\n' + \ '...86..25\n' + \ '.5.7.2.4.\n' + \ '.389.725.\n' + \ '6.2..1.74\n' result = self.txt_file.get_sudoku_data('invalid_sudoku_dot.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_invalid_size_using_dot(self): self.expected_size = -1 result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1])
class TestTXTHandler(unittest.TestCase): def setUp(self): self.txt_handler = TXTHandler() self.txt_file = TXTHandler() # get the pathdir where the application is running file = sys.argv[0] self.pathname = os.path.dirname(file) # create an file to test the existing file case my_file = open(self.pathname + '/test_exist_file.txt', 'w') my_file.write("te") my_file.close() # Valid txt format 9x9 self.txt = open('valid_sudoku_0.txt', 'rb') self.valid_txt = self.txt.read() # Invalid txt format 9x9 self.txt = open('invalid_sudoku_0.txt', 'rb') self.invalid_txt = self.txt.read() self.size = len(self.valid_txt) def tearDown(self): os.remove(self.pathname + '/test_exist_file.txt') def test_write_sudoku_returns_true_file_has_been_write(self): res = self.txt_handler.write_sudoku(self.pathname + "/", 'test_not_exist_file.txt', "test") os.remove(self.pathname + '/test_not_exist_file.txt') self.assertTrue(res) def test_write_sudoku_writes_the_specified_line(self): line_to_write = "test\n" + \ "this is a test" self.txt_handler.write_sudoku(self.pathname + "/", \ 'test_write_specified_line.txt', line_to_write) file_written = open( self.pathname + "/" + 'test_write_specified_line.txt', "r") content = file_written.readlines() res = "" for l in content: res = res + l file_written.close() os.remove(self.pathname + '/test_write_specified_line.txt') self.assertEquals(res, line_to_write) def test_read_sudoku_for_valid_txt_using_separator_0(self): self.expected = '316578492\n' + \ '529134768\n' + \ '487629531\n' + \ '263415987\n' + \ '974863125\n' + \ '851792643\n' + \ '138947256\n' + \ '692351874\n' + \ '745286319\n' result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_valid_size_using_0(self): self.expected_size = 9 result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_invalid_txt_using_separator_0(self): self.expected = '31657849\n' + \ '529134768\n' + \ '487629531\n' + \ '263415987\n' + \ '974863125\n' + \ '851792643\n' + \ '138947256\n' + \ '692351874\n' result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_invalid_size_using_0(self): self.expected_size = -1 result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_valid_txt_using_separator_dot(self): self.expected = '3.657.4.2\n' + \ '..913.76.\n' + \ '487...531\n' + \ '26..15.8.\n' + \ '...86..25\n' + \ '.5.7.2.4.\n' + \ '.389.725.\n' + \ '6.2..1.74\n' + \ '745.6.1..\n' result = self.txt_file.get_sudoku_data('valid_sudoku_dot.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_valid_size_using_dot(self): self.expected_size = 9 result = self.txt_file.get_sudoku_data('valid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1]) def test_read_sudoku_for_invalid_txt_using_separator_dot(self): txt_file = TXTHandler() self.expected = '3.657..2\n' + \ '..913.76.\n' + \ '487...531\n' + \ '26..15.8.\n' + \ '...86..25\n' + \ '.5.7.2.4.\n' + \ '.389.725.\n' + \ '6.2..1.74\n' result = self.txt_file.get_sudoku_data('invalid_sudoku_dot.txt') self.assertEquals(self.expected, result[0][0]) def test_read_sudoku_for_invalid_size_using_dot(self): self.expected_size = -1 result = self.txt_file.get_sudoku_data('invalid_sudoku_0.txt') self.assertEquals(self.expected_size, result[0][1])