Updated the Provider stats and rates DAO objects to query based on the identifier in the ProviderDetails object.

- updated all tests
- changed teardown to leave provider details rows on end of individual tests
This commit is contained in:
Martyn Inglis
2016-05-06 09:09:47 +01:00
parent fedbb27ffd
commit 57e05feafb
15 changed files with 149 additions and 135 deletions

View File

@@ -1,6 +1,5 @@
import uuid import uuid
import os import os
import re
from flask import request, url_for from flask import request, url_for
from flask import Flask, _request_ctx_stack from flask import Flask, _request_ctx_stack
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
@@ -26,6 +25,9 @@ mmg_client = MMGClient()
aws_ses_client = AwsSesClient() aws_ses_client = AwsSesClient()
encryption = Encryption() encryption = Encryption()
sms_clients = []
email_clients = []
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user) api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
@@ -75,6 +77,9 @@ def create_app(app_name=None):
application.register_blueprint(template_statistics_blueprint) application.register_blueprint(template_statistics_blueprint)
application.register_blueprint(events_blueprint) application.register_blueprint(events_blueprint)
email_clients = [aws_ses_client]
sms_clients = [mmg_client, firetext_client]
return application return application

View File

@@ -1,4 +1,3 @@
import math
from sqlalchemy import desc, func from sqlalchemy import desc, func
from datetime import ( from datetime import (
@@ -19,8 +18,8 @@ from app.models import (
TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_SMS,
TEMPLATE_TYPE_EMAIL, TEMPLATE_TYPE_EMAIL,
Template, Template,
ProviderStatistics ProviderStatistics,
) ProviderDetails)
from notifications_utils.template import get_sms_fragment_count from notifications_utils.template import get_sms_fragment_count
@@ -60,7 +59,9 @@ def dao_get_template_statistics_for_service(service_id, limit_days=None):
@transactional @transactional
def dao_create_notification(notification, notification_type, provider): def dao_create_notification(notification, notification_type, provider_identifier):
provider = ProviderDetails.query.filter_by(identifier=provider_identifier).one()
if notification.job_id: if notification.job_id:
db.session.query(Job).filter_by( db.session.query(Job).filter_by(
id=notification.job_id id=notification.job_id
@@ -97,7 +98,7 @@ def dao_create_notification(notification, notification_type, provider):
update_count = db.session.query(ProviderStatistics).filter_by( update_count = db.session.query(ProviderStatistics).filter_by(
day=date.today(), day=date.today(),
service_id=notification.service_id, service_id=notification.service_id,
provider=provider provider_id=provider.id
).update({'unit_count': ProviderStatistics.unit_count + ( ).update({'unit_count': ProviderStatistics.unit_count + (
1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(notification.content_char_count))}) 1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(notification.content_char_count))})
@@ -105,7 +106,7 @@ def dao_create_notification(notification, notification_type, provider):
provider_stats = ProviderStatistics( provider_stats = ProviderStatistics(
day=notification.created_at.date(), day=notification.created_at.date(),
service_id=notification.service_id, service_id=notification.service_id,
provider=provider, provider_id=provider.id,
unit_count=1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count( unit_count=1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(
notification.content_char_count)) notification.content_char_count))
db.session.add(provider_stats) db.session.add(provider_stats)

View File

@@ -1,9 +1,11 @@
from app.models import ProviderRates from app.models import ProviderRates, ProviderDetails
from app import db from app import db
from app.dao.dao_utils import transactional from app.dao.dao_utils import transactional
@transactional @transactional
def create_provider_rates(provider, valid_from, rate): def create_provider_rates(provider_identifier, valid_from, rate):
provider_rates = ProviderRates(provider=provider, valid_from=valid_from, rate=rate) provider = ProviderDetails.query.filter_by(identifier=provider_identifier).one()
provider_rates = ProviderRates(provider_id=provider.id, valid_from=valid_from, rate=rate)
db.session.add(provider_rates) db.session.add(provider_rates)

View File

@@ -1,5 +1,5 @@
from sqlalchemy import func from sqlalchemy import func
from app.models import (ProviderStatistics, SMS_PROVIDERS, EMAIL_PROVIDERS) from app.models import (ProviderStatistics, SMS_PROVIDERS, EMAIL_PROVIDERS, ProviderDetails)
def get_provider_statistics(service, **kwargs): def get_provider_statistics(service, **kwargs):
@@ -32,7 +32,9 @@ def get_fragment_count(service, date_from, date_to):
def filter_query(query, service, **kwargs): def filter_query(query, service, **kwargs):
query = query.filter_by(service=service) query = query.filter_by(service=service)
if 'providers' in kwargs: if 'providers' in kwargs:
query = query.filter(ProviderStatistics.provider.in_(kwargs['providers'])) providers = ProviderDetails.query.filter(ProviderDetails.identifier.in_(kwargs['providers'])).all()
provider_ids = [provider.id for provider in providers]
query = query.filter(ProviderStatistics.provider_id.in_(provider_ids))
if 'date_from' in kwargs: if 'date_from' in kwargs:
query.filter(ProviderStatistics.day >= kwargs['date_from']) query.filter(ProviderStatistics.day >= kwargs['date_from'])
if 'date_to' in kwargs: if 'date_to' in kwargs:

View File

@@ -193,13 +193,16 @@ PROVIDERS = SMS_PROVIDERS + EMAIL_PROVIDERS
NOTIFICATION_TYPE = ['email', 'sms', 'letter'] NOTIFICATION_TYPE = ['email', 'sms', 'letter']
class ProviderStatistics(db.Model): class ProviderStatistics(db.Model):
__tablename__ = 'provider_statistics' __tablename__ = 'provider_statistics'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
day = db.Column(db.Date, nullable=False) day = db.Column(db.Date, nullable=False)
provider_id = db.Column(UUID(as_uuid=True), db.ForeignKey('provider_details.id'), index=True, nullable=False) provider_id = db.Column(UUID(as_uuid=True), db.ForeignKey('provider_details.id'), index=True, nullable=False)
provider_stats_to_provider = db.relationship('ProviderDetails', backref=db.backref('provider_stats', lazy='dynamic')) provider_stats_to_provider = db.relationship(
'ProviderDetails', backref=db.backref('provider_stats', lazy='dynamic')
)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False)
service = db.relationship('Service', backref=db.backref('service_provider_stats', lazy='dynamic')) service = db.relationship('Service', backref=db.backref('service_provider_stats', lazy='dynamic'))
unit_count = db.Column(db.BigInteger, nullable=False) unit_count = db.Column(db.BigInteger, nullable=False)

View File

@@ -15,7 +15,6 @@ import sqlalchemy as sa
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
def upgrade(): def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('provider_rates', op.create_table('provider_rates',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('valid_from', sa.DateTime(), nullable=False), sa.Column('valid_from', sa.DateTime(), nullable=False),
@@ -33,12 +32,9 @@ def upgrade():
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )
op.create_index(op.f('ix_provider_statistics_service_id'), 'provider_statistics', ['service_id'], unique=False) op.create_index(op.f('ix_provider_statistics_service_id'), 'provider_statistics', ['service_id'], unique=False)
### end Alembic commands ###
def downgrade(): def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_provider_statistics_service_id'), table_name='provider_statistics') op.drop_index(op.f('ix_provider_statistics_service_id'), table_name='provider_statistics')
op.drop_table('provider_statistics') op.drop_table('provider_statistics')
op.drop_table('provider_rates') op.drop_table('provider_rates')
### end Alembic commands ###

View File

@@ -62,7 +62,6 @@ def upgrade():
"UPDATE provider_statistics set provider_id = (select id from provider_details where identifier = 'ses') where provider = 'ses'" "UPDATE provider_statistics set provider_id = (select id from provider_details where identifier = 'ses') where provider = 'ses'"
) )
def downgrade(): def downgrade():
op.drop_constraint(None, 'provider_statistics', type_='foreignkey') op.drop_constraint(None, 'provider_statistics', type_='foreignkey')
@@ -73,4 +72,3 @@ def downgrade():
op.drop_column('provider_rates', 'provider_id') op.drop_column('provider_rates', 'provider_id')
op.drop_table('provider_details') op.drop_table('provider_details')
### end Alembic commands ###

View File

@@ -26,7 +26,6 @@ def upgrade():
op.drop_column('provider_statistics', 'provider') op.drop_column('provider_statistics', 'provider')
def downgrade(): def downgrade():
op.add_column('provider_statistics', sa.Column('provider', postgresql.ENUM('mmg', 'twilio', 'firetext', 'ses', name='providers'), autoincrement=False, nullable=False)) op.add_column('provider_statistics', sa.Column('provider', postgresql.ENUM('mmg', 'twilio', 'firetext', 'ses', name='providers'), autoincrement=False, nullable=False))
@@ -37,4 +36,3 @@ def downgrade():
op.alter_column('provider_rates', 'provider_id', op.alter_column('provider_rates', 'provider_id',
existing_type=postgresql.UUID(), existing_type=postgresql.UUID(),
nullable=True) nullable=True)

View File

@@ -2,7 +2,7 @@ bleach==1.4.2
Flask==0.10.1 Flask==0.10.1
Flask-Script==2.0.5 Flask-Script==2.0.5
Flask-Migrate==1.3.1 Flask-Migrate==1.3.1
Flask-SQLAlchemy==2.0 Flask-SQLAlchemy==2.1
psycopg2==2.6.1 psycopg2==2.6.1
SQLAlchemy==1.0.5 SQLAlchemy==1.0.5
SQLAlchemy-Utils==0.30.5 SQLAlchemy-Utils==0.30.5

View File

@@ -1,6 +1,6 @@
-r requirements.txt -r requirements.txt
pep8==1.5.7 pep8==1.5.7
pytest==2.8.1 pytest==2.8.3
pytest-mock==0.8.1 pytest-mock==0.8.1
pytest-cov==2.2.0 pytest-cov==2.2.0
mock==1.0.1 mock==1.0.1

View File

@@ -13,7 +13,8 @@ from app.models import (
MMG_PROVIDER, MMG_PROVIDER,
SES_PROVIDER, SES_PROVIDER,
TWILIO_PROVIDER, TWILIO_PROVIDER,
ProviderStatistics) ProviderStatistics,
ProviderDetails)
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code) from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service) from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
from app.dao.templates_dao import dao_create_template from app.dao.templates_dao import dao_create_template
@@ -327,7 +328,7 @@ def sample_notification(notify_db,
notification_id = uuid.uuid4() notification_id = uuid.uuid4()
if provider_name is None: if provider_name is None:
provider_name = mmg_provider_name() if template.template_type == 'sms' else ses_provider_name() provider = mmg_provider() if template.template_type == 'sms' else ses_provider()
if to_field: if to_field:
to = to_field to = to_field
@@ -348,7 +349,7 @@ def sample_notification(notify_db,
} }
notification = Notification(**data) notification = Notification(**data)
if create: if create:
dao_create_notification(notification, template.template_type, provider_name) dao_create_notification(notification, template.template_type, provider.identifier)
return notification return notification
@@ -458,18 +459,18 @@ def fake_uuid():
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def ses_provider_name(): def ses_provider():
return SES_PROVIDER return ProviderDetails.query.filter_by(identifier='ses').one()
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def mmg_provider_name(): def firetext_provider():
return MMG_PROVIDER return ProviderDetails.query.filter_by(identifier='mmg').one()
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def twilio_provider_name(): def mmg_provider():
return TWILIO_PROVIDER return ProviderDetails.query.filter_by(identifier='mmg').one()
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@@ -479,13 +480,14 @@ def sample_provider_statistics(notify_db,
provider=None, provider=None,
day=None, day=None,
unit_count=1): unit_count=1):
if provider is None: if provider is None:
provider = mmg_provider_name() provider = ProviderDetails.query.filter_by(identifier='mmg').first()
if day is None: if day is None:
day = date.today() day = date.today()
stats = ProviderStatistics( stats = ProviderStatistics(
service=sample_service, service=sample_service,
provider=provider, provider_id=provider.id,
day=day, day=day,
unit_count=unit_count) unit_count=unit_count)
notify_db.session.add(stats) notify_db.session.add(stats)

