From 5805ab7b8e0fcc382b2260845144023b9506a39b Mon Sep 17 00:00:00 2001
From: thomaswoehlke <thomas.woehlke@gmail.com>
Date: Sat, 20 Feb 2021 19:56:12 +0100
Subject: [PATCH] working for 0.0.198 Release

---
 src/config.py                                 |  3 +
 src/database.py                               | 56 ++++++++++++++++---
 tests/conftest.py                             | 13 ++---
 .../blueprints/admin/test_admin_views.py      | 10 ++--
 .../__init__.py}                              |  0
 tests/covid19/blueprints/owid/test_flaskr.py  | 24 ++++++++
 .../covid19/blueprints/who/test_who_views.py  | 27 ++++-----
 tests/mq.py                                   | 14 +++++
 tests/test_flaskr.py                          | 12 ----
 tests/web.py                                  | 12 ++++
 10 files changed, 123 insertions(+), 48 deletions(-)
 rename tests/covid19/blueprints/{application/test_application_views.py => owid/__init__.py} (100%)
 create mode 100644 tests/covid19/blueprints/owid/test_flaskr.py
 create mode 100644 tests/mq.py
 delete mode 100644 tests/test_flaskr.py
 create mode 100644 tests/web.py

diff --git a/src/config.py b/src/config.py
index a067608f..5f556038 100644
--- a/src/config.py
+++ b/src/config.py
@@ -1,12 +1,15 @@
 SECRET_KEY = 'vfdjv423ndf654&%%'
 CELERY_BROKER_URL = 'redis://localhost:6379/0'
 MY_CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
+TEST_CELERY_BROKER_URL = 'redis://localhost:6379/1'
+TEST_MY_CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
 CELERY_CONF_WORKER_SEND_TASK_EVENTS = True
 CELERY_CONF_TASK_SEND_SENT_EVENT = True
 SQLALCHEMY_POSTGRES_USER = 'covid19data'
 SQLALCHEMY_POSTGRES_PW = 'covid19datapwd'
 SQLALCHEMY_POSTGRES_URL = 'localhost'
 SQLALCHEMY_POSTGRES_DB = 'covid19data'
+SQLALCHEMY_POSTGRES_DB_TEST = 'covid19datatest'
 SQLALCHEMY_ITEMS_PER_PAGE = 10
 SQLALCHEMY_TRACK_MODIFICATIONS = True
 FLASK_ADMIN_SWATCH = 'superhero'
diff --git a/src/database.py b/src/database.py
index aca3a323..cb9aeef3 100644
--- a/src/database.py
+++ b/src/database.py
@@ -8,7 +8,7 @@ from celery import Celery
 
 
 def create_app():
-    my_app = Flask('app')
+    my_app = Flask('covid19')
     CORS(my_app)
     Bootstrap(my_app)
     my_app.config.from_object("config")
@@ -23,6 +23,33 @@ def create_app():
     return my_app
 
 
