testsAndMisc/python_pkg/brother_printer/tests/test_network_query.py
Krzysztof kuhy Rudnicki ee27d10fef Reduce per-file-ignores by fixing lint violations across codebase
Fix ruff violations in ~15 source files and ~60+ test files to minimize
per-file-ignores in pyproject.toml. Remaining ignores are justified with
comments explaining why each suppression is necessary.

Source fixes: FBT003 (keyword args), S310 (URL validation), SLF001
(private access), T201 (print→logging), C901 (complexity), E501 (line
length), E402 (import order).

Test fixes: SIM117 (combined with), FBT (boolean args), PERF203 (try in
loop), S310/S607 (URLs/executables), E402/E501 (imports/lines), S108
(tmp paths), PLR0913 (too many args), ARG (unused args), ANN (type
annotations), RUF059 (unused unpacked vars), PT019 (fixture naming).

Remaining per-file-ignores (with justifications):
- Tests: ARG, D, PLC0415, PLR2004, S101, SLF001
- music_gen sources: PLC0415 (heavy ML lazy imports)
- moviepy_showcase: PLC0415 (circular dependency)
- generate_images: PLR0913 (matplotlib helpers need many params)
- praca_magisterska_video: E501, E402 (long paths, mpl.use)
2026-03-25 18:58:05 +01:00

190 lines
6.6 KiB
Python

"""Tests for brother_printer.network_query module."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
from python_pkg.brother_printer.network_query import (
_build_network_result,
_check_snmp_connectivity,
_snmpget_cmd,
_snmpwalk_cmd,
query_network_snmp,
snmp_walk,
)
class TestSnmpwalkCmd:
def test_builds_correct_command(self) -> None:
cmd = _snmpwalk_cmd("/usr/bin/snmpwalk", "public", 5, "1.2.3.4", "1.3.6")
assert cmd == [
"/usr/bin/snmpwalk",
"-v",
"2c",
"-c",
"public",
"-t",
"5",
"-OQvs",
"1.2.3.4",
"1.3.6",
]
class TestSnmpgetCmd:
def test_builds_correct_command(self) -> None:
cmd = _snmpget_cmd("/usr/bin/snmpget", "public", 5, "1.2.3.4", "1.3.6")
assert cmd == [
"/usr/bin/snmpget",
"-v",
"2c",
"-c",
"public",
"-t",
"5",
"1.2.3.4",
"1.3.6",
]
class TestSnmpWalk:
@patch("python_pkg.brother_printer.network_query.shutil.which", return_value=None)
def test_no_snmpwalk(self, mock: MagicMock) -> None:
assert snmp_walk("1.2.3.4", "1.3.6", "public", 5) == []
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpwalk",
)
def test_success(self, w: MagicMock, mock_run: MagicMock) -> None:
mock_run.return_value = MagicMock(
stdout=' "Brother HL-1110" \n "SN123" \n',
)
result = snmp_walk("1.2.3.4", "1.3.6", "public", 5)
assert result == ["Brother HL-1110", "SN123"]
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpwalk",
)
def test_empty_lines_stripped(self, w: MagicMock, mock_run: MagicMock) -> None:
mock_run.return_value = MagicMock(stdout=" \n value \n \n")
result = snmp_walk("1.2.3.4", "1.3.6", "public", 5)
assert result == ["value"]
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpwalk",
)
def test_timeout(self, w: MagicMock, mock_run: MagicMock) -> None:
import subprocess
mock_run.side_effect = subprocess.TimeoutExpired("snmpwalk", 15)
assert snmp_walk("1.2.3.4", "1.3.6", "public", 5) == []
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpwalk",
)
def test_oserror(self, w: MagicMock, mock_run: MagicMock) -> None:
mock_run.side_effect = OSError("fail")
assert snmp_walk("1.2.3.4", "1.3.6", "public", 5) == []
class TestCheckSnmpConnectivity:
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value=None,
)
def test_no_snmpget(self, mock: MagicMock) -> None:
result = _check_snmp_connectivity("1.2.3.4", "public", 5)
assert result is not None
assert "snmpget not found" in result
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpget",
)
def test_success(self, w: MagicMock, mock_run: MagicMock) -> None:
mock_run.return_value = MagicMock()
assert _check_snmp_connectivity("1.2.3.4", "public", 5) is None
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpget",
)
def test_timeout(self, w: MagicMock, mock_run: MagicMock) -> None:
import subprocess
mock_run.side_effect = subprocess.TimeoutExpired("snmpget", 10)
result = _check_snmp_connectivity("1.2.3.4", "public", 5)
assert result is not None
assert "Cannot reach" in result
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpget",
)
def test_called_process_error(self, w: MagicMock, mock_run: MagicMock) -> None:
import subprocess
mock_run.side_effect = subprocess.CalledProcessError(1, "snmpget")
result = _check_snmp_connectivity("1.2.3.4", "public", 5)
assert result is not None
@patch("python_pkg.brother_printer.network_query.subprocess.run")
@patch(
"python_pkg.brother_printer.network_query.shutil.which",
return_value="/usr/bin/snmpget",
)
def test_oserror(self, w: MagicMock, mock_run: MagicMock) -> None:
mock_run.side_effect = OSError("fail")
result = _check_snmp_connectivity("1.2.3.4", "public", 5)
assert result is not None
class TestBuildNetworkResult:
@patch("python_pkg.brother_printer.network_query.snmp_walk")
def test_builds_result(self, mock_walk: MagicMock) -> None:
mock_walk.return_value = ["Test Value"]
result = _build_network_result("1.2.3.4", "public", 5)
assert result.ip == "1.2.3.4"
assert result.product == "Test Value"
@patch("python_pkg.brother_printer.network_query.snmp_walk")
def test_empty_values(self, mock_walk: MagicMock) -> None:
mock_walk.return_value = []
result = _build_network_result("1.2.3.4", "public", 5)
assert result.product == "Unknown"
assert result.serial == ""
class TestQueryNetworkSnmp:
@patch("python_pkg.brother_printer.network_query._build_network_result")
@patch(
"python_pkg.brother_printer.network_query._check_snmp_connectivity",
return_value=None,
)
def test_success(self, c: MagicMock, mock_build: MagicMock) -> None:
from python_pkg.brother_printer.data_classes import NetworkResult
mock_build.return_value = NetworkResult(ip="1.2.3.4")
result = query_network_snmp("1.2.3.4")
assert result.ip == "1.2.3.4"
assert result.error == ""
@patch(
"python_pkg.brother_printer.network_query._check_snmp_connectivity",
return_value="Error msg",
)
def test_connectivity_error(self, c: MagicMock) -> None:
result = query_network_snmp("1.2.3.4")
assert result.error == "Error msg"