#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Standalone test for Hypothesis strategies (no Odoo dependency). This script verifies that the custom Hypothesis strategies can generate valid test data without requiring the full Odoo environment. """ import sys import re from io import BytesIO try: from hypothesis import given, settings HYPOTHESIS_AVAILABLE = True except ImportError: print("ERROR: Hypothesis is not installed. Install with: pip install hypothesis") sys.exit(1) try: from docx import Document DOCX_AVAILABLE = True except ImportError: print("WARNING: python-docx is not installed. DOCX tests will be skipped.") DOCX_AVAILABLE = False # Import our custom strategies from hypothesis_strategies import ( placeholder_keys, text_with_placeholders, placeholder_mappings, valid_mappings, participant_data, ) if DOCX_AVAILABLE: from hypothesis_strategies import ( docx_with_placeholders, docx_with_mappings_and_data, empty_docx, docx_with_duplicate_placeholders, ) def test_placeholder_keys(): """Test placeholder_keys strategy.""" print("Testing placeholder_keys strategy...") @given(placeholder_keys()) @settings(max_examples=10) def check_format(key): pattern = r'^\{key\.[a-zA-Z][a-zA-Z0-9_]*\}$' assert re.match(pattern, key), f"Invalid key format: {key}" check_format() print("✓ placeholder_keys generates valid formats") def test_text_with_placeholders(): """Test text_with_placeholders strategy.""" print("\nTesting text_with_placeholders strategy...") @given(text_with_placeholders(min_placeholders=1, max_placeholders=5)) @settings(max_examples=10) def check_text(result): text, placeholders = result assert isinstance(text, str), "Text should be a string" assert isinstance(placeholders, list), "Placeholders should be a list" assert len(placeholders) > 0, "Should have at least one placeholder" for placeholder in placeholders: assert placeholder in text, f"Placeholder {placeholder} not in text" check_text() print("✓ text_with_placeholders generates valid text with placeholders") def test_placeholder_mappings(): """Test placeholder_mappings strategy.""" print("\nTesting placeholder_mappings strategy...") @given(placeholder_mappings()) @settings(max_examples=10) def check_mapping(mapping): assert isinstance(mapping, dict), "Mapping should be a dict" assert 'key' in mapping, "Mapping should have 'key'" assert 'value_type' in mapping, "Mapping should have 'value_type'" assert 'value_field' in mapping, "Mapping should have 'value_field'" assert 'custom_text' in mapping, "Mapping should have 'custom_text'" assert mapping['value_type'] in ['survey_field', 'user_field', 'custom_text'] if mapping['value_type'] == 'custom_text': assert mapping['value_field'] == '', "value_field should be empty for custom_text" else: assert mapping['value_field'] != '', "value_field should not be empty" assert mapping['custom_text'] == '', "custom_text should be empty" check_mapping() print("✓ placeholder_mappings generates valid mapping structures") def test_valid_mappings(): """Test valid_mappings strategy.""" print("\nTesting valid_mappings strategy...") @given(valid_mappings(min_placeholders=1, max_placeholders=5)) @settings(max_examples=10) def check_mappings(mappings): assert isinstance(mappings, dict), "Mappings should be a dict" assert 'placeholders' in mappings, "Should have 'placeholders' key" assert isinstance(mappings['placeholders'], list), "placeholders should be a list" assert len(mappings['placeholders']) > 0, "Should have at least one placeholder" # Check uniqueness keys = [p['key'] for p in mappings['placeholders']] assert len(keys) == len(set(keys)), "All placeholder keys should be unique" check_mappings() print("✓ valid_mappings generates valid complete mapping structures") def test_participant_data(): """Test participant_data strategy.""" print("\nTesting participant_data strategy...") @given(participant_data()) @settings(max_examples=10) def check_data(data): assert isinstance(data, dict), "Data should be a dict" required_fields = [ 'survey_title', 'survey_description', 'partner_name', 'partner_email', 'email', 'create_date', 'completion_date', 'scoring_percentage', 'scoring_total' ] for field in required_fields: assert field in data, f"Missing required field: {field}" # Check email format email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' assert re.match(email_pattern, data['partner_email']), "Invalid email format" # Check date format date_pattern = r'^\d{4}-\d{2}-\d{2}$' assert re.match(date_pattern, data['create_date']), "Invalid date format" check_data() print("✓ participant_data generates valid participant data") def test_docx_with_placeholders(): """Test docx_with_placeholders strategy.""" if not DOCX_AVAILABLE: print("\nSkipping docx_with_placeholders test (python-docx not installed)") return print("\nTesting docx_with_placeholders strategy...") @given(docx_with_placeholders(min_placeholders=1, max_placeholders=5)) @settings(max_examples=5) def check_docx(result): docx_binary, placeholders = result assert isinstance(docx_binary, bytes), "DOCX should be bytes" assert len(docx_binary) > 0, "DOCX should have content" assert isinstance(placeholders, list), "Placeholders should be a list" assert len(placeholders) > 0, "Should have at least one placeholder" # Verify it's a valid DOCX doc_stream = BytesIO(docx_binary) doc = Document(doc_stream) assert doc is not None, "Should be a valid DOCX" check_docx() print("✓ docx_with_placeholders generates valid DOCX files") def test_docx_with_mappings_and_data(): """Test docx_with_mappings_and_data strategy.""" if not DOCX_AVAILABLE: print("\nSkipping docx_with_mappings_and_data test (python-docx not installed)") return print("\nTesting docx_with_mappings_and_data strategy...") @given(docx_with_mappings_and_data()) @settings(max_examples=3) def check_coordinated(result): docx_binary, mappings, data = result assert isinstance(docx_binary, bytes), "DOCX should be bytes" assert isinstance(mappings, dict), "Mappings should be a dict" assert isinstance(data, dict), "Data should be a dict" # Verify coordination for mapping in mappings['placeholders']: if mapping['value_type'] != 'custom_text': value_field = mapping['value_field'] assert value_field in data, f"Mapped field '{value_field}' not in data" check_coordinated() print("✓ docx_with_mappings_and_data generates coordinated test data") def main(): """Run all tests.""" print("=" * 60) print("Testing Hypothesis Strategies") print("=" * 60) try: test_placeholder_keys() test_text_with_placeholders() test_placeholder_mappings() test_valid_mappings() test_participant_data() test_docx_with_placeholders() test_docx_with_mappings_and_data() print("\n" + "=" * 60) print("✓ All tests passed!") print("=" * 60) return 0 except Exception as e: print(f"\n✗ Test failed: {e}") import traceback traceback.print_exc() return 1 if __name__ == '__main__': sys.exit(main())