Commit 59a930ba authored by Konstantin Schulz's avatar Konstantin Schulz

fixed database access for unit tests

parent c9de521d
Pipeline #11379 passed with stage
in 45 seconds
......@@ -31,7 +31,7 @@ class DatabaseService:
@staticmethod
def init_db_alembic() -> None:
""" In Docker, the alembic version is not initially written to the database, so we need to set it manually. """
if not db.engine.dialect.has_table(db.engine, "alembic_version"):
if not db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_ALEMBIC):
stamp(directory=Config.MIGRATIONS_DIRECTORY)
upgrade(directory=Config.MIGRATIONS_DIRECTORY)
......
......@@ -58,6 +58,7 @@ class Config(object):
CUSTOM_CORPUS_VIVA_URN = "urn:custom:latinLit:viva.lat"
CUSTOM_CORPUS_PROIEL_URN_TEMPLATE = "urn:custom:latinLit:proiel.{0}.lat"
DATABASE_LOCAL_URL = "postgresql://postgres@0.0.0.0:5432/postgres"
DATABASE_TABLE_ALEMBIC = "alembic_version"
DATABASE_URL = os.environ.get("DATABASE_URL",
("postgresql://postgres@db:5432/" if IS_DOCKER else
"postgresql://0.0.0.0:5432/postgres"))
......
......@@ -41,7 +41,7 @@ setup(
url="https://scm.cms.hu-berlin.de/callidus/mc-backend",
# Version number (initial):
version="1.8.2",
version="1.8.3",
zip_safe=False, install_requires=['flask', 'graphannis']
)
......@@ -487,13 +487,14 @@ class McTestCase(unittest.TestCase):
def test_create_app(self):
"""Creates a new Flask application and configures it. Initializes the application and the database."""
old_uri: str = TestingConfig.SQLALCHEMY_DATABASE_URI
with patch.object(sys, "argv", [None, None, Config.FLASK_MIGRATE]):
cfg: Type[Config] = TestingConfig
create_app(cfg)
self.assertEqual(cfg.SQLALCHEMY_DATABASE_URI, Config.DATABASE_LOCAL_URL)
with patch.object(mcserver.app, "init_app_common", return_value=Flask(__name__)):
cfg: Type[Config] = TestingConfig
old_uri: str = cfg.SQLALCHEMY_DATABASE_URI
create_app(cfg)
self.assertEqual(cfg.SQLALCHEMY_DATABASE_URI, Config.DATABASE_LOCAL_URL)
cfg.SQLALCHEMY_DATABASE_URI = old_uri
self.app_context.push()
TestingConfig.SQLALCHEMY_DATABASE_URI = old_uri
def test_init_corpus_storage_manager(self):
""" Initializes the corpus storage manager. """
......@@ -976,6 +977,13 @@ class CommonTestCase(unittest.TestCase):
cc: CustomCorpus = CustomCorpusService.init_custom_corpus(CustomCorpusService.custom_corpora[0])
self.assertEqual(len(cc.text_parts), 1)
def test_init_db_alembic(self):
""" In Docker, the alembic version is not initially written to the database, so we need to set it manually. """
db.engine.execute(f"DROP TABLE {Config.DATABASE_TABLE_ALEMBIC}")
self.assertEqual(db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_ALEMBIC), False)
DatabaseService.init_db_alembic()
self.assertEqual(db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_ALEMBIC), True)
def test_init_db_corpus(self):
"""Initializes the corpus table."""
cc: CustomCorpus = CustomCorpusService.custom_corpora[0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment