diff --git a/src/config.py b/src/config.py index a067608f1f3cd499a24c64f8f96211edf7afaad1..5f5560389c3221330700a1108d76b525aedd52a6 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 aca3a323d384d8a67ff0c873370e1857a57c0b59..cb9aeef31986253fc22f858f2b33581d00c6f9b2 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 d76e140a71320919bba06f925f27ea9bd9bdeaa1..b9dc84a9c04d425027aa27a341971ec5f9e624b8 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 0ebe7868949939c0062ceeeb08d9d362a383e928..5006fe047b1bae94b57450e083b942cd18fa639c 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 0000000000000000000000000000000000000000..907a5bd033ca16547052b177d8663157f17e1965 --- /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 38b8e5db5d5a8b2f99c9595583913bb709643adf..2b898433dd99f87d24b511161f4ccef8b76a3770 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 0000000000000000000000000000000000000000..a33905906322e145c274714c4b582d4ae0f86f9d --- /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 ad009f1f27835926212eb30163d46d0c6a10a657..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..657bfe442e03b1c567b4d6e33fb13817604374f3 --- /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()