From 434e63053b196b79474938a441357a35fb8360fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 2 Aug 2020 00:53:49 +0200 Subject: [PATCH] hotfix --- conftest.py | 45 ++++++++++++++++++++++++++++++++++++++ tester.py => manual_try.py | 0 requirements.test.txt | 4 ++++ src/app.py | 4 +++- src/flask_rabbit_broker.py | 23 ++++++++++--------- test.py | 7 ++++++ 6 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 conftest.py rename tester.py => manual_try.py (100%) create mode 100644 requirements.test.txt create mode 100644 test.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..f2236ee --- /dev/null +++ b/conftest.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import pytest +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + + +@pytest.fixture(scope='session') +def database(request): + """ + Create a Postgres database for the tests, and drop it when the tests are done. + """ + pg_host = DB_OPTS.get("postgresql") + pg_port = DB_OPTS.get("5432") + pg_user = DB_OPTS.get("input-service-test") + pg_db = DB_OPTS["input-service-test"] + + init_postgresql_database(pg_user, pg_host, pg_port, pg_db) + + @request.addfinalizer + def drop_database(): + drop_postgresql_database(pg_user, pg_host, pg_port, pg_db) + + +@pytest.fixture(scope='session') +def app(database): + """ + Create a Flask app context for the tests. + """ + app = Flask(__name__) + + app.config['SQLALCHEMY_DATABASE_URI'] = DB_CONN + + return app + + +@pytest.fixture(scope='session') +def _db(app): + """ + Provide the transactional fixtures with access to the database via a Flask-SQLAlchemy + database connection. + """ + db = SQLAlchemy(app=app) + + return db diff --git a/tester.py b/manual_try.py similarity index 100% rename from tester.py rename to manual_try.py diff --git a/requirements.test.txt b/requirements.test.txt new file mode 100644 index 0000000..5f5b607 --- /dev/null +++ b/requirements.test.txt @@ -0,0 +1,4 @@ +pytest +pytest-rabbitmq +pytest-httpserver +pytest-flask-sqlalchemy diff --git a/src/app.py b/src/app.py index 96f037d..ff7ab45 100644 --- a/src/app.py +++ b/src/app.py @@ -24,9 +24,11 @@ if SENTRY_DSN: sentry_sdk.init( dsn=SENTRY_DSN, integrations=[FlaskIntegration()], + traces_sample_rate=1.0, send_default_pii=True, release=RELEASE_ID, - environment=RELEASEMODE + environment=RELEASEMODE, + _experiments={"auto_enabling_integrations": True} ) diff --git a/src/flask_rabbit_broker.py b/src/flask_rabbit_broker.py index e85a61a..5eff525 100644 --- a/src/flask_rabbit_broker.py +++ b/src/flask_rabbit_broker.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import uuid import pika """ @@ -28,6 +29,7 @@ class FlaskRabbitBroker: self.connection = None self.channel = None self.exchange = None + self.exchange_type = "fanout" def init_app(self, app) -> None: """ @@ -35,11 +37,11 @@ class FlaskRabbitBroker: :param app: application context :return: """ - self.username = app.context.get('RABBIT_USERNAME') - self.password = app.context.get('RABBIT_PASSWORD') - self.rabbitmq_host = app.context.get('RABBIT_HOST') - self.exchange_name = app.context.get('EXCHANGE_NAME') - self.routing_key = app.context.get('RABBIT_ROUTING_KEY') + self.username = app.config.get('RABBIT_USERNAME') + self.password = app.config.get('RABBIT_PASSWORD') + self.rabbitmq_host = app.config.get('RABBIT_HOST') + self.exchange_name = app.config.get('EXCHANGE_NAME') + self.routing_key = app.config.get('RABBIT_ROUTING_KEY') self.init_connection(timeout=5) self.init_exchange() @@ -65,8 +67,8 @@ class FlaskRabbitBroker: """ channel = self.connection.channel() try: - exchange = channel.exchange_declare(exchange=self.exchange_name, - exchange_type='fanout', + channel.exchange_declare(exchange=self.exchange_name, + exchange_type=self.exchange_type, durable=True, auto_delete=False) finally: @@ -79,9 +81,10 @@ class FlaskRabbitBroker: :return: """ channel = self.connection.channel() - queue = channel.queue_declare(durable=True, auto_delete=False) - queue.bind(self.exchange) - queue.basic_consume(callback, no_ack=True) + queue = channel.queue_declare(durable=True, auto_delete=False, exclusive=True, + queue=uuid.uuid4().urn.split(':')[2]).method.queue + channel.bind(exchange=self.exchange_name, queue=queue) + channel.basic_consume(queue=queue, on_message_callback=callback, auto_ack=True) def send(self, message: str) -> None: """ diff --git a/test.py b/test.py new file mode 100644 index 0000000..ee11b30 --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +""" +Unit tests +""" + +import pytest