#!/usr/bin/env python3 """ Simple test for word list management without external dependencies. """ import sys import tempfile import json from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) # Import only the database and word list manager components from src.database import ( init_database, close_database, WordListRepository, session_scope ) def test_word_list_database(): """Test word list database functionality.""" print("Testing Word List Database System...") print("="*60) # Use temporary database for testing with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as tmp_db: db_path = tmp_db.name try: # Initialize database print("\n1. Initializing database...") init_database(f'sqlite:///{db_path}') print(" ✓ Database initialized") # Test repository operations with session_scope() as session: repo = WordListRepository(session) # Test 1: Create word list print("\n2. Creating word list...") word_list = repo.create( "Test List", "A test word list", "en", True ) list_id = word_list.id print(f" ✓ Created word list with ID: {list_id}") # Test 2: Add words print("\n3. Adding words...") from src.database import SeverityLevel, WordCategory words_added = 0 test_words = [ ('test1', SeverityLevel.LOW, WordCategory.PROFANITY), ('test2', SeverityLevel.MEDIUM, WordCategory.PROFANITY), ('test3', SeverityLevel.HIGH, WordCategory.SLUR) ] for word, severity, category in test_words: result = repo.add_word(list_id, word, severity, category) if result: words_added += 1 print(f" ✓ Added {words_added} words") # Test 3: Get word list print("\n4. Retrieving word list...") retrieved = repo.get_by_id(list_id) print(f" ✓ Retrieved list: {retrieved.name}") print(f" ✓ Word count: {len(retrieved.words)}") # Test 4: Export to JSON print("\n5. Exporting to JSON...") with tempfile.NamedTemporaryFile(suffix='.json', delete=False) as tmp_json: json_path = Path(tmp_json.name) success = repo.export_to_file(list_id, json_path) print(f" ✓ Exported to {json_path}: {success}") # Verify JSON content with open(json_path, 'r') as f: data = json.load(f) print(f" ✓ JSON contains {len(data['words'])} words") # Test 5: Get all lists print("\n6. Getting all word lists...") all_lists = repo.get_all() print(f" ✓ Found {len(all_lists)} total lists") # Test 6: Remove word print("\n7. Removing a word...") removed = repo.remove_word(list_id, 'test1') print(f" ✓ Word removed: {removed}") # Test 7: Update word list print("\n8. Updating word list...") updated = repo.update(list_id, description="Updated description") print(f" ✓ Updated: {updated is not None}") print("\n" + "="*60) print("✅ All database tests passed successfully!") except Exception as e: print(f"\n❌ Test failed: {e}") import traceback traceback.print_exc() return False finally: # Cleanup close_database() Path(db_path).unlink(missing_ok=True) if 'json_path' in locals(): json_path.unlink(missing_ok=True) return True if __name__ == "__main__": success = test_word_list_database() sys.exit(0 if success else 1)