|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import qrcode |
| 8 | +from qrcode.constants import PIL_AVAILABLE |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed") |
| 15 | +def test_qrcode_clear_resets_size(tmp_path: Path): |
| 16 | + """ |
| 17 | + Test that QRCode.clear() properly resets the QRCode object. |
| 18 | +
|
| 19 | + Regression test for: |
| 20 | +
|
| 21 | + QRCode class not resizing down between runs |
| 22 | + https://github.com/lincolnloop/python-qrcode/issues/392 |
| 23 | + """ |
| 24 | + test1_path = tmp_path / "test1.png" |
| 25 | + test2_path = tmp_path / "test2.png" |
| 26 | + test3_path = tmp_path / "test3.png" |
| 27 | + |
| 28 | + # Create a QR code instance |
| 29 | + qr = qrcode.QRCode(version=None) |
| 30 | + |
| 31 | + # Generate first QR code |
| 32 | + qr.add_data("https://example.com/") |
| 33 | + qr.make(fit=True) |
| 34 | + img1 = qr.make_image() |
| 35 | + img1.save(test1_path) |
| 36 | + |
| 37 | + # Clear and generate second QR code with different data |
| 38 | + qr.clear() |
| 39 | + qr.add_data("https://example.net/some/other/path") |
| 40 | + qr.make(fit=True) |
| 41 | + img2 = qr.make_image() |
| 42 | + img2.save(test2_path) |
| 43 | + |
| 44 | + # Clear and generate third QR code with same data as first |
| 45 | + qr.clear() |
| 46 | + qr.add_data("https://example.com/") |
| 47 | + qr.make(fit=True) |
| 48 | + img3 = qr.make_image() |
| 49 | + img3.save(test3_path) |
| 50 | + |
| 51 | + # Check that the first and third QR codes are identical. |
| 52 | + with test1_path.open("rb") as file1, test3_path.open("rb") as file3: |
| 53 | + assert file1.read() == file3.read(), ( |
| 54 | + "First and third QR codes should be identical after clearing" |
| 55 | + ) |
0 commit comments