|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from qrcode.constants import ERROR_CORRECT_H, PIL_AVAILABLE |
| 9 | +from qrcode.main import QRCode |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from tempfile import NamedTemporaryFile |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed") |
| 16 | +def test_moduledrawer_import() -> None: |
| 17 | + """ |
| 18 | + Importing a drawer from qrcode.image.styles.moduledrawers is deprecated |
| 19 | + and will raise a DeprecationWarning. |
| 20 | +
|
| 21 | + Removed in v9.0. |
| 22 | + """ |
| 23 | + # These module imports are fine to import |
| 24 | + from qrcode.image.styles.moduledrawers import base, pil, svg |
| 25 | + |
| 26 | + with pytest.warns( |
| 27 | + DeprecationWarning, |
| 28 | + match="Importing 'SquareModuleDrawer' directly from this module is deprecated.", |
| 29 | + ): |
| 30 | + from qrcode.image.styles.moduledrawers import SquareModuleDrawer |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.skipif(PIL_AVAILABLE, reason="PIL is installed") |
| 34 | +def test_moduledrawer_import_pil_not_installed() -> None: |
| 35 | + """ |
| 36 | + Importing from qrcode.image.styles.moduledrawers is deprecated, however, |
| 37 | + if PIL is not installed, there will be no (false) warning; it's a simple |
| 38 | + ImportError. |
| 39 | +
|
| 40 | + Removed in v9.0. |
| 41 | + """ |
| 42 | + # These module imports are fine to import |
| 43 | + from qrcode.image.styles.moduledrawers import base, svg |
| 44 | + |
| 45 | + # Importing a backwards compatible module drawer does normally render a |
| 46 | + # DeprecationWarning; however, since PIL is not installed, it will raise an |
| 47 | + # ImportError. |
| 48 | + with pytest.raises(ImportError): |
| 49 | + from qrcode.image.styles.moduledrawers import SquareModuleDrawer |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed") |
| 53 | +def test_make_image_embeded_parameters() -> None: |
| 54 | + """ |
| 55 | + Using 'embeded_image_path' or 'embeded_image' parameters with QRCode.make_image() |
| 56 | + is deprecated and will raise a DeprecationWarning. |
| 57 | +
|
| 58 | + Removed in v9.0. |
| 59 | + """ |
| 60 | + |
| 61 | + # Create a QRCode required for embedded images |
| 62 | + qr = QRCode(error_correction=ERROR_CORRECT_H) |
| 63 | + qr.add_data("test") |
| 64 | + |
| 65 | + # Test with embeded_image_path parameter |
| 66 | + with pytest.warns( |
| 67 | + DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated" |
| 68 | + ): |
| 69 | + qr.make_image(embeded_image_path="dummy_path") |
| 70 | + |
| 71 | + # Test with embeded_image parameter |
| 72 | + with pytest.warns( |
| 73 | + DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated." |
| 74 | + ): |
| 75 | + qr.make_image(embeded_image="dummy_image") |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed") |
| 79 | +def test_styledpilimage_embeded_parameters(dummy_image: NamedTemporaryFile) -> None: |
| 80 | + """ |
| 81 | + Using 'embeded_image_path' or 'embeded_image' parameters with StyledPilImage |
| 82 | + is deprecated and will raise a DeprecationWarning. |
| 83 | +
|
| 84 | + Removed in v9.0. |
| 85 | + """ |
| 86 | + from PIL import Image |
| 87 | + |
| 88 | + from qrcode.image.styledpil import StyledPilImage |
| 89 | + |
| 90 | + styled_kwargs = { |
| 91 | + "border": 4, |
| 92 | + "width": 21, |
| 93 | + "box_size": 10, |
| 94 | + "qrcode_modules": 1, |
| 95 | + } |
| 96 | + |
| 97 | + try: |
| 98 | + # Test with embeded_image_path parameter |
| 99 | + with pytest.warns( |
| 100 | + DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated." |
| 101 | + ): |
| 102 | + StyledPilImage(embeded_image_path=dummy_image.name, **styled_kwargs) |
| 103 | + |
| 104 | + # Test with embeded_image parameter |
| 105 | + embedded_img = Image.open(dummy_image.name) |
| 106 | + |
| 107 | + with pytest.warns( |
| 108 | + DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated." |
| 109 | + ): |
| 110 | + StyledPilImage(embeded_image=embedded_img, **styled_kwargs) |
| 111 | + |
| 112 | + # Make sure the temporary image is always deleted after the testrun. |
| 113 | + finally: |
| 114 | + Path(dummy_image.name).unlink(missing_ok=True) |
0 commit comments