Skip to content

Commit da16f4a

Browse files
committed
Split writer-specific tests out of test_qrcode
1 parent eec74e0 commit da16f4a

File tree

3 files changed

+230
-241
lines changed

3 files changed

+230
-241
lines changed

qrcode/tests/test_qrcode.py

Lines changed: 1 addition & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
import io
2-
import os
32
import unittest
4-
import warnings
5-
from tempfile import mkdtemp
63
from unittest import mock
74

8-
import png
9-
105
import qrcode
116
import qrcode.util
12-
from qrcode.compat.pil import Image as pil_Image
137
from qrcode.exceptions import DataOverflowError
148
from qrcode.image.base import BaseImage
15-
from qrcode.image.pure import PyPNGImage
16-
from qrcode.image.styledpil import StyledPilImage
17-
from qrcode.image.styles import colormasks, moduledrawers
18-
from qrcode.tests.consts import UNICODE_TEXT, WHITE, BLACK, RED
9+
from qrcode.tests.consts import UNICODE_TEXT
1910
from qrcode.util import MODE_8BIT_BYTE, MODE_ALPHA_NUM, MODE_NUMBER, QRData
2011

2112

2213
class QRCodeTests(unittest.TestCase):
23-
def setUp(self):
24-
self.tmpdir = mkdtemp()
25-
26-
def tearDown(self):
27-
os.rmdir(self.tmpdir)
28-
2914
def test_basic(self):
3015
qr = qrcode.QRCode(version=1)
3116
qr.add_data("a")
@@ -95,42 +80,6 @@ def test_mode_8bit_newline(self):
9580
qr.make()
9681
self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
9782

98-
@unittest.skipIf(not pil_Image, "Requires PIL")
99-
def test_render_pil(self):
100-
qr = qrcode.QRCode()
101-
qr.add_data(UNICODE_TEXT)
102-
img = qr.make_image()
103-
img.save(io.BytesIO())
104-
self.assertIsInstance(img.get_image(), pil_Image.Image)
105-
106-
@unittest.skipIf(not pil_Image, "Requires PIL")
107-
def test_render_pil_with_transparent_background(self):
108-
qr = qrcode.QRCode()
109-
qr.add_data(UNICODE_TEXT)
110-
img = qr.make_image(back_color="TransParent")
111-
img.save(io.BytesIO())
112-
113-
@unittest.skipIf(not pil_Image, "Requires PIL")
114-
def test_render_pil_with_red_background(self):
115-
qr = qrcode.QRCode()
116-
qr.add_data(UNICODE_TEXT)
117-
img = qr.make_image(back_color="red")
118-
img.save(io.BytesIO())
119-
120-
@unittest.skipIf(not pil_Image, "Requires PIL")
121-
def test_render_pil_with_rgb_color_tuples(self):
122-
qr = qrcode.QRCode()
123-
qr.add_data(UNICODE_TEXT)
124-
img = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35))
125-
img.save(io.BytesIO())
126-
127-
@unittest.skipIf(not pil_Image, "Requires PIL")
128-
def test_render_with_pattern(self):
129-
qr = qrcode.QRCode(mask_pattern=3)
130-
qr.add_data(UNICODE_TEXT)
131-
img = qr.make_image()
132-
img.save(io.BytesIO())
133-
13483
def test_make_image_with_wrong_pattern(self):
13584
with self.assertRaises(TypeError):
13685
qrcode.QRCode(mask_pattern="string pattern")
@@ -171,189 +120,6 @@ class MockFactory(BaseImage):
171120
self.assertTrue(MockFactory.new_image.called)
172121
self.assertTrue(MockFactory.drawrect.called)
173122

