test_migration.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env python3
  2. """
  3. Test script to verify the migration to Alembic.
  4. This script performs basic checks to ensure that:
  5. 1. Alembic is correctly installed
  6. 2. The configuration is valid
  7. 3. The migration files exist
  8. 4. The migrate_database() function works correctly
  9. """
  10. import os
  11. import sys
  12. from pathlib import Path
  13. # Add project root to path
  14. project_root = Path(__file__).parent.parent
  15. sys.path.insert(0, str(project_root))
  16. def test_alembic_installation():
  17. """Verify that Alembic is installed."""
  18. try:
  19. import alembic
  20. try:
  21. version = alembic.__version__
  22. except AttributeError:
  23. # Older versions might not have __version__
  24. version = "installed (version not available)"
  25. print(f"✓ Alembic installed: {version}")
  26. return True
  27. except ImportError:
  28. print("✗ Alembic NOT installed")
  29. print(" Run: pip install -r requirements.txt")
  30. return False
  31. def test_alembic_config():
  32. """Verify that alembic.ini exists."""
  33. alembic_ini = project_root / "alembic.ini"
  34. if alembic_ini.exists():
  35. print("✓ alembic.ini file found")
  36. return True
  37. else:
  38. print("✗ alembic.ini file NOT found")
  39. return False
  40. def test_alembic_structure():
  41. """Verify the Alembic directory structure."""
  42. checks = {
  43. "alembic/": "Alembic directory",
  44. "alembic/env.py": "env.py file",
  45. "alembic/script.py.mako": "script.py.mako template",
  46. "alembic/versions/": "versions directory",
  47. "alembic/versions/001_initial_schema.py": "Initial migration",
  48. "alembic/versions/002_expand_varchar_columns.py": "VARCHAR migration",
  49. }
  50. all_ok = True
  51. for path, description in checks.items():
  52. full_path = project_root / path
  53. if full_path.exists():
  54. print(f"✓ {description}")
  55. else:
  56. print(f"✗ {description} NOT found")
  57. all_ok = False
  58. return all_ok
  59. def test_database_module():
  60. """Verify that the database module can be imported."""
  61. try:
  62. from database.database import get_database_url
  63. print("✓ database.database module imported successfully")
  64. # Try to get database URL (doesn't connect, just builds URL)
  65. try:
  66. url = get_database_url()
  67. print(f"✓ Database URL: {url[:50]}...")
  68. except Exception as e:
  69. print(f"⚠ Warning getting URL: {e}")
  70. return True
  71. except ImportError:
  72. print("⚠ Cannot import database (missing dependencies)")
  73. print(" Run: pip install -r requirements.txt")
  74. # This is not a failure if dependencies aren't installed yet
  75. return True
  76. except Exception as e:
  77. print(f"✗ Error importing database: {e}")
  78. return False
  79. def test_manage_db_script():
  80. """Verify that manage_db.py exists and is executable."""
  81. manage_db = project_root / "manage_db.py"
  82. if not manage_db.exists():
  83. print("✗ manage_db.py NOT found")
  84. return False
  85. print("✓ manage_db.py script found")
  86. # Check if executable (Linux/Mac)
  87. if os.name != "nt":
  88. if os.access(manage_db, os.X_OK):
  89. print("✓ manage_db.py is executable")
  90. else:
  91. print("⚠ manage_db.py is not executable (run: chmod +x manage_db.py)")
  92. return True
  93. def main():
  94. """Run all tests."""
  95. print("=" * 60)
  96. print("ALEMBIC MIGRATION VERIFICATION")
  97. print("=" * 60)
  98. print()
  99. tests = [
  100. ("Alembic Installation", test_alembic_installation),
  101. ("Alembic Configuration", test_alembic_config),
  102. ("Alembic Structure", test_alembic_structure),
  103. ("Database Module", test_database_module),
  104. ("Management Script", test_manage_db_script),
  105. ]
  106. results = []
  107. for name, test_func in tests:
  108. print(f"\n[{name}]")
  109. print("-" * 60)
  110. try:
  111. result = test_func()
  112. results.append(result)
  113. except Exception as e:
  114. print(f"✗ Unexpected error: {e}")
  115. results.append(False)
  116. print("\n" + "=" * 60)
  117. print("SUMMARY")
  118. print("=" * 60)
  119. passed = sum(results)
  120. total = len(results)
  121. print(f"\nTests passed: {passed}/{total}")
  122. if all(results):
  123. print("\n✓ EVERYTHING IS READY!")
  124. print("\nNext steps:")
  125. print(" 1. If you have an existing DB: python manage_db.py init")
  126. print(" 2. Start the application: python app.py")
  127. return 0
  128. else:
  129. print("\n✗ SOME VERIFICATIONS FAILED")
  130. print("\nPlease check the errors above and fix them.")
  131. return 1
  132. if __name__ == "__main__":
  133. sys.exit(main())