| | import unittest
|
| | import cv2
|
| | import numpy as np
|
| | from PIL import Image
|
| | import os
|
| | from typing import Dict, Any, Optional, TypedDict, List, Tuple, cast
|
| | from src.utils.barcode import Barcode
|
| |
|
| | class BarcodeResult(TypedDict):
|
| | type: str
|
| | data: str
|
| | valid: bool
|
| |
|
| | class TestBarcode(unittest.TestCase):
|
| | def setUp(self):
|
| | self.barcode = Barcode()
|
| |
|
| | self.test_dir = os.path.dirname(os.path.abspath(__file__))
|
| | self.test_images_dir = os.path.join(self.test_dir, 'test_images')
|
| | os.makedirs(self.test_images_dir, exist_ok=True)
|
| |
|
| | def test_validate_barcode(self):
|
| | """Test barcode validation for different formats"""
|
| |
|
| | self.assertTrue(self.barcode.validate_barcode('5901234123457', 'EAN13'))
|
| |
|
| |
|
| | self.assertFalse(self.barcode.validate_barcode('5901234123458', 'EAN13'))
|
| |
|
| |
|
| | self.assertTrue(self.barcode.validate_barcode('40170725', 'EAN8'))
|
| |
|
| |
|
| | self.assertFalse(self.barcode.validate_barcode('40170726', 'EAN8'))
|
| |
|
| |
|
| | self.assertTrue(self.barcode.validate_barcode('042100005264', 'UPCA'))
|
| |
|
| |
|
| | self.assertFalse(self.barcode.validate_barcode('042100005265', 'UPCA'))
|
| |
|
| | def test_scan_and_validate_from_pil(self):
|
| | """Test barcode scanning from PIL Image"""
|
| |
|
| |
|
| | test_image_path = os.path.join(self.test_images_dir, 'test_barcode.png')
|
| | if not os.path.exists(test_image_path):
|
| |
|
| | img = Image.new('RGB', (400, 200), color='white')
|
| | img.save(test_image_path)
|
| |
|
| |
|
| | image = Image.open(test_image_path)
|
| |
|
| |
|
| | results = self.barcode.scan_and_validate(image)
|
| |
|
| |
|
| | self.assertIsInstance(results, list)
|
| |
|
| |
|
| | for result in results:
|
| | typed_result = cast(BarcodeResult, result)
|
| | self.assertIsInstance(result, dict)
|
| | self.assertTrue(all(key in result for key in ['type', 'data', 'valid']))
|
| | self.assertIsInstance(typed_result['type'], str)
|
| | self.assertIsInstance(typed_result['data'], str)
|
| | self.assertIsInstance(typed_result['valid'], bool)
|
| |
|
| | def test_scan_and_validate_from_cv2(self):
|
| | """Test barcode scanning from OpenCV image"""
|
| |
|
| | test_image_path = os.path.join(self.test_images_dir, 'test_barcode_cv2.png')
|
| | if not os.path.exists(test_image_path):
|
| |
|
| | img = np.full((200, 400, 3), 255, dtype=np.uint8)
|
| | cv2.imwrite(test_image_path, img)
|
| |
|
| |
|
| | image = cv2.imread(test_image_path)
|
| |
|
| |
|
| | scan_result = self.barcode.scan_and_validate(image, show_image=True)
|
| | self.assertIsInstance(scan_result, tuple)
|
| | results, annotated_image = scan_result
|
| |
|
| |
|
| | self.assertIsInstance(results, list)
|
| | self.assertIsInstance(annotated_image, np.ndarray)
|
| |
|
| |
|
| | self.assertEqual(annotated_image.shape, image.shape)
|
| |
|
| | def test_error_handling(self):
|
| | """Test error handling for invalid inputs"""
|
| |
|
| | with self.assertRaises(Exception):
|
| | self.barcode.scan_and_validate(None)
|
| |
|
| |
|
| | with self.assertRaises(Exception):
|
| | self.barcode.scan_and_validate("not an image")
|
| |
|
| |
|
| | self.assertFalse(self.barcode.validate_barcode("", "EAN13"))
|
| | self.assertFalse(self.barcode.validate_barcode("", "UNKNOWN"))
|
| |
|
| |
|
| |
|
| | self.assertFalse(self.barcode.validate_barcode("invalid", "UNKNOWN"))
|
| | self.assertFalse(self.barcode.validate_barcode("12345678", "UNKNOWN"))
|
| | self.assertTrue(self.barcode.validate_barcode("5901234123457", "UNKNOWN"))
|
| |
|
| | if __name__ == '__main__':
|
| | unittest.main() |