class TestReadFromCsv(unittest.TestCase): def setUp(self): self.route_one_sudoku = '../custom_games/easy1.csv' self.route_several_sudokus = '../custom_games/easy2.csv' self.read_csv_one_sudoku = ReadCsvFile(self.route_one_sudoku) self.read_csv_several_sudokus = ReadCsvFile(self.route_several_sudokus) def test_verify_if_file_exist_for_one_sudoku(self): self.assertTrue(os.path.isfile(self.route_one_sudoku)) def test_verify_if_file_exist_for_several_sudokua(self): self.assertTrue(os.path.isfile(self.route_several_sudokus)) def test_read_a_csv_file_with_one_sudoku_puzzle(self): expected_one_sudoku = ['00302060090030500100180640000810290070000000800' \ '6708200002609500800203009005010300'] one_sudoku = self.read_csv_one_sudoku.read_file() self.assertEqual(expected_one_sudoku, one_sudoku) def test_read_a_csv_file_with_several_sudoku_puzzle(self): expected_several_sudokus = ['002003000003410008004097000040500001605030029' \ '000000004050600000000009070000001030', '530029006002810000010000900006000000400090002' \ '000052000065000000000700310190000700', '120000000578400000000000100009060004006040020' \ '000190700000050200060801970000300000'] several_sudokus = self.read_csv_several_sudokus.read_file() self.assertEqual(expected_several_sudokus, several_sudokus)
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())
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
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_file() 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 9 digits for %s row of sudoku:" % str(row + 1))) 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
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 setUp(self): self.route_one_sudoku = '../custom_games/easy1.csv' self.route_several_sudokus = '../custom_games/easy2.csv' self.read_csv_one_sudoku = ReadCsvFile(self.route_one_sudoku) self.read_csv_several_sudokus = ReadCsvFile(self.route_several_sudokus)