test_table_creation.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import sys
  3. from pathlib import Path
  4. # add the parent directory to the system path
  5. sys.path.append(str(Path(__file__).resolve().parent.parent))
  6. import unittest
  7. from sqlalchemy import inspect
  8. from database.database import Base, get_engine, get_table_names
  9. from parsers.log import DatabaseManager
  10. class TestTableCreation(unittest.TestCase):
  11. def setUp(self):
  12. # Set environment variables for in-memory SQLite
  13. os.environ["DATABASE_TYPE"] = "SQLITE"
  14. os.environ["DATABASE_STRING_CONNECTION"] = ":memory:"
  15. self.engine = get_engine()
  16. self.connection = self.engine.connect()
  17. self.transaction = self.connection.begin()
  18. self.session = DatabaseManager(engine=self.engine).session
  19. def tearDown(self):
  20. self.session.close()
  21. self.transaction.rollback()
  22. self.connection.close()
  23. def test_table_creation(self):
  24. # Drop all tables to simulate a fresh database
  25. Base.metadata.drop_all(self.engine)
  26. # Verify tables do not exist initially
  27. inspector = inspect(self.engine)
  28. current_tables = inspector.get_table_names()
  29. table_names = get_table_names()
  30. for table in table_names:
  31. self.assertNotIn(table, current_tables)
  32. # Trigger table creation logic usando el mismo engine
  33. with DatabaseManager(engine=self.engine):
  34. pass
  35. # Refresh the inspector to get the latest table names
  36. inspector = inspect(self.engine)
  37. current_tables = inspector.get_table_names()
  38. for table in table_names:
  39. self.assertIn(table, current_tables)
  40. if __name__ == "__main__":
  41. unittest.main()