174-
def test_render_pypng(self):
175-
qr = qrcode.QRCode()
176-
qr.add_data(UNICODE_TEXT)
177-
img = qr.make_image(image_factory=PyPNGImage)
178-
self.assertIsInstance(img.get_image(), png.Writer)
179-
180-
print(img.width, img.box_size, img.border)
181-
img.save(io.BytesIO())
182-
183-
def test_render_pypng_to_str(self):
184-
qr = qrcode.QRCode()
185-
qr.add_data(UNICODE_TEXT)
186-
img = qr.make_image(image_factory=PyPNGImage)
187-
self.assertIsInstance(img.get_image(), png.Writer)
188-
189-
mock_open = mock.mock_open()
190-
with mock.patch("qrcode.image.pure.open", mock_open, create=True):
191-
img.save("test_file.png")
192-
mock_open.assert_called_once_with("test_file.png", "wb")
193-
mock_open("test_file.png", "wb").write.assert_called()
194-
195-
@unittest.skipIf(not pil_Image, "Requires PIL")
196-
def test_render_styled_Image(self):
197-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
198-
qr.add_data(UNICODE_TEXT)
199-
img = qr.make_image(image_factory=StyledPilImage)
200-
img.save(io.BytesIO())
201-
202-
@unittest.skipIf(not pil_Image, "Requires PIL")
203-
def test_render_styled_with_embeded_image(self):
204-
embeded_img = pil_Image.new("RGB", (10, 10), color="red")
205-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
206-
qr.add_data(UNICODE_TEXT)
207-
img = qr.make_image(image_factory=StyledPilImage, embeded_image=embeded_img)
208-
img.save(io.BytesIO())
209-
210-
@unittest.skipIf(not pil_Image, "Requires PIL")
211-
def test_render_styled_with_embeded_image_path(self):
212-
tmpfile = os.path.join(self.tmpdir, "test.png")
213-
embeded_img = pil_Image.new("RGB", (10, 10), color="red")
214-
embeded_img.save(tmpfile)
215-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
216-
qr.add_data(UNICODE_TEXT)
217-
img = qr.make_image(image_factory=StyledPilImage, embeded_image_path=tmpfile)
218-
img.save(io.BytesIO())
219-
os.remove(tmpfile)
220-
221-
@unittest.skipIf(not pil_Image, "Requires PIL")
222-
def test_render_styled_with_square_module_drawer(self):
223-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
224-
qr.add_data(UNICODE_TEXT)
225-
img = qr.make_image(
226-
image_factory=StyledPilImage,
227-
module_drawer=moduledrawers.SquareModuleDrawer(),
228-
)
229-
img.save(io.BytesIO())
230-
231-
@unittest.skipIf(not pil_Image, "Requires PIL")
232-
def test_render_styled_with_gapped_module_drawer(self):
233-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
234-
qr.add_data(UNICODE_TEXT)
235-
img = qr.make_image(
236-
image_factory=StyledPilImage,
237-
module_drawer=moduledrawers.GappedSquareModuleDrawer(),
238-
)
239-
img.save(io.BytesIO())
240-
241-
@unittest.skipIf(not pil_Image, "Requires PIL")
242-
def test_render_styled_with_circle_module_drawer(self):
243-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
244-
qr.add_data(UNICODE_TEXT)
245-
img = qr.make_image(
246-
image_factory=StyledPilImage,
247-
module_drawer=moduledrawers.CircleModuleDrawer(),
248-
)
249-
img.save(io.BytesIO())
250-
251-
@unittest.skipIf(not pil_Image, "Requires PIL")
252-
def test_render_styled_with_rounded_module_drawer(self):
253-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
254-
qr.add_data(UNICODE_TEXT)
255-
img = qr.make_image(
256-
image_factory=StyledPilImage,
257-
module_drawer=moduledrawers.RoundedModuleDrawer(),
258-
)
259-
img.save(io.BytesIO())
260-
261-
@unittest.skipIf(not pil_Image, "Requires PIL")
262-
def test_render_styled_with_vertical_bars_module_drawer(self):
263-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
264-
qr.add_data(UNICODE_TEXT)
265-
img = qr.make_image(
266-
image_factory=StyledPilImage,
267-
module_drawer=moduledrawers.VerticalBarsDrawer(),
268-
)
269-
img.save(io.BytesIO())
270-
271-
@unittest.skipIf(not pil_Image, "Requires PIL")
272-
def test_render_styled_with_horizontal_bars_module_drawer(self):
273-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
274-
qr.add_data(UNICODE_TEXT)
275-
img = qr.make_image(
276-
image_factory=StyledPilImage,
277-
module_drawer=moduledrawers.HorizontalBarsDrawer(),
278-
)
279-
img.save(io.BytesIO())
280-
281-
@unittest.skipIf(not pil_Image, "Requires PIL")
282-
def test_render_styled_with_default_solid_color_mask(self):
283-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
284-
qr.add_data(UNICODE_TEXT)
285-
mask = colormasks.SolidFillColorMask()
286-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
287-
img.save(io.BytesIO())
288-
289-
@unittest.skipIf(not pil_Image, "Requires PIL")
290-
def test_render_styled_with_solid_color_mask(self):
291-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
292-
qr.add_data(UNICODE_TEXT)
293-
mask = colormasks.SolidFillColorMask(back_color=WHITE, front_color=RED)
294-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
295-
img.save(io.BytesIO())
296-
297-
@unittest.skipIf(not pil_Image, "Requires PIL")
298-
def test_render_styled_with_color_mask_with_transparency(self):
299-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
300-
qr.add_data(UNICODE_TEXT)
301-
mask = colormasks.SolidFillColorMask(
302-
back_color=(255, 0, 255, 255), front_color=RED
303-
)
304-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
305-
img.save(io.BytesIO())
306-
assert img.mode == "RGBA"
307-
308-
@unittest.skipIf(not pil_Image, "Requires PIL")
309-
def test_render_styled_with_radial_gradient_color_mask(self):
310-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
311-
qr.add_data(UNICODE_TEXT)
312-
mask = colormasks.RadialGradiantColorMask(
313-
back_color=WHITE, center_color=BLACK, edge_color=RED
314-
)
315-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
316-
img.save(io.BytesIO())
317-
318-
@unittest.skipIf(not pil_Image, "Requires PIL")
319-
def test_render_styled_with_square_gradient_color_mask(self):
320-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
321-
qr.add_data(UNICODE_TEXT)
322-
mask = colormasks.SquareGradiantColorMask(
323-
back_color=WHITE, center_color=BLACK, edge_color=RED
324-
)
325-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
326-
img.save(io.BytesIO())
327-
328-
@unittest.skipIf(not pil_Image, "Requires PIL")
329-
def test_render_styled_with_horizontal_gradient_color_mask(self):
330-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
331-
qr.add_data(UNICODE_TEXT)
332-
mask = colormasks.HorizontalGradiantColorMask(
333-
back_color=WHITE, left_color=RED, right_color=BLACK
334-
)
335-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
336-
img.save(io.BytesIO())
337-
338-
@unittest.skipIf(not pil_Image, "Requires PIL")
339-
def test_render_styled_with_vertical_gradient_color_mask(self):
340-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
341-
qr.add_data(UNICODE_TEXT)
342-
mask = colormasks.VerticalGradiantColorMask(
343-
back_color=WHITE, top_color=RED, bottom_color=BLACK
344-
)
345-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
346-
img.save(io.BytesIO())
347-
348-
@unittest.skipIf(not pil_Image, "Requires PIL")
349-
def test_render_styled_with_image_color_mask(self):
350-
img_mask = pil_Image.new("RGB", (10, 10), color="red")
351-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
352-
qr.add_data(UNICODE_TEXT)
353-
mask = colormasks.ImageColorMask(back_color=WHITE, color_mask_image=img_mask)
354-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
355-
img.save(io.BytesIO())
356-
357123
def test_optimize(self):
358124
qr = qrcode.QRCode()
359125
text = "A1abc12345def1HELLOa"
@@ -475,9 +241,3 @@ def test_negative_size_at_usage(self):
475241
qr = qrcode.QRCode()
476242
qr.box_size = -1
477243
self.assertRaises(ValueError, qr.make_image)
478-
479-
480-
class ShortcutTest(unittest.TestCase):
481-
@unittest.skipIf(not pil_Image, "Requires PIL")
482-
def runTest(self):
483-
qrcode.make("image")

0 commit comments

Comments
 (0)