mirror of
https://github.com/kuhyx/testsAndMisc-archive.git
synced 2026-11-26 20:07:21 +01:00
Optimize tests and simplify coverage config
- Optimize slow tests (3.5s -> 0.4s): - Mock threading.Thread instead of spawning real threads - Use PropertyMock instead of generator-based exception - Simplify coverage config: - Set source to '.' to check all Python files - Remove exclude_lines (no special exclusions needed)
This commit is contained in:
parent
30b428ca0a
commit
0af823b962
@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, PropertyMock, patch
|
||||||
|
|
||||||
import chess
|
import chess
|
||||||
import pytest
|
import pytest
|
||||||
@ -781,10 +781,8 @@ class TestFinalizeGame:
|
|||||||
meta = GameMeta(game_id="game1", bot_version=1)
|
meta = GameMeta(game_id="game1", bot_version=1)
|
||||||
|
|
||||||
mock_board = MagicMock()
|
mock_board = MagicMock()
|
||||||
# Create a property that raises TypeError when accessed
|
# Use PropertyMock to raise TypeError when move_stack is accessed
|
||||||
type(mock_board).move_stack = property(
|
type(mock_board).move_stack = PropertyMock(side_effect=TypeError())
|
||||||
lambda _self: (_ for _ in ()).throw(TypeError())
|
|
||||||
)
|
|
||||||
state.board = mock_board
|
state.board = mock_board
|
||||||
|
|
||||||
with patch("python_pkg.lichess_bot.main._write_pgn_to_log"):
|
with patch("python_pkg.lichess_bot.main._write_pgn_to_log"):
|
||||||
@ -1061,25 +1059,20 @@ class TestProcessBotEvent:
|
|||||||
_process_bot_event(event, ctx, game_threads)
|
_process_bot_event(event, ctx, game_threads)
|
||||||
api.accept_challenge.assert_called_once()
|
api.accept_challenge.assert_called_once()
|
||||||
|
|
||||||
def test_process_game_start_event(self, tmp_path: Path) -> None:
|
def test_process_game_start_event(self) -> None:
|
||||||
"""Test processing gameStart event."""
|
"""Test processing gameStart event."""
|
||||||
api = MagicMock()
|
api = MagicMock()
|
||||||
api.stream_game_events.return_value = iter([])
|
|
||||||
ctx = BotContext(api=api, engine=MagicMock(), bot_version=1)
|
ctx = BotContext(api=api, engine=MagicMock(), bot_version=1)
|
||||||
game_threads: GameThreads = {}
|
game_threads: GameThreads = {}
|
||||||
event: Event = {"type": "gameStart", "game": {"id": "game1"}}
|
event: Event = {"type": "gameStart", "game": {"id": "game1"}}
|
||||||
|
|
||||||
with (
|
with patch("python_pkg.lichess_bot.main.threading.Thread") as mock_thread_class:
|
||||||
patch("python_pkg.lichess_bot.main.Path.cwd", return_value=tmp_path),
|
mock_thread = MagicMock()
|
||||||
patch(
|
mock_thread_class.return_value = mock_thread
|
||||||
"python_pkg.lichess_bot.main._run_analysis_subprocess",
|
|
||||||
return_value=None,
|
|
||||||
),
|
|
||||||
):
|
|
||||||
_process_bot_event(event, ctx, game_threads)
|
_process_bot_event(event, ctx, game_threads)
|
||||||
|
|
||||||
assert "game1" in game_threads
|
assert "game1" in game_threads
|
||||||
game_threads["game1"].join(timeout=1)
|
mock_thread.start.assert_called_once()
|
||||||
|
|
||||||
def test_process_game_start_existing_thread(self) -> None:
|
def test_process_game_start_existing_thread(self) -> None:
|
||||||
"""Test processing gameStart with existing alive thread."""
|
"""Test processing gameStart with existing alive thread."""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user