mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 23:07:03 +01:00
test_cups_service.py 390 -> 172 (usblp port status to _port), part2 261 -> 115 (status mapping and discovery out), part3 260 -> 105 (the four healthy-port cases out). part3 was one 235-line class, so it split by method rather than by class. The four part-numbered files are renamed for what they cover; three of them now name _cups_fallback, matching where the code moved earlier. The first run after the part2 split reported 400, not 402: the moved classes needed subprocess and get_status_info that my hand-written import block had dropped. Fixed, and the count is 402 again. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
103 lines
3.9 KiB
Python
103 lines
3.9 KiB
Python
"""Tests for brother_printer._cups_fallback - the CUPS fallback query."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from python_pkg.brother_printer._cups_fallback import query_usb_via_cups
|
|
from python_pkg.brother_printer.data_classes import USBPortStatus
|
|
|
|
MOD = "python_pkg.brother_printer._cups_fallback"
|
|
|
|
|
|
# ── query_usb_via_cups ───────────────────────────────────────────────
|
|
|
|
|
|
class TestQueryUsbViaCups:
|
|
"""Tests for query_usb_via_cups."""
|
|
|
|
@patch(f"{MOD}.find_cups_printer_name", return_value="")
|
|
@patch(f"{MOD}._ensure_cups_running", return_value=True)
|
|
def test_no_printer(self, e: MagicMock, f: MagicMock) -> None:
|
|
result = query_usb_via_cups()
|
|
assert result.error != ""
|
|
|
|
def test_no_port_status_idle(self) -> None:
|
|
with (
|
|
patch(f"{MOD}._ensure_cups_running", return_value=True),
|
|
patch(f"{MOD}.find_cups_printer_name", return_value="Brother"),
|
|
patch(f"{MOD}._get_pyusb_device_info", return_value={}),
|
|
patch(
|
|
f"{MOD}.printer_info_from_cups",
|
|
return_value={"product": "HL-1110", "serial": "ABC"},
|
|
),
|
|
patch(
|
|
f"{MOD}._get_cups_ipp_status",
|
|
return_value={
|
|
"printer-state": "idle",
|
|
"printer-state-reasons": "none",
|
|
"printer-state-message": "Ready",
|
|
},
|
|
),
|
|
patch(f"{MOD}._get_cups_economode", return_value="ON"),
|
|
patch(f"{MOD}._query_usb_port_status_raw", return_value=None),
|
|
):
|
|
result = query_usb_via_cups()
|
|
assert result.online == "TRUE"
|
|
assert result.product == "HL-1110"
|
|
assert result.economode == "ON"
|
|
|
|
def test_no_port_status_stopped(self) -> None:
|
|
with (
|
|
patch(f"{MOD}._ensure_cups_running", return_value=True),
|
|
patch(f"{MOD}.find_cups_printer_name", return_value="Brother"),
|
|
patch(f"{MOD}._get_pyusb_device_info", return_value={}),
|
|
patch(
|
|
f"{MOD}.printer_info_from_cups",
|
|
return_value={"product": "", "serial": ""},
|
|
),
|
|
patch(
|
|
f"{MOD}._get_cups_ipp_status",
|
|
return_value={
|
|
"printer-state": "stopped",
|
|
"printer-state-reasons": "none",
|
|
},
|
|
),
|
|
patch(f"{MOD}._get_cups_economode", return_value=""),
|
|
patch(f"{MOD}._query_usb_port_status_raw", return_value=None),
|
|
):
|
|
result = query_usb_via_cups()
|
|
assert result.online == "FALSE"
|
|
assert result.product == "Brother Laser Printer"
|
|
|
|
def test_port_status_hw_error(self) -> None:
|
|
with (
|
|
patch(f"{MOD}._ensure_cups_running", return_value=True),
|
|
patch(f"{MOD}.find_cups_printer_name", return_value="Brother"),
|
|
patch(f"{MOD}._get_pyusb_device_info", return_value={}),
|
|
patch(
|
|
f"{MOD}.printer_info_from_cups",
|
|
return_value={"product": "", "serial": ""},
|
|
),
|
|
patch(
|
|
f"{MOD}._get_cups_ipp_status",
|
|
return_value={
|
|
"printer-state": "stopped",
|
|
"printer-state-reasons": "none",
|
|
},
|
|
),
|
|
patch(f"{MOD}._get_cups_economode", return_value=""),
|
|
patch(
|
|
f"{MOD}._query_usb_port_status_raw",
|
|
return_value=USBPortStatus(
|
|
error=True,
|
|
paper_empty=True,
|
|
online=False,
|
|
raw_byte=0x20,
|
|
),
|
|
),
|
|
):
|
|
result = query_usb_via_cups()
|
|
assert result.status_code == "41000"
|
|
assert result.online == "FALSE"
|