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(
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}
)

View File

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

7
test.py Normal file
View File

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