iralex-backend/test_qr.py

62 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
Test script for QR code generation functionality
"""
import qrcode
import base64
from io import BytesIO
def test_qr_code_generation():
"""Test QR code generation and base64 conversion"""
# Test data (similar to what would be in lhdn_qrcode field)
test_data = "https://example.com/test-invoice-123"
try:
# Create QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(test_data)
qr.make(fit=True)
# Create QR code image
qr_image = qr.make_image(fill_color="black", back_color="white")
# Convert to base64
buffer = BytesIO()
qr_image.save(buffer, format='PNG')
qr_base64 = base64.b64encode(buffer.getvalue()).decode()
# Create HTML img tag
html_img = f'<img src="data:image/png;base64,{qr_base64}" alt="QR Code" style="width: 150px; height: auto; max-width: 200px; border: 1px solid #ddd;">'
print("✅ QR code generated successfully!")
print(f"📊 Data encoded: {test_data}")
print(f"🔢 Base64 length: {len(qr_base64)} characters")
print(f"📱 HTML img tag length: {len(html_img)} characters")
# Save QR code as PNG file for visual verification
qr_image.save("test_qr_code.png")
print("💾 QR code saved as 'test_qr_code.png'")
return True
except Exception as e:
print(f"❌ Error generating QR code: {e}")
return False
if __name__ == "__main__":
print("🧪 Testing QR code generation...")
success = test_qr_code_generation()
if success:
print("\n🎉 All tests passed! QR code functionality is working correctly.")
else:
print("\n💥 Tests failed! Please check the error messages above.")