import pytest
import quickcodes
import os
import tempfile
from pathlib import Path
class TestQuickCodes:
def test_generate_qr_svg(self):
result = quickcodes.generate("QRCode", "test", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
assert b"<svg" in result
assert b"</svg>" in result
def test_generate_qr_png(self):
result = quickcodes.generate("QRCode", "test", "PNG")
assert isinstance(result, bytes)
assert len(result) > 0
assert result[:8] == b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a'
def test_generate_ean13_svg(self):
result = quickcodes.generate("EAN13", "123456789012", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
assert b"<svg" in result
def test_generate_ean13_png(self):
result = quickcodes.generate("EAN13", "123456789012", "PNG")
assert isinstance(result, bytes)
assert len(result) > 0
assert result[:8] == b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a'
def test_generate_upca(self):
result = quickcodes.generate("UPCA", "03600029145", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
assert b"<svg" in result
def test_generate_code128(self):
result = quickcodes.generate("Code128", "HELLO123", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
assert b"<svg" in result
def test_generate_to_file(self):
with tempfile.TemporaryDirectory() as tmpdir:
output_path = os.path.join(tmpdir, "test_qr.svg")
quickcodes.generate_to_file("QRCode", "test", output_path)
assert os.path.exists(output_path)
with open(output_path, "rb") as f:
content = f.read()
assert b"<svg" in content
def test_qr_error_correction_levels(self):
for level in ["Low", "Medium", "Quartile", "High"]:
result = quickcodes.generate(
"QRCode",
"test",
"SVG",
error_correction=level
)
assert isinstance(result, bytes)
assert len(result) > 0
def test_custom_margins(self):
result = quickcodes.generate(
"QRCode",
"test",
"SVG",
margin=30
)
assert isinstance(result, bytes)
assert len(result) > 0
def test_invalid_barcode_type(self):
with pytest.raises(ValueError, match="Unsupported barcode type"):
quickcodes.generate("InvalidType", "test", "SVG")
def test_invalid_export_format(self):
with pytest.raises(ValueError, match="Unsupported export format"):
quickcodes.generate("QRCode", "test", "InvalidFormat")
def test_invalid_error_correction(self):
with pytest.raises(ValueError, match="Unknown error correction"):
quickcodes.generate("QRCode", "test", "SVG", error_correction="Invalid")
def test_ean13_with_check_digit(self):
result = quickcodes.generate("EAN13", "1234567890128", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
def test_empty_data_handling(self):
result = quickcodes.generate("QRCode", "", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
with pytest.raises(ValueError):
quickcodes.generate("Code128", "", "SVG")
class TestConstants:
def test_constants_exist(self):
assert hasattr(quickcodes, 'BARCODE_TYPES')
assert hasattr(quickcodes, 'EXPORT_FORMATS')
assert hasattr(quickcodes, 'ERROR_CORRECTION_LEVELS')
def test_barcode_types_constants(self):
assert quickcodes.BARCODE_TYPES['QR_CODE'] == 'QRCode'
assert quickcodes.BARCODE_TYPES['EAN13'] == 'EAN13'
assert quickcodes.BARCODE_TYPES['UPC_A'] == 'UPCA'
assert quickcodes.BARCODE_TYPES['CODE128'] == 'Code128'
def test_export_formats_constants(self):
assert quickcodes.EXPORT_FORMATS['SVG'] == 'SVG'
assert quickcodes.EXPORT_FORMATS['PNG'] == 'PNG'
def test_error_correction_constants(self):
assert quickcodes.ERROR_CORRECTION_LEVELS['LOW'] == 'Low'
assert quickcodes.ERROR_CORRECTION_LEVELS['MEDIUM'] == 'Medium'
assert quickcodes.ERROR_CORRECTION_LEVELS['QUARTILE'] == 'Quartile'
assert quickcodes.ERROR_CORRECTION_LEVELS['HIGH'] == 'High'
@pytest.mark.benchmark
class TestPerformance:
def test_qr_generation_performance(self, benchmark):
result = benchmark(quickcodes.generate, "QRCode", "Performance test data", "SVG")
assert isinstance(result, bytes)
assert len(result) > 0
def test_ean13_generation_performance(self, benchmark):
result = benchmark(quickcodes.generate, "EAN13", "123456789012", "PNG")
assert isinstance(result, bytes)
assert len(result) > 0