From 937eb7a9759b84f426d6ba75facec6865efebb63 Mon Sep 17 00:00:00 2001
From: Konstantin Schulz <schulzkx@hu-berlin.de>
Date: Wed, 15 Jul 2020 10:45:57 +0200
Subject: [PATCH] backend CI should now fail if any unit test fails

---
 .gitlab-ci.yml                        |  2 +-
 coverage_local.sh                     |  4 ++--
 mc_backend/.coveragerc                |  1 +
 mc_backend/coverage_backend.sh        |  4 ++++
 mc_backend/mcserver/app/__init__.py   | 20 ++++++++++++--------
 mc_backend/mcserver/config.py         |  2 ++
 mc_frontend/src/app/helper.service.ts |  2 +-
 7 files changed, 23 insertions(+), 12 deletions(-)
 create mode 100755 mc_backend/coverage_backend.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index fe592d8..b7a9c8f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -17,7 +17,7 @@ ci_frontend:
 ci_backend:
   stage: ci
   script:
-    - docker-compose run --rm mcserver bash -c "source ../venv/bin/activate && coverage run --rcfile=.coveragerc tests.py && coverage combine && coverage report -m" > ci_backend.log
+    - docker-compose run --rm --entrypoint="./coverage_backend.sh" mcserver > ci_backend.log
   artifacts:
     paths:
       - ci_backend.log
diff --git a/coverage_local.sh b/coverage_local.sh
index ae86b12..5b398e7 100755
--- a/coverage_local.sh
+++ b/coverage_local.sh
@@ -1,5 +1,5 @@
 docker-compose build
-docker-compose run --rm --entrypoint="npm run test-ci" mc_frontend > ci_frontend.log
-docker-compose run --rm mcserver bash -c "source ../venv/bin/activate && coverage run --rcfile=.coveragerc tests.py && coverage combine && coverage report -m" > ci_backend.log
+docker-compose run --rm --entrypoint="npm run test-ci" mc_frontend >ci_frontend.log
+docker-compose run --rm --entrypoint="./coverage_backend.sh" mcserver > ci_backend.log
 ./coverage_ci.sh
 cat coverage.log
diff --git a/mc_backend/.coveragerc b/mc_backend/.coveragerc
index 652a2c4..55b70aa 100644
--- a/mc_backend/.coveragerc
+++ b/mc_backend/.coveragerc
@@ -17,5 +17,6 @@ exclude_lines =
     # Don't complain if non-runnable code isn't run:
     if __name__ == .__main__.:
     except ModuleNotFoundError:
+fail_under = 100
 ignore_errors = True
 show_missing = True
diff --git a/mc_backend/coverage_backend.sh b/mc_backend/coverage_backend.sh
new file mode 100755
index 0000000..c23bbe3
--- /dev/null
+++ b/mc_backend/coverage_backend.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+source ../venv/bin/activate
+coverage run --rcfile=.coveragerc tests.py
+coverage combine && coverage report -m
diff --git a/mc_backend/mcserver/app/__init__.py b/mc_backend/mcserver/app/__init__.py
index 38857e4..5f27b2a 100644
--- a/mc_backend/mcserver/app/__init__.py
+++ b/mc_backend/mcserver/app/__init__.py
@@ -18,14 +18,6 @@ from flask_sqlalchemy import SQLAlchemy
 from open_alchemy import init_yaml
 from mcserver.config import Config
 
-# remove stale connections from the connection pool after 1 hour
-db: SQLAlchemy = SQLAlchemy()  # session_options={"autocommit": True, "pool_recycle": 3600}
-migrate: Migrate = Migrate(directory=Config.MIGRATIONS_DIRECTORY)
-if not hasattr(open_alchemy.models, Config.DATABASE_TABLE_CORPUS):
-    # do this _BEFORE_ you add any APIs to your application
-    init_yaml(Config.API_SPEC_MODELS_YAML_FILE_PATH, base=db.Model,
-              models_filename=os.path.join(Config.MC_SERVER_DIRECTORY, "models_auto.py"))
-
 
 def apply_event_handlers(app: FlaskApp):
     """Applies event handlers to a given Flask application, such as logging after requests or teardown logic."""
@@ -64,6 +56,11 @@ def create_app(cfg: Type[Config] = Config) -> Flask:
     return app
 
 
+def create_database() -> SQLAlchemy:
+    """Creates a new connection to a database, which will handle all future database transactions."""
+    return SQLAlchemy()  # session_options={"autocommit": True}
+
+
 def full_init(app: Flask, cfg: Type[Config] = Config) -> None:
     """ Fully initializes the application, including logging."""
     from mcserver.app.services import DatabaseService
@@ -145,6 +142,13 @@ def shutdown_session(exception=None):
     db.session.remove()
 
 
+db: SQLAlchemy = create_database()
+migrate: Migrate = Migrate(directory=Config.MIGRATIONS_DIRECTORY)
+if not hasattr(open_alchemy.models, Config.DATABASE_TABLE_CORPUS):
+    # do this _BEFORE_ you add any APIs to your application
+    init_yaml(Config.API_SPEC_MODELS_YAML_FILE_PATH, base=db.Model,
+              models_filename=os.path.join(Config.MC_SERVER_DIRECTORY, "models_auto.py"))
+
 # import the models so we can access them from other parts of the app using imports from "app.models";
 # this has to be at the bottom of the file
 from mcserver.app import models
diff --git a/mc_backend/mcserver/config.py b/mc_backend/mcserver/config.py
index 13191ba..0a52670 100644
--- a/mc_backend/mcserver/config.py
+++ b/mc_backend/mcserver/config.py
@@ -145,6 +145,8 @@ class Config(object):
     SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or DATABASE_URL_SQLITE
     # BEWARE: if True, logs every single database statement executed by this application to STDOUT
     SQLALCHEMY_ECHO = False
+    # remove stale connections from the connection pool after 1 hour
+    SQLALCHEMY_POOL_RECYCLE = 3600
     SQLALCHEMY_TRACK_MODIFICATIONS = False
     STATIC_EXERCISES_REPOSITORY_URL = "https://scm.cms.hu-berlin.de/callidus/machina-callida/-/archive/master/machina-callida-master.zip?path=mc_frontend%2Fsrc%2Fassets%2Fh5p"
     STOP_WORDS_LATIN_PATH = os.path.join(CACHE_DIRECTORY, "stop_words_latin.json")
diff --git a/mc_frontend/src/app/helper.service.ts b/mc_frontend/src/app/helper.service.ts
index b5cfab6..84a9cd3 100644
--- a/mc_frontend/src/app/helper.service.ts
+++ b/mc_frontend/src/app/helper.service.ts
@@ -259,7 +259,7 @@ export class HelperService {
             configMC.backendBaseUrl = part1.concat(configMC.backendBaseApiPath).concat('/');
         }
         if (!configMC.frontendBaseUrl) {
-            configMC.frontendBaseUrl = location.href.substring(0, location.href.lastIndexOf('/') + 1);
+            configMC.frontendBaseUrl = location.href.substring(0, location.href.lastIndexOf('/'));
         }
     }
 
-- 
GitLab