+def create_app_test():
+    my_app = Flask('covid19test')
+    CORS(my_app)
+    Bootstrap(my_app)
+    my_app.config.from_object("config")
+    db_url_test = 'postgresql+psycopg2://{user}:{pw}@{url}/{db}'.format(
+        user=my_app.config['SQLALCHEMY_POSTGRES_USER'],
+        pw=my_app.config['SQLALCHEMY_POSTGRES_PW'],
+        url=my_app.config['SQLALCHEMY_POSTGRES_URL'],
+        db=my_app.config['SQLALCHEMY_POSTGRES_DB_TEST'])
+    my_app.config['SQLALCHEMY_DATABASE_URI'] = db_url_test
+    my_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # silence the deprecation warning
+    my_app.config['FLASK_ADMIN_SWATCH'] = 'superhero'
+    return my_app
+
+
+def create_db(my_app):
+    my_db = SQLAlchemy(my_app)
+    my_db.create_all()
+    return my_db
+
+
+def create_db_test(my_app):
+    my_db = SQLAlchemy(my_app)
+    return my_db
+
+
 def create_celery(my_app):
     celery = Celery(
         my_app.import_name,
@@ -44,6 +71,27 @@ def create_celery(my_app):
     return celery
 
 
+def create_celery_test(my_app):
+    celery = Celery(
+        my_app.import_name,
+        #namespace="covid19",
+        backend=my_app.config['TEST_MY_CELERY_RESULT_BACKEND'],
+        broker=my_app.config['TEST_CELERY_BROKER_URL'],
+        worker_send_task_events=my_app.config['CELERY_CONF_WORKER_SEND_TASK_EVENTS'],
+        task_send_sent_event=my_app.config['CELERY_CONF_TASK_SEND_SENT_EVENT'],
+        broker_transport_options={'visibility_timeout': 18000, 'max_retries': 5}
+    )
+    celery.conf.update(my_app.config)
+
+    class ContextTask(celery.Task):
+        def __call__(self, *args, **kwargs):
+            with my_app.app_context():
+                return self.run(*args, **kwargs)
+
+    celery.Task = ContextTask
+    return celery
+
+
 def create_admin(my_app):
     my_admin = Admin(
         my_app,
@@ -52,12 +100,6 @@ def create_admin(my_app):
     return my_admin
 
 
-def create_db(my_app):
-    my_db = SQLAlchemy(my_app)
-    my_db.create_all()
-    return my_db
-
-
 app = create_app()
 db = create_db(app)
 admin = create_admin(app)
diff --git a/tests/conftest.py b/tests/conftest.py
index d76e140a..b9dc84a9 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,29 +1,24 @@
 import os
 import tempfile
 import pytest
-from database import create_app, create_db
+from database import create_app_test, create_db
 
 
 @pytest.fixture
 def app():
-    app = create_app()
+    app = create_app_test()
     return app
 
 
-@pytest.fixture
-def db():
-    db = create_db(app())
-    return db
-
-
 @pytest.fixture
 def client():
+    app = create_app_test()
     db_fd, app.config['DATABASE'] = tempfile.mkstemp()
     app.config['TESTING'] = True
 
     with app.test_client() as client:
         with app.app_context():
-            db(app)
+            create_db(app)
         yield client
 
     os.close(db_fd)
diff --git a/tests/covid19/blueprints/admin/test_admin_views.py b/tests/covid19/blueprints/admin/test_admin_views.py
index 0ebe7868..5006fe04 100644
--- a/tests/covid19/blueprints/admin/test_admin_views.py
+++ b/tests/covid19/blueprints/admin/test_admin_views.py
@@ -1,7 +1,9 @@
-from flask import render_template, redirect, url_for, flash, Blueprint
-from celery import states
-from celery.utils.log import get_task_logger
-import conftest
+#from flask import render_template, redirect, url_for, flash, Blueprint
+#from celery import states
+#from celery.utils.log import get_task_logger
+import pytest
+from flask import url_for
+import tests.conftest
 
 
 def test_url_admin_tasks(client):
diff --git a/tests/covid19/blueprints/application/test_application_views.py b/tests/covid19/blueprints/owid/__init__.py
similarity index 100%
rename from tests/covid19/blueprints/application/test_application_views.py
rename to tests/covid19/blueprints/owid/__init__.py
diff --git a/tests/covid19/blueprints/owid/test_flaskr.py b/tests/covid19/blueprints/owid/test_flaskr.py
new file mode 100644
index 00000000..907a5bd0
--- /dev/null
+++ b/tests/covid19/blueprints/owid/test_flaskr.py
@@ -0,0 +1,24 @@
+import logging
+
+#from src.covid19.blueprints.application.application_services import owid_service
+#from src.covid19.blueprints.application.application_workers import celery
+#from src.covid19.blueprints.owid.owid_model import OwidDateReported, OwidData
+#from src.covid19.blueprints.owid.owid_model_import import OwidImport
+#from src.covid19.blueprints.application.application_model_transient import ApplicationPage
+
+from unittest import TestCase
+#from tests.conftest import app
+
+
+class Test(TestCase):
+    #def test_run_test(self):
+    #    continents = OwidImport.get_continents()
+    #    for continent in continents:
+    #        logging.info(continent)
+    #    self.assertTrue(True)
+
+    def test_run_test_01(self):
+        self.assertTrue(True)
+
+    def test_run_test_02(self):
+        self.assertTrue(True)
diff --git a/tests/covid19/blueprints/who/test_who_views.py b/tests/covid19/blueprints/who/test_who_views.py
index 38b8e5db..2b898433 100644
--- a/tests/covid19/blueprints/who/test_who_views.py
+++ b/tests/covid19/blueprints/who/test_who_views.py
@@ -1,22 +1,17 @@
-from celery.contrib import pytest
-
-from database import app, run_run_with_debug, port
+import pytest
+from flask import url_for
 import covid19.blueprints.application.application_views
+import tests.conftest
 
-from flask import render_template, redirect, url_for, flash, Blueprint
-from celery import states
-from celery.utils.log import get_task_logger
-import conftest
-import src.covid19.blueprints.who.who_views
-
-from celery.contrib import pytest
+appctx = None
+reqctx = None
 
 
-@pytest.mark.usefixtures('live_server')
-class TestLiveServer:
+def test_url_admin_tasks(client):
+    url = url_for(endpoint='who.url_who_info', _external=True, appctx=appctx, reqctx=reqctx)
+    assert client.get(url).status_code == 200
 
-    def test_url_admin_tasks(client):
-        assert client.get(url_for('who.url_who_tasks')).status_code == 200
 
-    def test_url_admin_info(client):
-        assert client.get(url_for('who.url_who_info')).status_code == 200
+def test_url_admin_info(client):
+    url = url_for(endpoint='who.url_who_info', _external=True, appctx=appctx, reqctx=reqctx)
+    assert client.get(url).status_code == 200
diff --git a/tests/mq.py b/tests/mq.py
new file mode 100644
index 00000000..a3390590
--- /dev/null
+++ b/tests/mq.py
@@ -0,0 +1,14 @@
+import sys
+import subprocess
+import covid19
+import covid19_worker
+from covid19 import app
+from covid19.blueprints.application.application_workers import celery, run_mq
+
+
+# ---------------------------------------------------------------------------------
+#  MAIN
+# ---------------------------------------------------------------------------------
+
+if __name__ == '__main__':
+    run_mq(app, celery)
diff --git a/tests/test_flaskr.py b/tests/test_flaskr.py
deleted file mode 100644
index ad009f1f..00000000
--- a/tests/test_flaskr.py
+++ /dev/null
@@ -1,12 +0,0 @@
-from unittest import TestCase
-from conftest import app
-
-from src.covid19.blueprints.owid.owid_model_import import OwidImport
-
-
-class Test(TestCase):
-    def test_run_test(self):
-        continents = OwidImport.get_continents()
-        for continent in continents:
-            app.logger.info(continent)
-        self.assertTrue(True)
diff --git a/tests/web.py b/tests/web.py
new file mode 100644
index 00000000..657bfe44
--- /dev/null
+++ b/tests/web.py
@@ -0,0 +1,12 @@
+from database import app, run_run_with_debug, port
+import covid19
+import covid19.blueprints.application.application_views
+
+from covid19 import run_web
+
+# ---------------------------------------------------------------------------------
+#  MAIN
+# ---------------------------------------------------------------------------------
+
+if __name__ == '__main__':
+    run_web()
-- 
GitLab