| 1 | import unittest |
| 2 | |
| 3 | import chess |
| 4 | |
| 5 | from schach import explorer, opening |
| 6 | |
| 7 | |
| 8 | class LichessLinesTest(unittest.TestCase): |
| 9 | def test_parse_and_castling(self): |
| 10 | board = chess.Board("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1") |
| 11 | data = {"moves": [ |
| 12 | {"uci": "e1h1", "san": "O-O", "white": 6, "draws": 2, "black": 2}, |
| 13 | {"uci": "a1a8", "san": "Rxa8+", "white": 1, "draws": 0, "black": 1}, |
| 14 | {"uci": "zz", "white": 1}, |
| 15 | ]} |
| 16 | lines = explorer.lichess_lines(data, board) |
| 17 | self.assertEqual([l.move.uci() for l in lines], ["e1g1", "a1a8"]) |
| 18 | self.assertEqual(lines[0].games, 10) |
| 19 | self.assertAlmostEqual(lines[0].share, 10 / 13) # Anteil an allen Partien der Stellung |
| 20 | |
| 21 | def test_illegal_moves_skipped(self): |
| 22 | data = {"moves": [{"uci": "e2e5", "white": 1, "draws": 0, "black": 0}]} |
| 23 | self.assertEqual(explorer.lichess_lines(data, chess.Board()), []) |
| 24 | |
| 25 | |
| 26 | class BookLinesTest(unittest.TestCase): |
| 27 | def test_book_share(self): |
| 28 | class FakeBook: |
| 29 | def moves(self, board): |
| 30 | return [(chess.Move.from_uci("e2e4"), 3), (chess.Move.from_uci("d2d4"), 1)] |
| 31 | lines = explorer.book_lines(FakeBook(), chess.Board()) |
| 32 | self.assertEqual([round(l.share, 2) for l in lines], [0.75, 0.25]) |
| 33 | |
| 34 | def test_real_book_if_present(self): |
| 35 | lines = explorer.book_lines(opening.Book(), chess.Board()) |
| 36 | if lines: |
| 37 | self.assertAlmostEqual(sum(l.share for l in explorer.book_lines(opening.Book(), chess.Board())), 1, 1) |
| 38 | |
| 39 | |
| 40 | class HelpersTest(unittest.TestCase): |
| 41 | def test_ratings_near(self): |
| 42 | self.assertEqual(explorer.ratings_near(1250), [1200, 1400]) |
| 43 | self.assertEqual(explorer.ratings_near(700), [0, 1000]) |
| 44 | self.assertEqual(explorer.ratings_near(2700), [2200, 2500]) |
| 45 | |
| 46 | def test_games_text(self): |
| 47 | self.assertEqual(explorer.games_text(1234), "1.234") |
| 48 | self.assertEqual(explorer.games_text(45678), "45 Tsd.") |
| 49 | self.assertEqual(explorer.games_text(2_345_678), "2,3 Mio.") |
| 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | unittest.main() |