-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathtest_printer_cups.py
More file actions
176 lines (133 loc) · 4.8 KB
/
test_printer_cups.py
File metadata and controls
176 lines (133 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""tests for the Cups printer
:author: Benito López and the python-escpos developers
:organization: `python-escpos <https://github.com/python-escpos>`_
:copyright: Copyright (c) 2023 `python-escpos <https://github.com/python-escpos>`_
:license: MIT
"""
import logging
import sys
import pytest
# skip all the tests if the platform is Windows
pytestmark = pytest.mark.skipif(
sys.platform == "win32", reason="skipping non Windows platform specific tests"
)
def test_device_not_initialized(cupsprinter) -> None:
"""
GIVEN a cups printer object
WHEN it is not initialized
THEN check the device property is False
"""
assert cupsprinter._device is False
def test_open_raise_exception(cupsprinter, devicenotfounderror) -> None:
"""
GIVEN a cups printer object
WHEN open() is set to raise a DeviceNotFoundError on error
THEN check the exception is raised
"""
cupsprinter.host = "fakehost"
with pytest.raises(devicenotfounderror):
cupsprinter.open(raise_not_found=True)
def test_open_not_raise_exception(cupsprinter, caplog) -> None:
"""
GIVEN a cups printer object
WHEN open() is set to not raise on error but simply cancel
THEN check the error is logged and open() canceled
"""
cupsprinter.host = "fakehost"
with caplog.at_level(logging.ERROR):
cupsprinter.open(raise_not_found=False)
assert "not available" in caplog.text
assert cupsprinter.device is None
def test_open(cupsprinter, caplog, mocker) -> None:
"""
GIVEN a cups printer object and a mocked pycups device
WHEN a valid connection to a device is opened
THEN check the success is logged and the device property is set
"""
mocker.patch("cups.Connection")
mocker.patch("escpos.printer.CupsPrinter.printers", new={"test_printer": "Test"})
cupsprinter.printer_name = "test_printer"
assert cupsprinter.printer_name in cupsprinter.printers
with caplog.at_level(logging.INFO):
cupsprinter.open()
assert "enabled" in caplog.text
assert cupsprinter.device
def test_close_on_reopen(cupsprinter, mocker) -> None:
"""
GIVEN a cups printer object and a mocked connection
WHEN a valid connection to a device is reopened before close
THEN check the close method is called if _device
"""
spy = mocker.spy(cupsprinter, "close")
mocker.patch("cups.Connection")
mocker.patch("escpos.printer.CupsPrinter.printers", new={"test_printer": "Test"})
cupsprinter.printer_name = "test_printer"
cupsprinter.open()
assert cupsprinter._device
cupsprinter.open()
spy.assert_called_once()
def test_close(cupsprinter, caplog, mocker):
"""
GIVEN a cups printer object and a mocked pycups device
WHEN a connection is opened and closed
THEN check the closing is logged and the device property is False
"""
mocker.patch("cups.Connection")
mocker.patch("escpos.printer.CupsPrinter.printers", new={"test_printer": "Test"})
cupsprinter.printer_name = "test_printer"
cupsprinter.open()
with caplog.at_level(logging.INFO):
cupsprinter.close()
assert "Closing" in caplog.text
assert cupsprinter._device is False
def test_send_on_close(cupsprinter, mocker) -> None:
"""
GIVEN a cups printer object and a mocked pycups device
WHEN closing connection before send the buffer
THEN check the buffer is sent and cleared
"""
mocked_cups = mocker.patch("cups.Connection")
spy_send = mocker.spy(cupsprinter, "send")
spy_clear = mocker.spy(cupsprinter, "_clear")
cupsprinter._device = mocked_cups
cupsprinter.pending_job = True
cupsprinter.close()
spy_send.assert_called_once()
spy_clear.assert_called_once()
assert cupsprinter.pending_job is False
def test_raw_raise_exception(cupsprinter) -> None:
"""
GIVEN a cups printer object
WHEN passing a non byte string to _raw()
THEN check an exception is raised and pending_job is False
"""
with pytest.raises(TypeError):
cupsprinter._raw("Non bytes")
assert cupsprinter.pending_job is False
def test_raw(cupsprinter) -> None:
"""
GIVEN a cups printer object
WHEN passing a byte string to _raw()
THEN check the buffer content
"""
cupsprinter._raw(b"Test")
cupsprinter.tmpfile.seek(0)
assert cupsprinter.tmpfile.read() == b"Test"
def test_printers_no_device(cupsprinter) -> None:
"""
GIVEN a cups printer object
WHEN device is None
THEN check the return value is {}
"""
cupsprinter.device = None
assert cupsprinter.printers == {}
def test_read_no_device(cupsprinter) -> None:
"""
GIVEN a cups printer object
WHEN device is None
THEN check the return value is b'8'
"""
cupsprinter.device = None
assert cupsprinter._read() == b"8"