diff --git a/final_documents_tests_fix.py b/final_documents_tests_fix.py deleted file mode 100644 index 2faa039..0000000 --- a/final_documents_tests_fix.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -""" -Final script to fix all remaining issues in documents_tests.rs -""" - -import re -import sys - -def fix_documents_tests(content): - """Fix all remaining issues in documents_tests.rs""" - - # Fix 1: Replace user.id() with user.user_response.id (for TestUser objects) - # This converts String to Uuid properly - content = re.sub(r'(\w+)\.id\(\)', r'\1.user_response.id', content) - - # Fix 2: Replace user.role with user.user_response.role (for TestUser objects) - content = re.sub(r'(\w+)\.role\b', r'\1.user_response.role', content) - - # Fix 3: Replace create_test_admin() with create_admin_user() - content = re.sub(r'\.create_test_admin\(\)', '.create_admin_user()', content) - - # Fix 4: Fix document.id() back to document.id (documents don't have id() method) - content = re.sub(r'(doc\w*|document\w*|result\[\d+\]|deleted_doc|found_doc\.unwrap\(\))\.user_response\.id\b', r'\1.id', content) - - # Fix 5: Fix response.id() to response.id for DocumentResponse - content = re.sub(r'response\.user_response\.id\b', 'response.id', content) - - # Fix 6: Fix any standalone .user_response.id calls that shouldn't be there - content = re.sub(r'\.user_response\.id\(\)', '.user_response.id', content) - - # Fix 7: Fix doubled "user_response" patterns - content = re.sub(r'\.user_response\.user_response\.', '.user_response.', content) - - return content - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - # Read the file - try: - with open(file_path, 'r') as f: - content = f.read() - except FileNotFoundError: - print(f"Error: Could not find file {file_path}") - return 1 - - # Apply fixes - print("Applying final fixes to documents_tests.rs...") - fixed_content = fix_documents_tests(content) - - # Write back the fixed content - try: - with open(file_path, 'w') as f: - f.write(fixed_content) - print(f"Successfully applied fixes to {file_path}") - return 0 - except Exception as e: - print(f"Error writing file: {e}") - return 1 - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file diff --git a/fix_testuser_vs_user_final.py b/fix_testuser_vs_user_final.py deleted file mode 100644 index f04823e..0000000 --- a/fix_testuser_vs_user_final.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python3 -""" -Final comprehensive fix for TestUser vs models::User distinction -""" - -import re -import sys - -def fix_user_object_types(content): - """Fix the distinction between TestUser and models::User objects""" - - lines = content.split('\n') - fixed_lines = [] - - # Track which variables are TestUser vs User objects - testuser_vars = set() - user_vars = set() - - for i, line in enumerate(lines): - # Identify TestUser variables (created by auth_helper methods) - if re.search(r'let (\w+) = auth_helper\.create_test_user\(\)', line): - var_name = re.search(r'let (\w+) = auth_helper\.create_test_user\(\)', line).group(1) - testuser_vars.add(var_name) - elif re.search(r'let (\w+) = auth_helper\.create_admin_user\(\)', line): - var_name = re.search(r'let (\w+) = auth_helper\.create_admin_user\(\)', line).group(1) - testuser_vars.add(var_name) - elif re.search(r'let (\w+) = auth_helper\.create_test_admin\(\)', line): - var_name = re.search(r'let (\w+) = auth_helper\.create_test_admin\(\)', line).group(1) - testuser_vars.add(var_name) - - # Identify models::User variables (created by db.create_user) - elif re.search(r'let (\w+) = .*db\.create_user\(', line): - var_name = re.search(r'let (\w+) = .*db\.create_user\(', line).group(1) - user_vars.add(var_name) - - # Fix the line based on variable types - fixed_line = line - - # For TestUser objects, ensure they use .user_response - for var in testuser_vars: - # Convert .id to .user_response.id for TestUser objects - fixed_line = re.sub(rf'\b{var}\.id\b', f'{var}.user_response.id', fixed_line) - # Convert .role to .user_response.role for TestUser objects - fixed_line = re.sub(rf'\b{var}\.role\b', f'{var}.user_response.role', fixed_line) - - # For models::User objects, ensure they use direct access - for var in user_vars: - # Remove .user_response for User objects - fixed_line = re.sub(rf'\b{var}\.user_response\.id\b', f'{var}.id', fixed_line) - fixed_line = re.sub(rf'\b{var}\.user_response\.role\b', f'{var}.role', fixed_line) - - fixed_lines.append(fixed_line) - - return '\n'.join(fixed_lines) - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - # Read the file - try: - with open(file_path, 'r') as f: - content = f.read() - except FileNotFoundError: - print(f"Error: Could not find file {file_path}") - return 1 - - # Apply fixes - print("Applying comprehensive TestUser vs User fixes...") - fixed_content = fix_user_object_types(content) - - # Write back the fixed content - try: - with open(file_path, 'w') as f: - f.write(fixed_content) - print(f"Successfully fixed {file_path}") - return 0 - except Exception as e: - print(f"Error writing file: {e}") - return 1 - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file diff --git a/fix_user_vs_testuser.py b/fix_user_vs_testuser.py deleted file mode 100644 index 3e21b6d..0000000 --- a/fix_user_vs_testuser.py +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env python3 -""" -Fix the distinction between models::User and TestUser objects -""" - -import re -import sys - -def fix_user_types(content): - """Fix the distinction between models::User and TestUser objects""" - - # First, find all the places where we import or create Users vs TestUsers - # and fix them appropriately - - # In the test functions, we need to identify which variables are TestUser and which are User - # Let's look for patterns that indicate TestUser creation - - # Pattern 1: Variables created from auth_helper.create_test_user() are TestUser - # Pattern 2: Variables created from auth_helper.create_admin_user() are TestUser - # Pattern 3: Variables created from auth_helper.create_test_admin() are TestUser - - # Find all test functions and fix them individually - test_functions = re.findall(r'(#\[tokio::test\].*?^ })', content, re.MULTILINE | re.DOTALL) - - for func in test_functions: - # Check if this function creates TestUser objects - if 'auth_helper.create_test_user()' in func or 'auth_helper.create_admin_user()' in func or 'auth_helper.create_test_admin()' in func: - # This function uses TestUser objects, keep .user_response - continue - else: - # This function might be using models::User objects, revert .user_response - # But only if the variable is clearly a User object - func_lines = func.split('\n') - for i, line in enumerate(func_lines): - # Look for variable declarations that create User objects - if 'create_test_user(&' in line and 'UserRole::' in line: - # This creates a models::User object - var_match = re.search(r'let (\w+) = create_test_user\(', line) - if var_match: - var_name = var_match.group(1) - # Replace .user_response with direct access for this variable - func = func.replace(f'{var_name}.user_response.id', f'{var_name}.id') - func = func.replace(f'{var_name}.user_response.role', f'{var_name}.role') - - # Apply the fixed functions back to content - # This is complex, so let's use a different approach - - # Let's be more specific about which variables are TestUser vs User - # Look for the specific patterns in the migration - - # Fix models::User objects that got incorrectly converted - # Pattern: Variables that are clearly User objects (not TestUser) - lines = content.split('\n') - in_test_function = False - current_function_uses_testuser = False - - fixed_lines = [] - - for line in lines: - if '#[tokio::test]' in line: - in_test_function = True - current_function_uses_testuser = False - elif in_test_function and line.strip() == '}': - in_test_function = False - current_function_uses_testuser = False - elif in_test_function and ('auth_helper.create_test_user()' in line or 'auth_helper.create_admin_user()' in line or 'auth_helper.create_test_admin()' in line): - current_function_uses_testuser = True - elif in_test_function and not current_function_uses_testuser: - # This function doesn't use TestUser objects, so revert .user_response - # But only for variables that are created with the old pattern - if 'create_test_user(&' in line and 'UserRole::' in line: - # This line creates a models::User object - var_match = re.search(r'let (\w+) = create_test_user\(', line) - if var_match: - var_name = var_match.group(1) - # Mark this variable as a User object - # We'll fix its usage in subsequent lines - pass - # Fix usage of User objects - line = re.sub(r'(\w+)\.user_response\.id\b', r'\1.id', line) - line = re.sub(r'(\w+)\.user_response\.role\b', r'\1.role', line) - - fixed_lines.append(line) - - return '\n'.join(fixed_lines) - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - # Read the file - try: - with open(file_path, 'r') as f: - content = f.read() - except FileNotFoundError: - print(f"Error: Could not find file {file_path}") - return 1 - - # Apply fixes - print("Fixing User vs TestUser distinction...") - fixed_content = fix_user_types(content) - - # Write back the fixed content - try: - with open(file_path, 'w') as f: - f.write(fixed_content) - print(f"Successfully fixed {file_path}") - return 0 - except Exception as e: - print(f"Error writing file: {e}") - return 1 - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file diff --git a/migrate_documents_tests.py b/migrate_documents_tests.py deleted file mode 100644 index 0169298..0000000 --- a/migrate_documents_tests.py +++ /dev/null @@ -1,166 +0,0 @@ -#!/usr/bin/env python3 -""" -Script to migrate remaining tests in documents_tests.rs to use the new TestContext pattern. -""" - -import re -import sys - -def migrate_test_file(file_path): - """Migrate the documents_tests.rs file to use new test patterns.""" - - with open(file_path, 'r') as f: - content = f.read() - - # Store the original content for comparison - original_content = content - - # 1. Remove #[ignore = "Requires PostgreSQL database"] annotations - content = re.sub(r' #\[ignore = "Requires PostgreSQL database"\]\n', '', content) - - # 2. Remove old database pool creation lines - content = re.sub(r' let pool = create_test_db_pool\(\)\.await;\n', '', content) - - # 3. Remove old Database struct creation lines - content = re.sub(r' let documents_db = Database \{ pool: pool\.clone\(\) \};\n', '', content) - - # 4. Replace old user creation with new pattern - # Handle both User and Admin role patterns - content = re.sub( - r' let user = create_test_user\(&pool, UserRole::User\)\.await;', - ' let ctx = TestContext::new().await;\n let auth_helper = TestAuthHelper::new(ctx.app.clone());\n let user = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let admin = create_test_user\(&pool, UserRole::Admin\)\.await;', - ' let admin = auth_helper.create_test_admin().await;', - content - ) - - # Handle other variations of user creation - content = re.sub( - r' let user1 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let ctx = TestContext::new().await;\n let auth_helper = TestAuthHelper::new(ctx.app.clone());\n let user1 = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let user2 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let user2 = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let tenant1_user1 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let ctx = TestContext::new().await;\n let auth_helper = TestAuthHelper::new(ctx.app.clone());\n let tenant1_user1 = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let tenant1_user2 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let tenant1_user2 = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let tenant2_user1 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let tenant2_user1 = auth_helper.create_test_user().await;', - content - ) - - content = re.sub( - r' let tenant2_user2 = create_test_user\(&pool, UserRole::User\)\.await;', - ' let tenant2_user2 = auth_helper.create_test_user().await;', - content - ) - - # 5. Replace document creation and insertion pattern - content = re.sub( - r' let ([a-zA-Z0-9_]+) = create_and_insert_test_document\(&pool, ([a-zA-Z0-9_.()]+)\)\.await;', - r' let \1_doc = create_test_document(\2);\n let \1 = ctx.state.db.create_document(\1_doc).await.expect("Failed to create document");', - content - ) - - # 6. Replace documents_db. with ctx.state.db. - content = re.sub(r'documents_db\.', 'ctx.state.db.', content) - - # 7. Replace user.id with user.id() for TestUser instances - # This is tricky because we need to be careful about which instances are TestUser vs regular User - # We'll handle this pattern by pattern based on context - - # For delete_document calls that use user.id, user.role pattern - content = re.sub( - r'\.delete_document\(([^,]+), ([a-zA-Z0-9_]+)\.id, ([a-zA-Z0-9_]+)\.role\)', - r'.delete_document(\1, \2.id(), \3.role)', - content - ) - - # For bulk_delete_documents calls - content = re.sub( - r'\.bulk_delete_documents\(([^,]+), ([a-zA-Z0-9_]+)\.id, ([a-zA-Z0-9_]+)\.role\)', - r'.bulk_delete_documents(\1, \2.id(), \3.role)', - content - ) - - # For get_document_by_id calls - content = re.sub( - r'\.get_document_by_id\(([^,]+), ([a-zA-Z0-9_]+)\.id, ([a-zA-Z0-9_]+)\.role\)', - r'.get_document_by_id(\1, \2.id(), \3.role)', - content - ) - - # For create_test_document calls - content = re.sub( - r'create_test_document\(([a-zA-Z0-9_]+)\.id\)', - r'create_test_document(\1.id())', - content - ) - - # For bind calls in SQL - content = re.sub( - r'\.bind\(([a-zA-Z0-9_]+)\.id\)', - r'.bind(\1.id())', - content - ) - - # For let user_id assignments - content = re.sub( - r' let user_id = ([a-zA-Z0-9_]+)\.id;', - r' let user_id = \1.id();', - content - ) - - # Add missing imports if TestContext/TestAuthHelper aren't already imported - # Check if the imports are present - if 'use crate::test_utils::{TestContext, TestAuthHelper};' not in content: - # Find the existing test_utils import and update it - content = re.sub( - r'use crate::test_utils::TestContext;', - 'use crate::test_utils::{TestContext, TestAuthHelper};', - content - ) - - # Check if we made any changes - if content != original_content: - return content - else: - return None - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - print("Starting migration of documents_tests.rs...") - - migrated_content = migrate_test_file(file_path) - - if migrated_content: - # Write the migrated content back - with open(file_path, 'w') as f: - f.write(migrated_content) - print("Migration completed successfully!") - else: - print("No changes needed - file is already migrated or no patterns found.") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/migrate_documents_tests_v2.py b/migrate_documents_tests_v2.py deleted file mode 100644 index b7c80a8..0000000 --- a/migrate_documents_tests_v2.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python3 -""" -Enhanced script to migrate remaining tests in documents_tests.rs to use the new TestContext pattern. -""" - -import re -import sys - -def migrate_test_file(file_path): - """Migrate the documents_tests.rs file to use new test patterns.""" - - with open(file_path, 'r') as f: - content = f.read() - - # Store the original content for comparison - original_content = content - - # Fix remaining documents_db references that were missed - content = re.sub(r' let result = documents_db', ' let result = ctx.state.db', content) - content = re.sub(r' let result2 = documents_db', ' let result2 = ctx.state.db', content) - - # Fix any remaining documents_db references in method calls - content = re.sub(r'documents_db\n', 'ctx.state.db\n', content) - - # Fix variable naming from the document creation pattern - # The regex replacement created variables like user_doc_doc, let's fix those - content = re.sub(r' let ([a-zA-Z0-9_]+)_doc_doc = create_test_document\(([^)]+)\);', - r' let \1_doc = create_test_document(\2);', content) - - # Check if we made any changes - if content != original_content: - return content - else: - return None - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - print("Starting enhanced migration of documents_tests.rs...") - - migrated_content = migrate_test_file(file_path) - - if migrated_content: - # Write the migrated content back - with open(file_path, 'w') as f: - f.write(migrated_content) - print("Enhanced migration completed successfully!") - else: - print("No changes needed - file is already migrated or no patterns found.") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/migrate_documents_tests_v3.py b/migrate_documents_tests_v3.py deleted file mode 100644 index bb5327b..0000000 --- a/migrate_documents_tests_v3.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 -""" -Final cleanup script to fix variable naming issues from the migration. -""" - -import re -import sys - -def migrate_test_file(file_path): - """Clean up variable naming issues from the migration.""" - - with open(file_path, 'r') as f: - content = f.read() - - # Store the original content for comparison - original_content = content - - # Fix the doubled variable names created by the regex - content = re.sub(r' let ([a-zA-Z0-9_]+)_doc_doc = create_test_document\(([^)]+)\);', - r' let \1_doc = create_test_document(\2);', content) - - # Also fix any references to these variables in the same context - content = re.sub(r'create_document\(([a-zA-Z0-9_]+)_doc_doc\)', - r'create_document(\1_doc)', content) - - # Check if we made any changes - if content != original_content: - return content - else: - return None - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - print("Starting final cleanup of documents_tests.rs...") - - migrated_content = migrate_test_file(file_path) - - if migrated_content: - # Write the migrated content back - with open(file_path, 'w') as f: - f.write(migrated_content) - print("Final cleanup completed successfully!") - else: - print("No changes needed - file is already clean.") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/migrate_tests_fix.py b/migrate_tests_fix.py deleted file mode 100644 index 8e9d5cf..0000000 --- a/migrate_tests_fix.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env python3 -""" -Enhanced migration script to fix remaining issues in documents_tests.rs -""" - -import re -import sys - -def migrate_remaining_issues(content): - """Fix remaining issues from the bulk migration""" - - # Fix remaining pool references - content = re.sub(r'\.execute\(&pool\)', '.execute(&ctx.state.db.pool)', content) - - # Fix Database::new patterns - replace with TestContext - database_new_pattern = r'let database = Database::new\(&connection_string\)\.await\.unwrap\(\);' - database_new_replacement = 'let ctx = TestContext::new().await;\n let database = &ctx.state.db;' - content = re.sub(database_new_pattern, database_new_replacement, content) - - # Also handle the variable name 'database' in subsequent lines - # Replace database. with ctx.state.db. only in test functions - content = re.sub(r'\bdatabase\.', 'ctx.state.db.', content) - - # Fix cases where we have ctx declared multiple times in the same function - # This is a more complex pattern - let's fix it by ensuring we only declare ctx once per function - - # Find functions with multiple ctx declarations and fix them - def fix_multiple_ctx(match): - func_content = match.group(0) - # Count ctx declarations - ctx_count = len(re.findall(r'let ctx = TestContext::new\(\)\.await;', func_content)) - if ctx_count > 1: - # Keep only the first one, replace others with comments - first_done = False - def replace_ctx(ctx_match): - nonlocal first_done - if not first_done: - first_done = True - return ctx_match.group(0) - else: - return '// let ctx = TestContext::new().await; // Already declared above' - func_content = re.sub(r'let ctx = TestContext::new\(\)\.await;', replace_ctx, func_content) - return func_content - - # Apply this to each test function - func_pattern = r'#\[tokio::test\][^}]*?(?=\n #\[tokio::test\]|\n}\n|\Z)' - content = re.sub(func_pattern, fix_multiple_ctx, content, flags=re.MULTILINE | re.DOTALL) - - return content - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - # Read the file - try: - with open(file_path, 'r') as f: - content = f.read() - except FileNotFoundError: - print(f"Error: Could not find file {file_path}") - return 1 - - # Apply additional fixes - print("Applying additional migration fixes...") - migrated_content = migrate_remaining_issues(content) - - # Write back the migrated content - try: - with open(file_path, 'w') as f: - f.write(migrated_content) - print(f"Successfully applied fixes to {file_path}") - return 0 - except Exception as e: - print(f"Error writing file: {e}") - return 1 - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file diff --git a/precise_testuser_fix.py b/precise_testuser_fix.py deleted file mode 100644 index 7dea1d2..0000000 --- a/precise_testuser_fix.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python3 -""" -Precise fix for TestUser field access based on variable creation patterns -""" - -import re -import sys - -def fix_testuser_access(content): - """Fix TestUser objects to use proper .user_response field access""" - - lines = content.split('\n') - fixed_lines = [] - - # Track which variables are TestUser objects within each function - current_testuser_vars = set() - in_function = False - - for line in lines: - # Reset when entering a new function - if re.match(r'\s*#\[tokio::test\]', line) or re.match(r'\s*async fn ', line): - current_testuser_vars.clear() - in_function = True - elif re.match(r'^\s*}$', line) and in_function: - in_function = False - current_testuser_vars.clear() - - # Track TestUser variable declarations - if in_function: - # Variables created by auth_helper methods are TestUser - testuser_match = re.search(r'let (\w+) = auth_helper\.(?:create_test_user|create_admin_user|create_test_admin)\(\)', line) - if testuser_match: - var_name = testuser_match.group(1) - current_testuser_vars.add(var_name) - print(f"Found TestUser variable: {var_name}") - - # Fix field access for known TestUser variables - fixed_line = line - for var_name in current_testuser_vars: - # Replace .id with .user_response.id for TestUser objects - fixed_line = re.sub(rf'\b{var_name}\.id\b', f'{var_name}.user_response.id', fixed_line) - # Replace .role with .user_response.role for TestUser objects - fixed_line = re.sub(rf'\b{var_name}\.role\b', f'{var_name}.user_response.role', fixed_line) - - fixed_lines.append(fixed_line) - - return '\n'.join(fixed_lines) - -def main(): - file_path = '/root/repos/readur/src/tests/documents_tests.rs' - - # Read the file - try: - with open(file_path, 'r') as f: - content = f.read() - except FileNotFoundError: - print(f"Error: Could not find file {file_path}") - return 1 - - # Apply fixes - print("Applying precise TestUser field access fixes...") - fixed_content = fix_testuser_access(content) - - # Write back the fixed content - try: - with open(file_path, 'w') as f: - f.write(fixed_content) - print(f"Successfully fixed {file_path}") - return 0 - except Exception as e: - print(f"Error writing file: {e}") - return 1 - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file