| 1 | import unittest |
| 2 | |
| 3 | import chess |
| 4 | |
| 5 | from schach import odds |
| 6 | |
| 7 | |
| 8 | class OddsTest(unittest.TestCase): |
| 9 | def test_none(self): |
| 10 | self.assertIsNone(odds.odds_fen(chess.WHITE, "none")) |
| 11 | self.assertIsNone(odds.describe(chess.STARTING_FEN)) |
| 12 | |
| 13 | def test_knight_odds(self): |
| 14 | fen = odds.odds_fen(chess.WHITE, "knight") |
| 15 | board = chess.Board(fen) |
| 16 | self.assertIsNone(board.piece_at(chess.B1)) |
| 17 | self.assertTrue(board.is_valid()) |
| 18 | self.assertEqual(odds.describe(fen), "Weiß spielt ohne Springer") |
| 19 | |
| 20 | def test_rook_odds_drop_long_castling(self): |
| 21 | board = chess.Board(odds.odds_fen(chess.BLACK, "rook")) |
| 22 | self.assertIsNone(board.piece_at(chess.A8)) |
| 23 | self.assertFalse(board.has_queenside_castling_rights(chess.BLACK)) |
| 24 | self.assertTrue(board.has_kingside_castling_rights(chess.BLACK)) |
| 25 | self.assertTrue(board.has_queenside_castling_rights(chess.WHITE)) |
| 26 | self.assertEqual(odds.describe(board.fen()), "Schwarz spielt ohne Turm") |
| 27 | |
| 28 | def test_pawn_and_queen(self): |
| 29 | self.assertEqual(odds.describe(odds.odds_fen(chess.WHITE, "pawn")), "Weiß spielt ohne Bauer") |
| 30 | self.assertEqual(odds.describe(odds.odds_fen(chess.BLACK, "queen")), "Schwarz spielt ohne Dame") |
| 31 | |
| 32 | def test_other_positions(self): |
| 33 | self.assertEqual(odds.describe("8/8/8/4k3/8/8/8/4K2R w K - 0 1"), "eigene Startstellung") |
| 34 | moved = chess.Board() |
| 35 | moved.push_san("e4") |
| 36 | self.assertEqual(odds.describe(moved.fen()), "eigene Startstellung") |
| 37 | |
| 38 | |
| 39 | class ChallengeRequestTest(unittest.TestCase): |
| 40 | def test_fen_forces_unrated_from_position(self): |
| 41 | from schach import lichess |
| 42 | |
| 43 | client = lichess.Client() |
| 44 | captured = {} |
| 45 | client._stream = lambda method, path, on_line, stop, data: captured.update(path=path, data=data) |
| 46 | fen = odds.odds_fen(chess.WHITE, "knight") |
| 47 | client.challenge("max", 10, 5, "white", True, lambda _id: None, None, fen=fen) |
| 48 | self.assertEqual(captured["path"], "/api/challenge/max") |
| 49 | self.assertEqual(captured["data"]["fen"], fen) |
| 50 | self.assertEqual(captured["data"]["variant"], "fromPosition") |
| 51 | self.assertEqual(captured["data"]["rated"], "false") |
| 52 | |
| 53 | def test_without_fen_unchanged(self): |
| 54 | from schach import lichess |
| 55 | |
| 56 | client = lichess.Client() |
| 57 | captured = {} |
| 58 | client._stream = lambda method, path, on_line, stop, data: captured.update(data=data) |
| 59 | client.challenge("max", 10, 5, "random", True, lambda _id: None, None) |
| 60 | self.assertNotIn("fen", captured["data"]) |
| 61 | self.assertEqual(captured["data"]["rated"], "true") |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | unittest.main() |