#!/usr/bin/env python3 """ Test script for special pricing detection in UK scraper. This script tests various special pricing scenarios to ensure the enhanced detection works correctly. """ import sys import os import asyncio import logging from bs4 import BeautifulSoup # Add the src directory to the path sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src')) from uk_scraper import UKCateringScraper from config import Config # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def create_test_html_scenarios(): """Create test HTML scenarios for different special pricing patterns.""" scenarios = { 'strikethrough_pricing': """
£15.99 £12.99
""", 'was_now_pricing': """
Was £20.50, now £17.25
""", 'offer_label_pricing': """
SPECIAL OFFER £8.99
""", 'delivery_special_pricing': """

Delivery: £25.00 £19.99

""", 'multiple_prices_no_context': """
£15.99 £12.99
""", 'amazon_deal_pricing': """
£29.99 £24.99
""", 'jj_member_pricing': """
£18.50 Member price: £15.25
""", 'atoz_h3_delivery': """

Delivery: Was £22.00 Now £18.50

""", 'percentage_discount': """
20% OFF RRP £25.00 £20.00
""" } return scenarios async def test_special_pricing_scenarios(): """Test the special pricing detection with various scenarios.""" # Initialize the scraper config = Config() scraper = UKCateringScraper(config) scenarios = create_test_html_scenarios() print("Testing Special Pricing Detection") print("=" * 50) for scenario_name, html_content in scenarios.items(): print(f"\nTesting: {scenario_name}") print("-" * 30) # Parse the HTML soup = BeautifulSoup(html_content, 'html.parser') # Test with different sites for site_name in ['jjfoodservice', 'atoz_catering', 'amazon_uk']: print(f"\n {site_name}:") try: # Test special offer detection special_prices = scraper._find_special_offer_prices(soup, site_name) if special_prices: best_price = min(price for price, _ in special_prices) print(f" ✓ Special offers found: {special_prices}") print(f" ✓ Best price: £{best_price}") else: print(f" ✗ No special offers detected") # Test the extraction methods if site_name == 'jjfoodservice': result = scraper._extract_jjfoodservice_data(soup) elif site_name == 'atoz_catering': result = scraper._extract_atoz_catering_data(soup) elif site_name == 'amazon_uk': result = scraper._extract_amazon_uk_data(soup) if result['price']: print(f" ✓ Extracted price: £{result['price']}") else: print(f" ✗ No price extracted") except Exception as e: print(f" ✗ Error: {e}") def test_parse_uk_price_functionality(): """Test the enhanced _parse_uk_price function.""" config = Config() scraper = UKCateringScraper(config) print("\n\nTesting _parse_uk_price Functionality") print("=" * 50) test_cases = [ ("£15.99", False, False, 15.99), ("Was £20.00 Now £15.99", False, True, 15.99), ("£25.50 £19.99", False, True, 19.99), ("Delivery: £12.50", True, False, 12.50), ("Collection: £10.00 Delivery: £12.50", True, False, 12.50), ("RRP £30.00 Sale £24.99", False, True, 24.99), ("Save £5.00! Was £25.00 Now £20.00", False, True, 20.00), ] for i, (price_text, prefer_delivery, detect_special, expected) in enumerate(test_cases, 1): print(f"\nTest {i}: '{price_text}'") print(f" prefer_delivery={prefer_delivery}, detect_special={detect_special}") # Create a mock element for testing mock_html = f"{price_text}" mock_element = BeautifulSoup(mock_html, 'html.parser').find('span') result = scraper._parse_uk_price( price_text, prefer_delivery=prefer_delivery, detect_special_offers=detect_special, element=mock_element ) if result == expected: print(f" ✓ Result: £{result} (Expected: £{expected})") else: print(f" ✗ Result: £{result} (Expected: £{expected})") def test_special_pricing_context(): """Test the special pricing context detection.""" config = Config() scraper = UKCateringScraper(config) print("\n\nTesting Special Pricing Context Detection") print("=" * 50) context_test_cases = [ ('
£20.00£15.99
', 'strikethrough'), ('
Was £25.00 Now £19.99
', 'was_now'), ('
£12.99
', 'offer_label'), ('
£18.00£14.99
', 'inline_strikethrough'), ] for i, (html_content, test_type) in enumerate(context_test_cases, 1): print(f"\nTest {i}: {test_type}") print(f" HTML: {html_content}") soup = BeautifulSoup(html_content, 'html.parser') element = soup.find(['span', 'div']) if element: context = scraper._extract_special_pricing_context(element) print(f" ✓ Context: {context}") else: print(f" ✗ No element found") if __name__ == "__main__": print("UK Scraper Special Pricing Test Suite") print("=" * 60) # Test the price parsing functionality test_parse_uk_price_functionality() # Test special pricing context detection test_special_pricing_context() # Test full scenarios asyncio.run(test_special_pricing_scenarios()) print("\n" + "=" * 60) print("Test suite completed!")