161 lines
5.4 KiB
Python
161 lines
5.4 KiB
Python
"""
|
|
Unit tests for enterprise_pdf_checker.py
|
|
"""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, patch, MagicMock
|
|
|
|
|
|
class TestEnterprisePDFChecker:
|
|
"""Test suite for EnterprisePDFChecker class"""
|
|
|
|
def test_checker_initialization_valid_pdf(self, sample_good_pdf):
|
|
"""Test that checker initializes with valid PDF"""
|
|
from enterprise_pdf_checker import EnterprisePDFChecker
|
|
|
|
config = {'anthropic_api_key': 'test-key', 'google_api_key': None}
|
|
checker = EnterprisePDFChecker(str(sample_good_pdf), config)
|
|
assert checker.pdf_path.exists()
|
|
assert checker.pdf_path.suffix == '.pdf'
|
|
|
|
def test_checker_initialization_missing_file(self):
|
|
"""Test that checker initializes but path does not exist"""
|
|
from enterprise_pdf_checker import EnterprisePDFChecker
|
|
|
|
checker = EnterprisePDFChecker("nonexistent.pdf")
|
|
assert not checker.pdf_path.exists()
|
|
|
|
def test_severity_levels(self):
|
|
"""Test that Severity enum has required levels"""
|
|
from enterprise_pdf_checker import Severity
|
|
|
|
assert hasattr(Severity, 'CRITICAL')
|
|
assert hasattr(Severity, 'ERROR')
|
|
assert hasattr(Severity, 'WARNING')
|
|
assert hasattr(Severity, 'INFO')
|
|
assert hasattr(Severity, 'SUCCESS')
|
|
|
|
@patch('enterprise_pdf_checker.anthropic')
|
|
def test_quick_check_without_api(self, mock_anthropic, sample_good_pdf):
|
|
"""Test quick check runs without actual API calls"""
|
|
# Mock Anthropic to avoid real API calls
|
|
mock_anthropic.Anthropic.return_value = MagicMock()
|
|
|
|
from enterprise_pdf_checker import EnterprisePDFChecker
|
|
|
|
config = {'anthropic_api_key': 'test-key', 'google_api_key': None}
|
|
checker = EnterprisePDFChecker(str(sample_good_pdf), config)
|
|
|
|
# Quick check should skip expensive API calls
|
|
# Note: This will still try to analyze the PDF structure
|
|
# but won't make external API calls if properly configured
|
|
try:
|
|
# Test that the method exists and is callable
|
|
assert hasattr(checker, 'run_full_check')
|
|
assert callable(checker.run_full_check)
|
|
except Exception as e:
|
|
pytest.skip(f"Skipping due to: {e}")
|
|
|
|
def test_accessibility_issue_creation(self):
|
|
"""Test AccessibilityIssue dataclass"""
|
|
from enterprise_pdf_checker import AccessibilityIssue, Severity
|
|
|
|
issue = AccessibilityIssue(
|
|
severity=Severity.ERROR,
|
|
category="Test Category",
|
|
description="Test description",
|
|
wcag_criterion="1.1.1",
|
|
recommendation="Test recommendation"
|
|
)
|
|
|
|
assert issue.severity == Severity.ERROR
|
|
assert issue.category == "Test Category"
|
|
assert issue.wcag_criterion == "1.1.1"
|
|
|
|
def test_check_result_structure(self):
|
|
"""Test CheckResult dataclass"""
|
|
from enterprise_pdf_checker import CheckResult
|
|
|
|
result = CheckResult(
|
|
check_name="Test Check",
|
|
passed=True,
|
|
issues=[],
|
|
metadata={'test': 'data'}
|
|
)
|
|
|
|
assert result.check_name == "Test Check"
|
|
assert result.passed is True
|
|
assert isinstance(result.issues, list)
|
|
assert isinstance(result.metadata, dict)
|
|
|
|
|
|
class TestCacheManager:
|
|
"""Test suite for CacheManager class"""
|
|
|
|
def test_cache_key_generation(self):
|
|
"""Test that cache keys are generated correctly"""
|
|
from enterprise_pdf_checker import CacheManager
|
|
|
|
cache_manager = CacheManager()
|
|
|
|
# Test with same content
|
|
key1 = cache_manager.get_cache_key(b"test content")
|
|
key2 = cache_manager.get_cache_key(b"test content")
|
|
|
|
assert key1 == key2
|
|
assert isinstance(key1, str)
|
|
assert len(key1) > 0
|
|
|
|
def test_cache_key_different_content(self):
|
|
"""Test that different content produces different keys"""
|
|
from enterprise_pdf_checker import CacheManager
|
|
|
|
cache_manager = CacheManager()
|
|
|
|
key1 = cache_manager.get_cache_key(b"content 1")
|
|
key2 = cache_manager.get_cache_key(b"content 2")
|
|
|
|
assert key1 != key2
|
|
|
|
|
|
class TestRetryLogic:
|
|
"""Test retry logic integration"""
|
|
|
|
def test_retry_decorator_exists(self):
|
|
"""Test that retry decorators are applied"""
|
|
from enterprise_pdf_checker import EnterprisePDFChecker
|
|
import inspect
|
|
|
|
# Check that methods exist
|
|
assert hasattr(EnterprisePDFChecker, '_analyze_image_with_claude')
|
|
assert hasattr(EnterprisePDFChecker, '_analyze_image_with_google')
|
|
|
|
def test_logger_initialized(self):
|
|
"""Test that logger is properly initialized"""
|
|
import enterprise_pdf_checker
|
|
|
|
assert hasattr(enterprise_pdf_checker, 'logger')
|
|
assert enterprise_pdf_checker.logger is not None
|
|
|
|
|
|
# Integration test (requires actual PDF processing)
|
|
@pytest.mark.integration
|
|
class TestFullCheck:
|
|
"""Integration tests for full PDF checking"""
|
|
|
|
def test_full_workflow_exists(self, sample_good_pdf):
|
|
"""Test that full workflow methods exist"""
|
|
from enterprise_pdf_checker import EnterprisePDFChecker
|
|
|
|
checker = EnterprisePDFChecker(str(sample_good_pdf))
|
|
|
|
# Check that main methods exist
|
|
assert hasattr(checker, 'run_full_check')
|
|
assert hasattr(checker, 'to_dict')
|
|
assert callable(checker.run_full_check)
|
|
assert callable(checker.to_dict)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|