Skip to content

Commit 423579b

Browse files
committed
Parametrize PIL tests for DRYness
1 parent c37646f commit 423579b

File tree

1 file changed

+30
-125
lines changed

1 file changed

+30
-125
lines changed

qrcode/tests/test_qrcode_pil.py

Lines changed: 30 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ def test_render_pil():
1919
assert isinstance(img.get_image(), Image.Image)
2020

2121

22-
def test_render_pil_with_transparent_background():
22+
@pytest.mark.parametrize("back_color", ["TransParent", "red", (255, 195, 235)])
23+
def test_render_pil_background(back_color):
2324
qr = qrcode.QRCode()
2425
qr.add_data(UNICODE_TEXT)
2526
img = qr.make_image(back_color="TransParent")
2627
img.save(io.BytesIO())
2728

2829

29-
def test_render_pil_with_red_background():
30-
qr = qrcode.QRCode()
31-
qr.add_data(UNICODE_TEXT)
32-
img = qr.make_image(back_color="red")
33-
img.save(io.BytesIO())
34-
35-
3630
def test_render_pil_with_rgb_color_tuples():
3731
qr = qrcode.QRCode()
3832
qr.add_data(UNICODE_TEXT)
@@ -72,138 +66,49 @@ def test_render_styled_with_embeded_image_path(tmp_path):
7266
img.save(io.BytesIO())
7367

7468

75-
def test_render_styled_with_square_module_drawer():
76-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
77-
qr.add_data(UNICODE_TEXT)
78-
img = qr.make_image(
79-
image_factory=StyledPilImage,
80-
module_drawer=moduledrawers.SquareModuleDrawer(),
81-
)
82-
img.save(io.BytesIO())
83-
84-
85-
def test_render_styled_with_gapped_module_drawer():
86-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
87-
qr.add_data(UNICODE_TEXT)
88-
img = qr.make_image(
89-
image_factory=StyledPilImage,
90-
module_drawer=moduledrawers.GappedSquareModuleDrawer(),
91-
)
92-
img.save(io.BytesIO())
93-
94-
95-
def test_render_styled_with_circle_module_drawer():
96-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
97-
qr.add_data(UNICODE_TEXT)
98-
img = qr.make_image(
99-
image_factory=StyledPilImage,
100-
module_drawer=moduledrawers.CircleModuleDrawer(),
101-
)
102-
img.save(io.BytesIO())
103-
104-
105-
def test_render_styled_with_rounded_module_drawer():
69+
@pytest.mark.parametrize("drawer", [
70+
moduledrawers.CircleModuleDrawer,
71+
moduledrawers.GappedSquareModuleDrawer,
72+
moduledrawers.HorizontalBarsDrawer,
73+
moduledrawers.RoundedModuleDrawer,
74+
moduledrawers.SquareModuleDrawer,
75+
moduledrawers.VerticalBarsDrawer,
76+
])
77+
def test_render_styled_with_drawer(drawer):
10678
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
10779
qr.add_data(UNICODE_TEXT)
10880
img = qr.make_image(
10981
image_factory=StyledPilImage,
110-
module_drawer=moduledrawers.RoundedModuleDrawer(),
82+
module_drawer=drawer(),
11183
)
11284
img.save(io.BytesIO())
11385

11486

115-
def test_render_styled_with_vertical_bars_module_drawer():
116-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
117-
qr.add_data(UNICODE_TEXT)
118-
img = qr.make_image(
119-
image_factory=StyledPilImage,
120-
module_drawer=moduledrawers.VerticalBarsDrawer(),
121-
)
122-
img.save(io.BytesIO())
123-
124-
125-
def test_render_styled_with_horizontal_bars_module_drawer():
126-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
127-
qr.add_data(UNICODE_TEXT)
128-
img = qr.make_image(
129-
image_factory=StyledPilImage,
130-
module_drawer=moduledrawers.HorizontalBarsDrawer(),
131-
)
132-
img.save(io.BytesIO())
133-
134-
135-
def test_render_styled_with_default_solid_color_mask():
136-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
137-
qr.add_data(UNICODE_TEXT)
138-
mask = colormasks.SolidFillColorMask()
139-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
140-
img.save(io.BytesIO())
141-
142-
143-
def test_render_styled_with_solid_color_mask():
144-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
145-
qr.add_data(UNICODE_TEXT)
146-
mask = colormasks.SolidFillColorMask(back_color=WHITE, front_color=RED)
147-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
148-
img.save(io.BytesIO())
149-
150-
151-
def test_render_styled_with_color_mask_with_transparency():
152-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
153-
qr.add_data(UNICODE_TEXT)
154-
mask = colormasks.SolidFillColorMask(
87+
@pytest.mark.parametrize("mask", [
88+
colormasks.SolidFillColorMask(),
89+
colormasks.SolidFillColorMask(back_color=WHITE, front_color=RED),
90+
colormasks.SolidFillColorMask(
15591
back_color=(255, 0, 255, 255), front_color=RED
156-
)
157-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
158-
img.save(io.BytesIO())
159-
assert img.mode == "RGBA"
160-
161-
162-
def test_render_styled_with_radial_gradient_color_mask():
163-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
164-
qr.add_data(UNICODE_TEXT)
165-
mask = colormasks.RadialGradiantColorMask(
92+
),
93+
colormasks.RadialGradiantColorMask(
16694
back_color=WHITE, center_color=BLACK, edge_color=RED
167-
)
168-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
169-
img.save(io.BytesIO())
170-
171-
172-
def test_render_styled_with_square_gradient_color_mask():
173-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
174-
qr.add_data(UNICODE_TEXT)
175-
mask = colormasks.SquareGradiantColorMask(
95+
),
96+
colormasks.SquareGradiantColorMask(
17697
back_color=WHITE, center_color=BLACK, edge_color=RED
177-
)
178-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
179-
img.save(io.BytesIO())
180-
181-
182-
def test_render_styled_with_horizontal_gradient_color_mask():
183-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
184-
qr.add_data(UNICODE_TEXT)
185-
mask = colormasks.HorizontalGradiantColorMask(
98+
),
99+
colormasks.HorizontalGradiantColorMask(
186100
back_color=WHITE, left_color=RED, right_color=BLACK
187-
)
188-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
189-
img.save(io.BytesIO())
190-
191-
192-
def test_render_styled_with_vertical_gradient_color_mask():
193-
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
194-
qr.add_data(UNICODE_TEXT)
195-
mask = colormasks.VerticalGradiantColorMask(
101+
),
102+
colormasks.VerticalGradiantColorMask(
196103
back_color=WHITE, top_color=RED, bottom_color=BLACK
197-
)
198-
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
199-
img.save(io.BytesIO())
200-
201-
202-
def test_render_styled_with_image_color_mask():
203-
img_mask = Image.new("RGB", (10, 10), color="red")
104+
),
105+
colormasks.ImageColorMask(
106+
back_color=WHITE, color_mask_image=Image.new("RGB", (10, 10), color="red")
107+
),
108+
])
109+
def test_render_styled_with_mask(mask):
204110
qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_L)
205111
qr.add_data(UNICODE_TEXT)
206-
mask = colormasks.ImageColorMask(back_color=WHITE, color_mask_image=img_mask)
207112
img = qr.make_image(image_factory=StyledPilImage, color_mask=mask)
208113
img.save(io.BytesIO())
209114

0 commit comments

Comments
 (0)