This commit is contained in:
Torma Kristóf 2020-08-02 00:53:49 +02:00
parent 80de8062a3
commit 434e63053b
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
6 changed files with 72 additions and 11 deletions

45
conftest.py Normal file
View File

@ -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

4
requirements.test.txt Normal file
View File

@ -0,0 +1,4 @@
pytest
pytest-rabbitmq
pytest-httpserver
pytest-flask-sqlalchemy

View File

@ -24,9 +24,11 @@ if SENTRY_DSN:
sentry_sdk.init( sentry_sdk.init(
dsn=SENTRY_DSN, dsn=SENTRY_DSN,
integrations=[FlaskIntegration()], integrations=[FlaskIntegration()],
traces_sample_rate=1.0,
send_default_pii=True, send_default_pii=True,
release=RELEASE_ID, release=RELEASE_ID,
environment=RELEASEMODE environment=RELEASEMODE,
_experiments={"auto_enabling_integrations": True}
) )

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import uuid
import pika import pika
""" """
@ -28,6 +29,7 @@ class FlaskRabbitBroker:
self.connection = None self.connection = None
self.channel = None self.channel = None
self.exchange = None self.exchange = None
self.exchange_type = "fanout"
def init_app(self, app) -> None: def init_app(self, app) -> None:
""" """
@ -35,11 +37,11 @@ class FlaskRabbitBroker:
:param app: application context :param app: application context
:return: :return:
""" """
self.username = app.context.get('RABBIT_USERNAME') self.username = app.config.get('RABBIT_USERNAME')
self.password = app.context.get('RABBIT_PASSWORD') self.password = app.config.get('RABBIT_PASSWORD')
self.rabbitmq_host = app.context.get('RABBIT_HOST') self.rabbitmq_host = app.config.get('RABBIT_HOST')
self.exchange_name = app.context.get('EXCHANGE_NAME') self.exchange_name = app.config.get('EXCHANGE_NAME')
self.routing_key = app.context.get('RABBIT_ROUTING_KEY') self.routing_key = app.config.get('RABBIT_ROUTING_KEY')
self.init_connection(timeout=5) self.init_connection(timeout=5)
self.init_exchange() self.init_exchange()
@ -65,8 +67,8 @@ class FlaskRabbitBroker:
""" """
channel = self.connection.channel() channel = self.connection.channel()
try: try:
exchange = channel.exchange_declare(exchange=self.exchange_name, channel.exchange_declare(exchange=self.exchange_name,
exchange_type='fanout', exchange_type=self.exchange_type,
durable=True, durable=True,
auto_delete=False) auto_delete=False)
finally: finally:
@ -79,9 +81,10 @@ class FlaskRabbitBroker:
:return: :return:
""" """
channel = self.connection.channel() channel = self.connection.channel()
queue = channel.queue_declare(durable=True, auto_delete=False) queue = channel.queue_declare(durable=True, auto_delete=False, exclusive=True,
queue.bind(self.exchange) queue=uuid.uuid4().urn.split(':')[2]).method.queue
queue.basic_consume(callback, no_ack=True) 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: def send(self, message: str) -> None:
""" """

7
test.py Normal file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env python3
"""
Unit tests
"""
import pytest