mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-23 12:46:30 +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
87b2602318
commit
a560341a03
@ -200,7 +200,7 @@ filterwarnings = [
|
|||||||
# COVERAGE - Code coverage configuration
|
# COVERAGE - Code coverage configuration
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
source = ["PYTHON", "articles", "poker-modifier-app", "python_pkg/lichess_bot"]
|
source = ["."]
|
||||||
branch = true
|
branch = true
|
||||||
omit = [
|
omit = [
|
||||||
"*/__pycache__/*",
|
"*/__pycache__/*",
|
||||||
@ -214,14 +214,6 @@ omit = [
|
|||||||
fail_under = 100
|
fail_under = 100
|
||||||
show_missing = true
|
show_missing = true
|
||||||
skip_covered = false
|
skip_covered = false
|
||||||
exclude_lines = [
|
|
||||||
"pragma: no cover",
|
|
||||||
"def __repr__",
|
|
||||||
"raise AssertionError",
|
|
||||||
"raise NotImplementedError",
|
|
||||||
"if __name__ == .__main__.:",
|
|
||||||
"if TYPE_CHECKING:",
|
|
||||||
]
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# VULTURE - Dead code detection
|
# VULTURE - Dead code detection
|
||||||
|
|||||||
@ -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