mirror of
https://github.com/kuhyx/testsAndMisc-archive.git
synced 2026-07-04 13:23:01 +02:00
refactor(tests): remove noqa comments from test files
- Fix lint issues in keyboard_coop, lichess_bot, and tag_divider tests - Prefix unused parameters with underscore instead of noqa: ARG002
This commit is contained in:
parent
9482719608
commit
2486449300
@ -1,8 +1,5 @@
|
|||||||
"""Unit tests for keyboard_coop module."""
|
"""Unit tests for keyboard_coop module."""
|
||||||
|
|
||||||
# ruff: noqa: SLF001
|
|
||||||
# Tests need to access private members to verify internal logic
|
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,5 @@
|
|||||||
"""Unit tests for lichess_bot engine module."""
|
"""Unit tests for lichess_bot engine module."""
|
||||||
|
|
||||||
# ruff: noqa: SLF001
|
|
||||||
# Tests need to access private members to verify internal logic
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|||||||
@ -1,13 +1,5 @@
|
|||||||
"""Unit tests for lichess_bot lichess_api module."""
|
"""Unit tests for lichess_bot lichess_api module."""
|
||||||
|
|
||||||
# ruff: noqa: SLF001, S105, ARG001, PT012, TRY003, EM101, PERF402
|
|
||||||
# SLF001: Tests need to access private members to verify internal logic
|
|
||||||
# S105: Test tokens are not real passwords
|
|
||||||
# ARG001: Mock functions need *args, **kwargs signature
|
|
||||||
# PT012: pytest.raises can contain multiple statements for generator testing
|
|
||||||
# TRY003, EM101: Exception messages in tests are fine
|
|
||||||
# PERF402: We need loop append for generator consumption with exception break
|
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import json
|
import json
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
@ -18,6 +10,8 @@ import requests
|
|||||||
|
|
||||||
from python_pkg.lichess_bot.lichess_api import LichessAPI
|
from python_pkg.lichess_bot.lichess_api import LichessAPI
|
||||||
|
|
||||||
|
_TERMINATION_MSG = "Test termination"
|
||||||
|
|
||||||
|
|
||||||
class _TestTerminationError(Exception):
|
class _TestTerminationError(Exception):
|
||||||
"""Custom exception to break out of infinite loops in tests."""
|
"""Custom exception to break out of infinite loops in tests."""
|
||||||
@ -30,7 +24,8 @@ class TestLichessAPIInit:
|
|||||||
"""Test initialization creates session with proper headers."""
|
"""Test initialization creates session with proper headers."""
|
||||||
api = LichessAPI("test_token")
|
api = LichessAPI("test_token")
|
||||||
|
|
||||||
assert api.token == "test_token"
|
expected = "test_token"
|
||||||
|
assert api.token == expected
|
||||||
assert "Bearer test_token" in api.session.headers["Authorization"]
|
assert "Bearer test_token" in api.session.headers["Authorization"]
|
||||||
assert "application/json" in api.session.headers["Accept"]
|
assert "application/json" in api.session.headers["Accept"]
|
||||||
|
|
||||||
@ -119,7 +114,7 @@ class TestStreamEvents:
|
|||||||
# stream_events has a while True loop, so we need to break out of it
|
# stream_events has a while True loop, so we need to break out of it
|
||||||
# by raising an exception after yielding our test data
|
# by raising an exception after yielding our test data
|
||||||
|
|
||||||
def iter_lines_with_stop(*args: object, **kwargs: object) -> list[str]:
|
def iter_lines_with_stop(*_args: object, **_kwargs: object) -> list[str]:
|
||||||
"""Return lines then signal generator to stop."""
|
"""Return lines then signal generator to stop."""
|
||||||
return ['{"type": "challenge"}', "", '{"type": "gameStart"}']
|
return ['{"type": "challenge"}', "", '{"type": "gameStart"}']
|
||||||
|
|
||||||
@ -131,12 +126,12 @@ class TestStreamEvents:
|
|||||||
|
|
||||||
call_count = 0
|
call_count = 0
|
||||||
|
|
||||||
def mock_request(*args: object, **kwargs: object) -> MagicMock:
|
def mock_request(*_args: object, **_kwargs: object) -> MagicMock:
|
||||||
nonlocal call_count
|
nonlocal call_count
|
||||||
call_count += 1
|
call_count += 1
|
||||||
if call_count > 1:
|
if call_count > 1:
|
||||||
# Break out of while True on second iteration
|
# Break out of while True on second iteration
|
||||||
raise _TestTerminationError("Test termination")
|
raise _TestTerminationError(_TERMINATION_MSG)
|
||||||
return mock_response
|
return mock_response
|
||||||
|
|
||||||
events_collected: list[dict] = []
|
events_collected: list[dict] = []
|
||||||
@ -144,8 +139,7 @@ class TestStreamEvents:
|
|||||||
patch.object(api, "_request", side_effect=mock_request),
|
patch.object(api, "_request", side_effect=mock_request),
|
||||||
pytest.raises(_TestTerminationError),
|
pytest.raises(_TestTerminationError),
|
||||||
):
|
):
|
||||||
for event in api.stream_events():
|
events_collected.extend(api.stream_events())
|
||||||
events_collected.append(event)
|
|
||||||
|
|
||||||
assert len(events_collected) == 2
|
assert len(events_collected) == 2
|
||||||
assert events_collected[0]["type"] == "challenge"
|
assert events_collected[0]["type"] == "challenge"
|
||||||
@ -154,7 +148,7 @@ class TestStreamEvents:
|
|||||||
def test_stream_events_skips_invalid_json(self, api: LichessAPI) -> None:
|
def test_stream_events_skips_invalid_json(self, api: LichessAPI) -> None:
|
||||||
"""Test stream_events skips non-JSON lines."""
|
"""Test stream_events skips non-JSON lines."""
|
||||||
|
|
||||||
def iter_lines_with_invalid(*args: object, **kwargs: object) -> list[str]:
|
def iter_lines_with_invalid(*_args: object, **_kwargs: object) -> list[str]:
|
||||||
return ['{"type": "challenge"}', "not json", '{"type": "gameStart"}']
|
return ['{"type": "challenge"}', "not json", '{"type": "gameStart"}']
|
||||||
|
|
||||||
mock_response = MagicMock()
|
mock_response = MagicMock()
|
||||||
@ -165,11 +159,11 @@ class TestStreamEvents:
|
|||||||
|
|
||||||
call_count = 0
|
call_count = 0
|
||||||
|
|
||||||
def mock_request(*args: object, **kwargs: object) -> MagicMock:
|
def mock_request(*_args: object, **_kwargs: object) -> MagicMock:
|
||||||
nonlocal call_count
|
nonlocal call_count
|
||||||
call_count += 1
|
call_count += 1
|
||||||
if call_count > 1:
|
if call_count > 1:
|
||||||
raise _TestTerminationError("Test termination")
|
raise _TestTerminationError(_TERMINATION_MSG)
|
||||||
return mock_response
|
return mock_response
|
||||||
|
|
||||||
events_collected: list[dict] = []
|
events_collected: list[dict] = []
|
||||||
@ -177,8 +171,7 @@ class TestStreamEvents:
|
|||||||
patch.object(api, "_request", side_effect=mock_request),
|
patch.object(api, "_request", side_effect=mock_request),
|
||||||
pytest.raises(_TestTerminationError),
|
pytest.raises(_TestTerminationError),
|
||||||
):
|
):
|
||||||
for event in api.stream_events():
|
events_collected.extend(api.stream_events())
|
||||||
events_collected.append(event)
|
|
||||||
|
|
||||||
assert len(events_collected) == 2
|
assert len(events_collected) == 2
|
||||||
|
|
||||||
@ -192,7 +185,7 @@ class TestStreamEvents:
|
|||||||
mock_response_429.__enter__ = MagicMock(return_value=mock_response_429)
|
mock_response_429.__enter__ = MagicMock(return_value=mock_response_429)
|
||||||
mock_response_429.__exit__ = MagicMock(return_value=False)
|
mock_response_429.__exit__ = MagicMock(return_value=False)
|
||||||
|
|
||||||
def iter_lines_ok(*args: object, **kwargs: object) -> list[str]:
|
def iter_lines_ok(*_args: object, **_kwargs: object) -> list[str]:
|
||||||
return ['{"type": "test"}']
|
return ['{"type": "test"}']
|
||||||
|
|
||||||
mock_response_ok = MagicMock()
|
mock_response_ok = MagicMock()
|
||||||
@ -203,14 +196,14 @@ class TestStreamEvents:
|
|||||||
|
|
||||||
call_count = 0
|
call_count = 0
|
||||||
|
|
||||||
def mock_request(*args: object, **kwargs: object) -> MagicMock:
|
def mock_request(*_args: object, **_kwargs: object) -> MagicMock:
|
||||||
nonlocal call_count
|
nonlocal call_count
|
||||||
call_count += 1
|
call_count += 1
|
||||||
if call_count == 1:
|
if call_count == 1:
|
||||||
return mock_response_429
|
return mock_response_429
|
||||||
if call_count == 2:
|
if call_count == 2:
|
||||||
return mock_response_ok
|
return mock_response_ok
|
||||||
raise _TestTerminationError("Test termination")
|
raise _TestTerminationError(_TERMINATION_MSG)
|
||||||
|
|
||||||
events_collected: list[dict] = []
|
events_collected: list[dict] = []
|
||||||
with (
|
with (
|
||||||
@ -218,8 +211,7 @@ class TestStreamEvents:
|
|||||||
patch("python_pkg.lichess_bot.lichess_api.time.sleep"),
|
patch("python_pkg.lichess_bot.lichess_api.time.sleep"),
|
||||||
pytest.raises(_TestTerminationError),
|
pytest.raises(_TestTerminationError),
|
||||||
):
|
):
|
||||||
for event in api.stream_events():
|
events_collected.extend(api.stream_events())
|
||||||
events_collected.append(event)
|
|
||||||
|
|
||||||
assert len(events_collected) == 1
|
assert len(events_collected) == 1
|
||||||
assert call_count == 3 # 429 + OK + termination
|
assert call_count == 3 # 429 + OK + termination
|
||||||
@ -411,7 +403,7 @@ class TestMakeMove:
|
|||||||
|
|
||||||
call_count = 0
|
call_count = 0
|
||||||
|
|
||||||
def mock_request(*args: object, **kwargs: object) -> MagicMock:
|
def mock_request(*_args: object, **_kwargs: object) -> MagicMock:
|
||||||
nonlocal call_count
|
nonlocal call_count
|
||||||
call_count += 1
|
call_count += 1
|
||||||
if call_count == 1:
|
if call_count == 1:
|
||||||
|
|||||||
@ -5,6 +5,7 @@ making it difficult to test the main functionality without refactoring.
|
|||||||
These tests verify the module-level constants.
|
These tests verify the module-level constants.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import importlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
@ -88,7 +89,7 @@ class TestModuleExecution:
|
|||||||
patch("pathlib.Path.iterdir", return_value=[]),
|
patch("pathlib.Path.iterdir", return_value=[]),
|
||||||
patch("os.chdir"),
|
patch("os.chdir"),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# mkdir should have been called twice (once for each folder)
|
# mkdir should have been called twice (once for each folder)
|
||||||
assert mock_mkdir.call_count == 2
|
assert mock_mkdir.call_count == 2
|
||||||
@ -108,7 +109,7 @@ class TestModuleExecution:
|
|||||||
patch("pathlib.Path.iterdir", return_value=[]),
|
patch("pathlib.Path.iterdir", return_value=[]),
|
||||||
patch("os.chdir"),
|
patch("os.chdir"),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# mkdir should not have been called
|
# mkdir should not have been called
|
||||||
mock_mkdir.assert_not_called()
|
mock_mkdir.assert_not_called()
|
||||||
@ -137,7 +138,7 @@ class TestModuleExecution:
|
|||||||
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
||||||
patch("shutil.move", mock_move),
|
patch("shutil.move", mock_move),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# Image should be moved to first folder
|
# Image should be moved to first folder
|
||||||
mock_move.assert_called_once()
|
mock_move.assert_called_once()
|
||||||
@ -166,7 +167,7 @@ class TestModuleExecution:
|
|||||||
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
||||||
patch("shutil.move", mock_move),
|
patch("shutil.move", mock_move),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# Image should be moved to second folder
|
# Image should be moved to second folder
|
||||||
mock_move.assert_called_once()
|
mock_move.assert_called_once()
|
||||||
@ -192,7 +193,7 @@ class TestModuleExecution:
|
|||||||
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
||||||
patch("shutil.move", mock_move),
|
patch("shutil.move", mock_move),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# No image processing should have occurred
|
# No image processing should have occurred
|
||||||
mock_cv2.imread.assert_not_called()
|
mock_cv2.imread.assert_not_called()
|
||||||
@ -222,7 +223,7 @@ class TestModuleExecution:
|
|||||||
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
patch.dict("sys.modules", {"cv2": mock_cv2}),
|
||||||
patch("shutil.move", mock_move),
|
patch("shutil.move", mock_move),
|
||||||
):
|
):
|
||||||
import python_pkg.tag_divider.tag_divider # noqa: F401
|
importlib.import_module("python_pkg.tag_divider.tag_divider")
|
||||||
|
|
||||||
# File should not be moved
|
# File should not be moved
|
||||||
mock_move.assert_not_called()
|
mock_move.assert_not_called()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user