| 1 | """Wertungen im Spielstand (PGN-Tags WhiteElo/BlackElo) und Maias Balken als [%wdl …]: Beim Laden |
| 2 | gelten die Elo-Zahlen zur Zeit der Partie, und die gespeicherten Balken sind sofort da.""" |
| 3 | |
| 4 | import unittest |
| 5 | |
| 6 | import chess |
| 7 | |
| 8 | from schach import app, clipboard, savegames |
| 9 | |
| 10 | WDL = (0.45, 0.30, 0.25) |
| 11 | |
| 12 | |
| 13 | def played(*sans): |
| 14 | board = chess.Board() |
| 15 | for san in sans: |
| 16 | board.push_san(san) |
| 17 | return board |
| 18 | |
| 19 | |
| 20 | class PgnTest(unittest.TestCase): |
| 21 | def test_ratings_and_bars_round_trip(self): |
| 22 | state = savegames.GameState(board=played("e4", "e5"), human=chess.WHITE, elo=1100, |
| 23 | ratings=(1640, 1100), wdls={0: WDL}) |
| 24 | text = savegames.to_pgn(state) |
| 25 | self.assertIn('[WhiteElo "1640"]', text) |
| 26 | self.assertIn("[%wdl 0.450 0.300 0.250]", text) |
| 27 | loaded = savegames.from_pgn(text) |
| 28 | self.assertEqual(loaded.ratings, (1640, 1100)) |
| 29 | self.assertEqual(loaded.wdls, {0: WDL}) |
| 30 | |
| 31 | def test_unknown_side_is_not_written(self): |
| 32 | state = savegames.GameState(board=played("e4"), human=chess.WHITE, elo=1100, ratings=(None, 1100)) |
| 33 | text = savegames.to_pgn(state) |
| 34 | self.assertNotIn("WhiteElo", text) |
| 35 | self.assertEqual(savegames.from_pgn(text).ratings, (None, 1100)) |
| 36 | |
| 37 | def test_question_mark_is_unknown(self): |
| 38 | self.assertEqual(savegames.read_ratings({"WhiteElo": "?", "BlackElo": "1873"}), (None, 1873)) |
| 39 | |
| 40 | def test_pasted_lichess_game_keeps_its_ratings(self): |
| 41 | pasted = clipboard.parse('[White "anna"]\n[Black "ben"]\n[WhiteElo "1520"]\n[BlackElo "1610"]\n\n1. e4 e5 *') |
| 42 | self.assertEqual(pasted.state.ratings, (1520, 1610)) |
| 43 | |
| 44 | |
| 45 | class FakeApp: |
| 46 | lichess_user = None |
| 47 | |
| 48 | |
| 49 | class Window: |
| 50 | """Die echten Methoden aus app.Window auf einer Attrappe.""" |
| 51 | _wdl_elos = app.Window._wdl_elos |
| 52 | _save_ratings = app.Window._save_ratings |
| 53 | _save_wdls = app.Window._save_wdls |
| 54 | _wdl_preload = app.Window._wdl_preload |
| 55 | |
| 56 | def __init__(self, board, mode="maia", names=(), ratings=(None, None), user=None): |
| 57 | self.board, self.mode, self.names, self.ratings = board, mode, names, ratings |
| 58 | self.human, self.elo, self.settings, self.maia_wdls = chess.WHITE, 1100, {"elo": 1100}, {} |
| 59 | self.app = FakeApp() |
| 60 | self.app.lichess_user = user |
| 61 | |
| 62 | def _opponent_elo(self): |
| 63 | return self.elo |
| 64 | |
| 65 | def get_application(self): |
| 66 | return self.app |
| 67 | |
| 68 | |
| 69 | class WindowTest(unittest.TestCase): |
| 70 | def test_stored_ratings_win_over_todays_rating(self): |
| 71 | today = {"perfs": {"rapid": {"rating": 1900, "games": 40}}} |
| 72 | self.assertEqual(Window(played("e4"), user=today)._wdl_elos(), (1900, 1100)) # ohne Tags: heute |
| 73 | self.assertEqual(Window(played("e4"), ratings=(1500, 1100), user=today)._wdl_elos(), (1500, 1100)) |
| 74 | |
| 75 | def test_missing_side_filled_with_maia(self): |
| 76 | self.assertEqual(Window(played("e4"), ratings=(None, 1300))._wdl_elos(), (1100, 1300)) |
| 77 | |
| 78 | def test_saved_ratings_give_back_the_same_elos(self): |
| 79 | """Ohne Lichess-Anmeldung rechnet Maia mit (Maia, Maia); gespeichert wird nur Maias Seite, |
| 80 | beim Laden ergibt sich wieder (Maia, Maia).""" |
| 81 | win = Window(played("e4")) |
| 82 | before = win._wdl_elos() |
| 83 | ratings = win._save_ratings() |
| 84 | self.assertEqual(ratings, (None, 1100)) |
| 85 | self.assertEqual(Window(played("e4"), ratings=ratings)._wdl_elos(), before) |
| 86 | |
| 87 | def test_two_players_write_nothing(self): |
| 88 | self.assertEqual(Window(played("e4"), mode="duo")._save_ratings(), (None, None)) |
| 89 | |
| 90 | def test_bars_saved_and_preloaded(self): |
| 91 | win = Window(played("e4", "e5"), ratings=(1500, 1100)) |
| 92 | after = played("e4").fen() |
| 93 | win.maia_wdls[(after, 1500, 1100)] = WDL |
| 94 | self.assertEqual(win._save_wdls(), {0: WDL}) |
| 95 | fresh = Window(played("e4", "e5"), ratings=(1500, 1100)) |
| 96 | fresh._wdl_preload({0: WDL}) |
| 97 | self.assertEqual(fresh.maia_wdls, {(after, 1500, 1100): WDL}) |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | unittest.main() |