test_migration.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. version = "installed (version not available)"
  24. print(f"✓ Alembic installed: {version}")
  25. assert True
  26. except ImportError:
  27. print("✗ Alembic NOT installed")
  28. print(" Run: pip install -r requirements.txt")
  29. raise AssertionError("Alembic is not installed")
  30. def test_alembic_config():
  31. """Verify that alembic.ini exists."""
  32. alembic_ini = project_root / "alembic.ini"
  33. assert alembic_ini.exists(), "alembic.ini file not found"
  34. print("✓ alembic.ini file found")
  35. def test_alembic_structure():
  36. """Verify the Alembic directory structure."""
  37. checks = {
  38. "alembic/": "Alembic directory",
  39. "alembic/env.py": "env.py file",
  40. "alembic/script.py.mako": "script.py.mako template",
  41. "alembic/versions/": "versions directory",
  42. "alembic/versions/001_initial_schema.py": "Initial migration",
  43. "alembic/versions/002_expand_varchar_columns.py": "VARCHAR migration",
  44. }
  45. all_ok = True
  46. for path, description in checks.items():
  47. full_path = project_root / path
  48. if full_path.exists():
  49. print(f"✓ {description}")
  50. else:
  51. print(f"✗ {description} NOT found")
  52. all_ok = False
  53. assert all_ok, "One or more Alembic structure checks failed"
  54. def test_database_module():
  55. """Verify that the database module can be imported."""
  56. try:
  57. from database.database import get_database_url
  58. print("✓ database.database module imported successfully")
  59. # Try to get database URL (doesn't connect, just builds URL)
  60. try:
  61. url = get_database_url()
  62. print(f"✓ Database URL: {url[:50]}...")
  63. except Exception as e:
  64. print(f"⚠ Warning getting URL: {e}")
  65. assert True
  66. except ImportError:
  67. print("⚠ Cannot import database (missing dependencies)")
  68. print(" Run: pip install -r requirements.txt")
  69. assert True
  70. except Exception as e:
  71. print(f"✗ Error importing database: {e}")
  72. raise AssertionError(f"Error importing database: {e}")
  73. def test_manage_db_script():
  74. """Verify that manage_db.py exists and is executable."""
  75. manage_db = project_root / "manage_db.py"
  76. if not manage_db.exists():
  77. print("✗ manage_db.py NOT found")
  78. raise AssertionError("manage_db.py not found")
  79. print("✓ manage_db.py script found")
  80. # Check if executable (Linux/Mac)
  81. if os.name != "nt":
  82. if os.access(manage_db, os.X_OK):
  83. print("✓ manage_db.py is executable")
  84. else:
  85. print("⚠ manage_db.py is not executable (run: chmod +x manage_db.py)")
  86. assert True
  87. def main():
  88. """Run all tests."""
  89. print("=" * 60)
  90. print("ALEMBIC MIGRATION VERIFICATION")
  91. print("=" * 60)
  92. print()
  93. tests = [
  94. ("Alembic Installation", test_alembic_installation),
  95. ("Alembic Configuration", test_alembic_config),
  96. ("Alembic Structure", test_alembic_structure),
  97. ("Database Module", test_database_module),
  98. ("Management Script", test_manage_db_script),
  99. ]
  100. results = []
  101. for name, test_func in tests:
  102. print(f"\n[{name}]")
  103. print("-" * 60)
  104. try:
  105. result = test_func()
  106. results.append(result)
  107. except Exception as e:
  108. print(f"✗ Unexpected error: {e}")
  109. results.append(False)
  110. print("\n" + "=" * 60)
  111. print("SUMMARY")
  112. print("=" * 60)
  113. passed = sum(results)
  114. total = len(results)
  115. print(f"\nTests passed: {passed}/{total}")
  116. if all(results):
  117. print("\n✓ EVERYTHING IS READY!")
  118. print("\nNext steps:")
  119. print(" 1. If you have an existing DB: python manage_db.py init")
  120. print(" 2. Start the application: python app.py")
  121. return 0
  122. else:
  123. print("\n✗ SOME VERIFICATIONS FAILED")
  124. print("\nPlease check the errors above and fix them.")
  125. return 1
  126. if __name__ == "__main__":
  127. sys.exit(main())