View File

@@ -44,7 +44,7 @@ def test_should_by_able_to_update_reference_by_id(sample_notification):
assert Notification.query.get(sample_notification.id).reference == 'reference' assert Notification.query.get(sample_notification.id).reference == 'reference'
def test_should_by_able_to_update_status_by_reference(sample_email_template, ses_provider_name): def test_should_by_able_to_update_status_by_reference(sample_email_template, ses_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_email_template.service, 'service': sample_email_template.service,
@@ -58,7 +58,7 @@ def test_should_by_able_to_update_status_by_reference(sample_email_template, ses
dao_create_notification( dao_create_notification(
notification, notification,
sample_email_template.template_type, sample_email_template.template_type,
ses_provider_name) ses_provider.identifier)
assert Notification.query.get(notification.id).status == "sending" assert Notification.query.get(notification.id).status == "sending"
update_notification_reference_by_id(notification.id, 'reference') update_notification_reference_by_id(notification.id, 'reference')
@@ -107,7 +107,7 @@ def test_should_be_able_to_record_statistics_failure_for_sms(sample_notification
).one().sms_failed == 1 ).one().sms_failed == 1
def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template, ses_provider_name): def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template, ses_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_email_template.service, 'service': sample_email_template.service,
@@ -118,7 +118,7 @@ def test_should_be_able_to_record_statistics_failure_for_email(sample_email_temp
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_email_template.template_type, ses_provider_name) dao_create_notification(notification, sample_email_template.template_type, ses_provider.identifier)
update_notification_reference_by_id(notification.id, 'reference') update_notification_reference_by_id(notification.id, 'reference')
count = update_notification_status_by_reference('reference', 'failed', 'failure') count = update_notification_status_by_reference('reference', 'failed', 'failure')
@@ -143,7 +143,7 @@ def test_should_return_zero_count_if_no_notification_with_reference():
assert update_notification_status_by_reference('something', 'delivered', 'delivered') == 0 assert update_notification_status_by_reference('something', 'delivered', 'delivered') == 0
def test_should_be_able_to_get_statistics_for_a_service(sample_template, mmg_provider_name): def test_should_be_able_to_get_statistics_for_a_service(sample_template, mmg_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_template.service, 'service': sample_template.service,
@@ -155,7 +155,7 @@ def test_should_be_able_to_get_statistics_for_a_service(sample_template, mmg_pro
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
stats = dao_get_notification_statistics_for_service(sample_template.service.id) stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 1 assert len(stats) == 1
@@ -170,7 +170,7 @@ def test_should_be_able_to_get_statistics_for_a_service(sample_template, mmg_pro
assert stats[0].emails_failed == 0 assert stats[0].emails_failed == 0
def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template, mmg_provider_name): def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template, mmg_provider):
now = datetime.utcnow() now = datetime.utcnow()
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -183,7 +183,7 @@ def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_templat
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
stat = dao_get_notification_statistics_for_service_and_day( stat = dao_get_notification_statistics_for_service_and_day(
sample_template.service.id, now.date() sample_template.service.id, now.date()
) )
@@ -197,7 +197,7 @@ def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_templat
assert stat.service_id == notification.service_id assert stat.service_id == notification.service_id
def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_template, mmg_provider_name): def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_template, mmg_provider):
now = datetime.utcnow() now = datetime.utcnow()
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -210,13 +210,13 @@ def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_temp
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert not dao_get_notification_statistics_for_service_and_day( assert not dao_get_notification_statistics_for_service_and_day(
sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).date() sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).date()
) )
def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg_provider_name): def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_template.service, 'service': sample_template.service,
@@ -230,9 +230,9 @@ def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg
notification_1 = Notification(**data) notification_1 = Notification(**data)
notification_2 = Notification(**data) notification_2 = Notification(**data)
notification_3 = Notification(**data) notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_3, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_3, sample_template.template_type, mmg_provider.identifier)
stats = dao_get_notification_statistics_for_service(sample_template.service.id) stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 1 assert len(stats) == 1
@@ -240,7 +240,7 @@ def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg
assert stats[0].sms_requested == 3 assert stats[0].sms_requested == 3
def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days(sample_template, mmg_provider_name): def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days(sample_template, mmg_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_template.service, 'service': sample_template.service,
@@ -265,9 +265,9 @@ def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days(sam
'created_at': two_days_ago 'created_at': two_days_ago
}) })
notification_3 = Notification(**data) notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_3, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_3, sample_template.template_type, mmg_provider.identifier)
stats = dao_get_notification_statistics_for_service(sample_template.service.id) stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 3 assert len(stats) == 3
@@ -287,7 +287,7 @@ def test_should_be_empty_list_if_no_statistics_for_a_service(sample_service):
def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days_previous(sample_template, def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days_previous(sample_template,
mmg_provider_name): mmg_provider):
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
'service': sample_template.service, 'service': sample_template.service,
@@ -312,9 +312,9 @@ def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days_pre
'created_at': eight_days_ago 'created_at': eight_days_ago
}) })
notification_3 = Notification(**data) notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_3, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_3, sample_template.template_type, mmg_provider.identifier)
stats = dao_get_notification_statistics_for_service( stats = dao_get_notification_statistics_for_service(
sample_template.service.id, 7 sample_template.service.id, 7
@@ -328,7 +328,7 @@ def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days_pre
assert stats[1].day == seven_days_ago.date() assert stats[1].day == seven_days_ago.date()
def test_save_notification_creates_sms_and_template_stats(sample_template, sample_job, mmg_provider_name): def test_save_notification_creates_sms_and_template_stats(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0 assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
@@ -345,7 +345,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -373,7 +373,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
assert template_stats.usage_count == 1 assert template_stats.usage_count == 1
def test_save_notification_and_create_email_and_template_stats(sample_email_template, sample_job, ses_provider_name): def test_save_notification_and_create_email_and_template_stats(sample_email_template, sample_job, ses_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0 assert NotificationStatistics.query.count() == 0
@@ -391,7 +391,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_email_template.template_type, ses_provider_name) dao_create_notification(notification, sample_email_template.template_type, ses_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -420,7 +420,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
@freeze_time("2016-01-01 00:00:00.000000") @freeze_time("2016-01-01 00:00:00.000000")
def test_save_notification_handles_midnight_properly(sample_template, sample_job, mmg_provider_name): def test_save_notification_handles_midnight_properly(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -434,7 +434,7 @@ def test_save_notification_handles_midnight_properly(sample_template, sample_job
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
@@ -446,7 +446,7 @@ def test_save_notification_handles_midnight_properly(sample_template, sample_job
@freeze_time("2016-01-01 23:59:59.999999") @freeze_time("2016-01-01 23:59:59.999999")
def test_save_notification_handles_just_before_midnight_properly(sample_template, sample_job, mmg_provider_name): def test_save_notification_handles_just_before_midnight_properly(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -460,7 +460,7 @@ def test_save_notification_handles_just_before_midnight_properly(sample_template
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
@@ -471,7 +471,7 @@ def test_save_notification_handles_just_before_midnight_properly(sample_template
assert stats.day == date(2016, 1, 1) assert stats.day == date(2016, 1, 1)
def test_save_notification_and_increment_email_stats(sample_email_template, sample_job, ses_provider_name): def test_save_notification_and_increment_email_stats(sample_email_template, sample_job, ses_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -486,7 +486,7 @@ def test_save_notification_and_increment_email_stats(sample_email_template, samp
notification_1 = Notification(**data) notification_1 = Notification(**data)
notification_2 = Notification(**data) notification_2 = Notification(**data)
dao_create_notification(notification_1, sample_email_template.template_type, ses_provider_name) dao_create_notification(notification_1, sample_email_template.template_type, ses_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
@@ -497,7 +497,7 @@ def test_save_notification_and_increment_email_stats(sample_email_template, samp
assert stats1.emails_requested == 1 assert stats1.emails_requested == 1
assert stats1.sms_requested == 0 assert stats1.sms_requested == 0
dao_create_notification(notification_2, sample_email_template.template_type, ses_provider_name) dao_create_notification(notification_2, sample_email_template.template_type, ses_provider.identifier)
assert Notification.query.count() == 2 assert Notification.query.count() == 2
@@ -509,7 +509,7 @@ def test_save_notification_and_increment_email_stats(sample_email_template, samp
assert stats2.sms_requested == 0 assert stats2.sms_requested == 0
def test_save_notification_and_increment_sms_stats(sample_template, sample_job, mmg_provider_name): def test_save_notification_and_increment_sms_stats(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -524,7 +524,7 @@ def test_save_notification_and_increment_sms_stats(sample_template, sample_job,
notification_1 = Notification(**data) notification_1 = Notification(**data)
notification_2 = Notification(**data) notification_2 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
@@ -535,7 +535,7 @@ def test_save_notification_and_increment_sms_stats(sample_template, sample_job,
assert stats1.emails_requested == 0 assert stats1.emails_requested == 0
assert stats1.sms_requested == 1 assert stats1.sms_requested == 1
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 2 assert Notification.query.count() == 2
@@ -547,7 +547,7 @@ def test_save_notification_and_increment_sms_stats(sample_template, sample_job,
assert stats2.sms_requested == 2 assert stats2.sms_requested == 2
def test_not_save_notification_and_not_create_stats_on_commit_error(sample_template, sample_job, mmg_provider_name): def test_not_save_notification_and_not_create_stats_on_commit_error(sample_template, sample_job, mmg_provider):
random_id = str(uuid.uuid4()) random_id = str(uuid.uuid4())
assert Notification.query.count() == 0 assert Notification.query.count() == 0
@@ -564,7 +564,7 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
notification = Notification(**data) notification = Notification(**data)
with pytest.raises(SQLAlchemyError): with pytest.raises(SQLAlchemyError):
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert Job.query.get(sample_job.id).notifications_sent == 0 assert Job.query.get(sample_job.id).notifications_sent == 0
@@ -572,7 +572,7 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider_name): def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -586,7 +586,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -600,12 +600,12 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
assert Job.query.get(sample_job.id).notifications_sent == 1 assert Job.query.get(sample_job.id).notifications_sent == 1
notification_2 = Notification(**data) notification_2 = Notification(**data)
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 2 assert Notification.query.count() == 2
assert Job.query.get(sample_job.id).notifications_sent == 2 assert Job.query.get(sample_job.id).notifications_sent == 2
def test_should_not_increment_job_if_notification_fails_to_persist(sample_template, sample_job, mmg_provider_name): def test_should_not_increment_job_if_notification_fails_to_persist(sample_template, sample_job, mmg_provider):
random_id = str(uuid.uuid4()) random_id = str(uuid.uuid4())
assert Notification.query.count() == 0 assert Notification.query.count() == 0
@@ -622,7 +622,7 @@ def test_should_not_increment_job_if_notification_fails_to_persist(sample_templa
} }
notification_1 = Notification(**data) notification_1 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
assert Job.query.get(sample_job.id).notifications_sent == 1 assert Job.query.get(sample_job.id).notifications_sent == 1
@@ -630,14 +630,14 @@ def test_should_not_increment_job_if_notification_fails_to_persist(sample_templa
notification_2 = Notification(**data) notification_2 = Notification(**data)
with pytest.raises(SQLAlchemyError): with pytest.raises(SQLAlchemyError):
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
assert Job.query.get(sample_job.id).notifications_sent == 1 assert Job.query.get(sample_job.id).notifications_sent == 1
assert Job.query.get(sample_job.id).updated_at == job_last_updated_at assert Job.query.get(sample_job.id).updated_at == job_last_updated_at
def test_save_notification_and_increment_correct_job(notify_db, notify_db_session, sample_template, mmg_provider_name): def test_save_notification_and_increment_correct_job(notify_db, notify_db_session, sample_template, mmg_provider):
job_1 = sample_job(notify_db, notify_db_session, sample_template.service) job_1 = sample_job(notify_db, notify_db_session, sample_template.service)
job_2 = sample_job(notify_db, notify_db_session, sample_template.service) job_2 = sample_job(notify_db, notify_db_session, sample_template.service)
@@ -654,7 +654,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -669,7 +669,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
assert Job.query.get(job_2.id).notifications_sent == 0 assert Job.query.get(job_2.id).notifications_sent == 0
def test_save_notification_with_no_job(sample_template, mmg_provider_name): def test_save_notification_with_no_job(sample_template, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
data = { data = {
'to': '+44709123456', 'to': '+44709123456',
@@ -682,7 +682,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider_name):
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -701,7 +701,7 @@ def test_get_notification(sample_notification):
assert sample_notification == notifcation_from_db assert sample_notification == notifcation_from_db
def test_save_notification_no_job_id(sample_template, mmg_provider_name): def test_save_notification_no_job_id(sample_template, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
to = '+44709123456' to = '+44709123456'
data = { data = {
@@ -715,7 +715,7 @@ def test_save_notification_no_job_id(sample_template, mmg_provider_name):
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert Notification.query.count() == 1 assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0] notification_from_db = Notification.query.all()[0]
@@ -799,7 +799,7 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not
@freeze_time("2016-03-30") @freeze_time("2016-03-30")
def test_save_new_notification_creates_template_stats(sample_template, sample_job, mmg_provider_name): def test_save_new_notification_creates_template_stats(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
data = { data = {
@@ -814,7 +814,7 @@ def test_save_new_notification_creates_template_stats(sample_template, sample_jo
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert TemplateStatistics.query.count() == 1 assert TemplateStatistics.query.count() == 1
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id, template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
@@ -826,7 +826,7 @@ def test_save_new_notification_creates_template_stats(sample_template, sample_jo
@freeze_time("2016-03-30") @freeze_time("2016-03-30")
def test_save_new_notification_creates_template_stats_per_day(sample_template, sample_job, mmg_provider_name): def test_save_new_notification_creates_template_stats_per_day(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
data = { data = {
@@ -841,7 +841,7 @@ def test_save_new_notification_creates_template_stats_per_day(sample_template, s
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
assert TemplateStatistics.query.count() == 1 assert TemplateStatistics.query.count() == 1
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id, template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
@@ -855,7 +855,7 @@ def test_save_new_notification_creates_template_stats_per_day(sample_template, s
with freeze_time('2016-03-31'): with freeze_time('2016-03-31'):
assert TemplateStatistics.query.count() == 1 assert TemplateStatistics.query.count() == 1
new_notification = Notification(**data) new_notification = Notification(**data)
dao_create_notification(new_notification, sample_template.template_type, mmg_provider_name) dao_create_notification(new_notification, sample_template.template_type, mmg_provider.identifier)
assert TemplateStatistics.query.count() == 2 assert TemplateStatistics.query.count() == 2
first_stats = TemplateStatistics.query.filter(TemplateStatistics.day == datetime(2016, 3, 30)).first() first_stats = TemplateStatistics.query.filter(TemplateStatistics.day == datetime(2016, 3, 30)).first()
@@ -871,7 +871,7 @@ def test_save_new_notification_creates_template_stats_per_day(sample_template, s
assert second_stats.usage_count == 1 assert second_stats.usage_count == 1
def test_save_another_notification_increments_template_stats(sample_template, sample_job, mmg_provider_name): def test_save_another_notification_increments_template_stats(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
data = { data = {
@@ -887,7 +887,7 @@ def test_save_another_notification_increments_template_stats(sample_template, sa
notification_1 = Notification(**data) notification_1 = Notification(**data)
notification_2 = Notification(**data) notification_2 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
assert TemplateStatistics.query.count() == 1 assert TemplateStatistics.query.count() == 1
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id, template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
@@ -896,7 +896,7 @@ def test_save_another_notification_increments_template_stats(sample_template, sa
assert template_stats.template_id == sample_template.id assert template_stats.template_id == sample_template.id
assert template_stats.usage_count == 1 assert template_stats.usage_count == 1
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
assert TemplateStatistics.query.count() == 1 assert TemplateStatistics.query.count() == 1
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id, template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
@@ -906,7 +906,7 @@ def test_save_another_notification_increments_template_stats(sample_template, sa
def test_successful_notification_inserts_followed_by_failure_does_not_increment_template_stats(sample_template, def test_successful_notification_inserts_followed_by_failure_does_not_increment_template_stats(sample_template,
sample_job, sample_job,
mmg_provider_name): mmg_provider):
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert NotificationStatistics.query.count() == 0 assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0 assert TemplateStatistics.query.count() == 0
@@ -925,9 +925,9 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
notification_1 = Notification(**data) notification_1 = Notification(**data)
notification_2 = Notification(**data) notification_2 = Notification(**data)
notification_3 = Notification(**data) notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_1, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_2, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_2, sample_template.template_type, mmg_provider.identifier)
dao_create_notification(notification_3, sample_template.template_type, mmg_provider_name) dao_create_notification(notification_3, sample_template.template_type, mmg_provider.identifier)
assert NotificationStatistics.query.count() == 1 assert NotificationStatistics.query.count() == 1
notication_stats = NotificationStatistics.query.filter( notication_stats = NotificationStatistics.query.filter(
@@ -946,7 +946,7 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
try: try:
# Mess up db in really bad way # Mess up db in really bad way
db.session.execute('DROP TABLE TEMPLATE_STATISTICS') db.session.execute('DROP TABLE TEMPLATE_STATISTICS')
dao_create_notification(failing_notification, sample_template.template_type, mmg_provider_name) dao_create_notification(failing_notification, sample_template.template_type, mmg_provider.identifier)
except Exception as e: except Exception as e:
# There should be no additional notification stats or counts # There should be no additional notification stats or counts
assert NotificationStatistics.query.count() == 1 assert NotificationStatistics.query.count() == 1
@@ -959,7 +959,7 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
@freeze_time("2016-03-30") @freeze_time("2016-03-30")
def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(sample_template, def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(sample_template,
sample_job, sample_job,
mmg_provider_name): mmg_provider):
template_stats = dao_get_template_statistics_for_service(sample_template.service.id) template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
assert len(template_stats) == 0 assert len(template_stats) == 0
@@ -975,17 +975,17 @@ def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(samp
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider_name) dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
# move on one day # move on one day
with freeze_time('2016-03-31'): with freeze_time('2016-03-31'):
new_notification = Notification(**data) new_notification = Notification(**data)
dao_create_notification(new_notification, sample_template.template_type, mmg_provider_name) dao_create_notification(new_notification, sample_template.template_type, mmg_provider.identifier)
# move on one more day # move on one more day
with freeze_time('2016-04-01'): with freeze_time('2016-04-01'):
new_notification = Notification(**data) new_notification = Notification(**data)
dao_create_notification(new_notification, sample_template.template_type, mmg_provider_name) dao_create_notification(new_notification, sample_template.template_type, mmg_provider.identifier)
template_stats = dao_get_template_statistics_for_service(sample_template.service_id) template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
assert len(template_stats) == 3 assert len(template_stats) == 3

View File

@@ -3,112 +3,114 @@ from app.models import ProviderStatistics
from app.dao.provider_statistics_dao import ( from app.dao.provider_statistics_dao import (
get_provider_statistics, get_fragment_count) get_provider_statistics, get_fragment_count)
from app.models import Notification
from tests.app.conftest import sample_notification as create_sample_notification from tests.app.conftest import sample_notification as create_sample_notification
def test_should_update_provider_statistics_sms(notify_db, def test_should_update_provider_statistics_sms(notify_db,
notify_db_session, notify_db_session,
sample_template, sample_template,
mmg_provider_name): mmg_provider):
notification = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_template) template=sample_template)
provider_stats = get_provider_statistics( provider_stats = get_provider_statistics(
sample_template.service, sample_template.service,
providers=[mmg_provider_name]).one() providers=[mmg_provider.identifier]).one()
assert provider_stats.unit_count == 1 assert provider_stats.unit_count == 1
def test_should_update_provider_statistics_email(notify_db, def test_should_update_provider_statistics_email(notify_db,
notify_db_session, notify_db_session,
sample_email_template, sample_email_template,
ses_provider_name): ses_provider):
notification = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_email_template) template=sample_email_template)
provider_stats = get_provider_statistics( provider_stats = get_provider_statistics(
sample_email_template.service, sample_email_template.service,
providers=[ses_provider_name]).one() providers=[ses_provider.identifier]).one()
assert provider_stats.unit_count == 1 assert provider_stats.unit_count == 1
def test_should_update_provider_statistics_sms_multi(notify_db, def test_should_update_provider_statistics_sms_multi(notify_db,
notify_db_session, notify_db_session,
sample_template, sample_template,
mmg_provider_name): mmg_provider):
notification1 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_template, template=sample_template,
content_char_count=160) content_char_count=160)
notification1 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_template, template=sample_template,
content_char_count=161) content_char_count=161)
notification1 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_template, template=sample_template,
content_char_count=307) content_char_count=307)
provider_stats = get_provider_statistics( provider_stats = get_provider_statistics(
sample_template.service, sample_template.service,
providers=[mmg_provider_name]).one() providers=[mmg_provider.identifier]).one()
assert provider_stats.unit_count == 6 assert provider_stats.unit_count == 6
def test_should_update_provider_statistics_email_multi(notify_db, def test_should_update_provider_statistics_email_multi(notify_db,
notify_db_session, notify_db_session,
sample_email_template, sample_email_template,
ses_provider_name): ses_provider):
notification1 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_email_template) template=sample_email_template)
notification2 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_email_template) template=sample_email_template)
notification3 = create_sample_notification( create_sample_notification(
notify_db, notify_db,
notify_db_session, notify_db_session,
template=sample_email_template) template=sample_email_template)
provider_stats = get_provider_statistics( provider_stats = get_provider_statistics(
sample_email_template.service, sample_email_template.service,
providers=[ses_provider_name]).one() providers=[ses_provider.identifier]).one()
assert provider_stats.unit_count == 3 assert provider_stats.unit_count == 3
def test_should_aggregate_fragment_count(notify_db, def test_should_aggregate_fragment_count(notify_db,
notify_db_session, notify_db_session,
sample_service, sample_service,
mmg_provider_name, mmg_provider,
twilio_provider_name, firetext_provider,
ses_provider_name): ses_provider):
day = date.today() day = date.today()
stats_mmg = ProviderStatistics( stats_mmg = ProviderStatistics(
service=sample_service, service=sample_service,
day=day, day=day,
provider=mmg_provider_name, provider_id=mmg_provider.id,
unit_count=2 unit_count=2
) )
stats_twilio = ProviderStatistics(
stats_firetext = ProviderStatistics(
service=sample_service, service=sample_service,
day=day, day=day,
provider=twilio_provider_name, provider_id=firetext_provider.id,
unit_count=3 unit_count=3
) )
stats_twilio = ProviderStatistics(
stats_ses = ProviderStatistics(
service=sample_service, service=sample_service,
day=day, day=day,
provider=ses_provider_name, provider_id=ses_provider.id,
unit_count=1 unit_count=1
) )
notify_db.session.add(stats_mmg) notify_db.session.add(stats_mmg)
notify_db.session.add(stats_twilio) notify_db.session.add(stats_firetext)
notify_db.session.add(stats_ses)
notify_db.session.commit() notify_db.session.commit()
results = get_fragment_count(sample_service, day, day) results = get_fragment_count(sample_service, day, day)
assert results['sms_count'] == 5 assert results['sms_count'] == 5
@@ -118,19 +120,19 @@ def test_should_aggregate_fragment_count(notify_db,
def test_should_aggregate_fragment_count_over_days(notify_db, def test_should_aggregate_fragment_count_over_days(notify_db,
notify_db_session, notify_db_session,
sample_service, sample_service,
mmg_provider_name): mmg_provider):
today = date.today() today = date.today()
yesterday = today - timedelta(days=1) yesterday = today - timedelta(days=1)
stats_today = ProviderStatistics( stats_today = ProviderStatistics(
service=sample_service, service=sample_service,
day=today, day=today,
provider=mmg_provider_name, provider_id=mmg_provider.id,
unit_count=2 unit_count=2
) )
stats_yesterday = ProviderStatistics( stats_yesterday = ProviderStatistics(
service=sample_service, service=sample_service,
day=yesterday, day=yesterday,
provider=mmg_provider_name, provider_id=mmg_provider.id,
unit_count=3 unit_count=3
) )
notify_db.session.add(stats_today) notify_db.session.add(stats_today)

View File

@@ -1,14 +1,17 @@
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from app.dao.provider_rates_dao import create_provider_rates from app.dao.provider_rates_dao import create_provider_rates
from app.models import ProviderRates from app.models import ProviderRates, ProviderDetails
def test_create_provider_rates(notify_db, notify_db_session, mmg_provider_name): def test_create_provider_rates(notify_db, notify_db_session, mmg_provider):
now = datetime.now() now = datetime.now()
rate = Decimal("1.00000") rate = Decimal("1.00000")
create_provider_rates(mmg_provider_name, now, rate)
provider = ProviderDetails.query.filter_by(identifier=mmg_provider.identifier).one()
create_provider_rates(mmg_provider.identifier, now, rate)
assert ProviderRates.query.count() == 1 assert ProviderRates.query.count() == 1
assert ProviderRates.query.first().rate == rate assert ProviderRates.query.first().rate == rate
assert ProviderRates.query.first().valid_from == now assert ProviderRates.query.first().valid_from == now
assert ProviderRates.query.first().provider == mmg_provider_name assert ProviderRates.query.first().provider_id == provider.id

View File

@@ -41,6 +41,7 @@ def notify_db(notify_api, request):
db.session.remove() db.session.remove()
db.drop_all() db.drop_all()
db.engine.execute("drop table alembic_version") db.engine.execute("drop table alembic_version")
db.engine.execute("drop type providers")
db.get_engine(notify_api).dispose() db.get_engine(notify_api).dispose()
request.addfinalizer(teardown) request.addfinalizer(teardown)
@@ -52,7 +53,8 @@ def notify_db_session(request):
def teardown(): def teardown():
db.session.remove() db.session.remove()
for tbl in reversed(meta.sorted_tables): for tbl in reversed(meta.sorted_tables):
db.engine.execute(tbl.delete()) if tbl.name not in ["provider_details"]:
db.engine.execute(tbl.delete())
db.session.commit() db.session.commit()
meta = MetaData(bind=db.engine, reflect=True) meta = MetaData(bind=db.engine, reflect=True)