class TestReadFromTxt(unittest.TestCase): def setUp(self): self.route_one_sudoku = "../custom_games/easy.txt" self.route_bad_format = "../custom_games/bad_format.txt" self.read_txt_one_sudoku = ReadTxtFile(self.route_one_sudoku) self.read_bad_format = ReadTxtFile(self.route_bad_format) def test_verify_if_file_exist_for_one_sudoku(self): self.assertTrue(os.path.isfile(self.route_one_sudoku)) def test_sudoku_puzzle_not_in_9x9_format(self): expected_one_sudoku = '4000008050300000000007000000200000600000804' \ '00000010000000603070500200000104000000' one_sudoku = self.read_txt_one_sudoku.read_one_sudoku_puzzle() self.assertEqual(expected_one_sudoku, one_sudoku) def test_read_a_csv_file_with_one_sudoku_puzzle(self): one_bad_sudoku = self.read_bad_format.read_one_sudoku_puzzle() self.assertFalse(one_bad_sudoku)
class TestReadFromFile(unittest.TestCase): def setUp(self): self.route_csv_file = '../custom_games/easy1.csv' self.route_txt_file = '../custom_games/easy.txt' self.read = ReadFromFile() self.csv = ReadCsvFile(self.route_csv_file) self.txt = ReadTxtFile(self.route_txt_file) def test_read_csv_file_function(self): expected_read = self.read.read_file(self.route_csv_file) self.assertEqual(expected_read, self.csv.read_file()) def test_read_txt_file_function(self): expected_read = self.read.read_file(self.route_txt_file) self.assertEqual(expected_read, self.txt.read_one_sudoku_puzzle())
class ReadFromFile: def __init__(self): pass def read_file(self, route): """Reads a file and verify if the extension of the file is csv or txt in order to call the respective function to open it. Keyword: route -- Is the path were the csv of txt file is found """ self.route = route if self.route[-3:] == 'csv': self.csv = ReadCsvFile(route) res = self.csv.read_file() elif self.route[-3:] == 'txt': self.txt = ReadTxtFile(route) res = self.txt.read_one_sudoku_puzzle() else: print ("There is no file in the directory") return res def get_sudoku_from_console(self): """Return a string from user that contains a sudoku to solve. """ sudoku_from_console = "" for row in range(9): try: console_row = str(raw_input("Enter the 9 digits for %s row of sudoku:" % str(row))) sudoku_from_console += console_row except: sudoku_from_console = None return sudoku_from_console def inbound_sudoku_has_good_format(self, inbound_sudoku): """ Return True when the string only contains numbers from 0 to 9 or '.' character and its length is 81. Keyword arguments: inbound_sudoku -- string with values for sudoku game from user i.e.: 400000805030000000000700000020000060000080400000010000 """ if re.match("^(\.|[0-9])+$", inbound_sudoku) and len(inbound_sudoku) == 81: return True else: return False