52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
"""Regression tests for generated and resumable Sudoku games."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from tempfile import TemporaryDirectory
|
|
from pathlib import Path
|
|
import unittest
|
|
|
|
from app.sudoku import SudokuError, SudokuStore, generate_game
|
|
|
|
|
|
class SudokuTests(unittest.TestCase):
|
|
def test_generated_puzzle_has_a_complete_solution(self) -> None:
|
|
game = generate_game("easy")
|
|
self.assertEqual(len(game["puzzle"]), 81)
|
|
self.assertEqual(len(game["solution"]), 81)
|
|
self.assertTrue(any(cell == 0 for cell in game["puzzle"]))
|
|
self.assertTrue(all(1 <= cell <= 9 for cell in game["solution"]))
|
|
|
|
def test_game_can_be_resumed_with_same_nickname(self) -> None:
|
|
with TemporaryDirectory() as directory:
|
|
store = SudokuStore(sqlite_path=Path(directory) / "sudoku.sqlite3")
|
|
created = store.create("Лис_7", "easy")
|
|
board = created["board"].copy()
|
|
empty_cell = board.index(0)
|
|
board[empty_cell] = created["solution"][empty_cell]
|
|
saved = store.save(created["id"], "лис_7", board, 42)
|
|
current = store.current("ЛИС_7")
|
|
self.assertEqual(saved["board"][empty_cell], created["solution"][empty_cell])
|
|
self.assertEqual(current["id"], created["id"])
|
|
self.assertEqual(current["elapsedSeconds"], 42)
|
|
|
|
def test_given_cells_and_conflicting_moves_are_rejected(self) -> None:
|
|
with TemporaryDirectory() as directory:
|
|
store = SudokuStore(sqlite_path=Path(directory) / "sudoku.sqlite3")
|
|
created = store.create("Белка", "easy")
|
|
changed_clue = created["board"].copy()
|
|
clue_cell = next(index for index, value in enumerate(created["puzzle"]) if value)
|
|
changed_clue[clue_cell] = 0
|
|
with self.assertRaisesRegex(SudokuError, "Исходные"):
|
|
store.save(created["id"], "Белка", changed_clue, 0)
|
|
conflict = created["board"].copy()
|
|
empty_cell = conflict.index(0)
|
|
row_start = empty_cell // 9 * 9
|
|
conflict[empty_cell] = next(value for value in conflict[row_start:row_start + 9] if value)
|
|
with self.assertRaisesRegex(SudokuError, "строке"):
|
|
store.save(created["id"], "Белка", conflict, 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|