Correct unit tests

This commit is contained in:
Guille 2024-03-20 09:46:12 +01:00
parent 56538e803b
commit f91c10cc70
2 changed files with 12 additions and 16 deletions

View File

@ -3,3 +3,4 @@ python-dotenv
SQLAlchemy SQLAlchemy
cerc-hub cerc-hub
psycopg2-binary psycopg2-binary
sqlalchemy_utils

View File

@ -14,9 +14,12 @@ import unittest
from pathlib import Path from pathlib import Path
from unittest import TestCase from unittest import TestCase
import psycopg2
import sqlalchemy.exc import sqlalchemy.exc
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.exc import ProgrammingError from sqlalchemy.exc import ProgrammingError
from sqlalchemy_utils import database_exists, create_database, drop_database
import hub.helpers.constants as cte import hub.helpers.constants as cte
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
@ -54,22 +57,14 @@ class Control:
dotenv_path = str(dotenv_path) dotenv_path = str(dotenv_path)
repository = Repository(db_name='test_db', app_env='TEST', dotenv_path=dotenv_path) repository = Repository(db_name='test_db', app_env='TEST', dotenv_path=dotenv_path)
engine = create_engine(repository.configuration.connection_string) engine = create_engine(repository.configuration.connection_string)
try: if database_exists(engine.url):
# delete test database if it exists drop_database(engine.url)
connection = engine.connect() create_database(engine.url)
connection.close() Application.__table__.create(bind=engine, checkfirst=True)
except ProgrammingError: User.__table__.create(bind=engine, checkfirst=True)
logging.info('Database does not exist. Nothing to delete') City.__table__.create(bind=engine, checkfirst=True)
except sqlalchemy.exc.OperationalError as operational_error: CityObject.__table__.create(bind=engine, checkfirst=True)
self._skip_test = True SimulationResults.__table__.create(bind=engine, checkfirst=True)
self._skip_reason = f'{operational_error}'
return
Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__table__.create(bind=repository.engine, checkfirst=True)
SimulationResults.__table__.create(bind=repository.engine, checkfirst=True)
city_file = Path('tests_data/test.geojson').resolve() city_file = Path('tests_data/test.geojson').resolve()
output_path = Path('tests_outputs/').resolve() output_path = Path('tests_outputs/').resolve()