Merge branch 'master' into remove-initial-update-sms-sender

This commit is contained in:
Rebecca Law
2017-11-14 16:27:10 +00:00
36 changed files with 1113 additions and 157 deletions

View File

@@ -6,7 +6,8 @@ from app.dao.inbound_sms_dao import (
dao_get_inbound_sms_for_service,
dao_count_inbound_sms_for_service,
delete_inbound_sms_created_more_than_a_week_ago,
dao_get_inbound_sms_by_id
dao_get_inbound_sms_by_id,
dao_get_paginated_inbound_sms_for_service
)
from tests.app.db import create_inbound_sms, create_service
@@ -89,9 +90,83 @@ def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
assert len(InboundSms.query.all()) == 2
def test_get_inbound_sms_by_id_returns(sample_service):
inbound = create_inbound_sms(sample_service)
def test_get_inbound_sms_by_id_returns(sample_inbound_sms):
inbound_from_db = dao_get_inbound_sms_by_id(sample_inbound_sms.service.id, sample_inbound_sms.id)
inbound_from_db = dao_get_inbound_sms_by_id(sample_service.id, inbound.id)
assert sample_inbound_sms == inbound_from_db
assert inbound == inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service(sample_inbound_sms):
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_inbound_sms.service.id)
assert sample_inbound_sms == inbound_from_db[0]
def test_dao_get_paginated_inbound_sms_for_service_return_only_for_service(sample_inbound_sms):
another_service = create_service(service_name='another service')
another_inbound_sms = create_inbound_sms(another_service)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_inbound_sms.service.id)
assert sample_inbound_sms in inbound_from_db
assert another_inbound_sms not in inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service_no_inbound_sms_returns_empty_list(sample_service):
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_service.id)
assert inbound_from_db == []
def test_dao_get_paginated_inbound_sms_for_service_page_size_returns_correct_size(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
assert len(inbound_from_db) == 2
def test_dao_get_paginated_inbound_sms_for_service_older_than_returns_correct_list(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
expected_inbound_sms = reversed_inbound_sms[2:]
assert expected_inbound_sms == inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service_older_than_end_returns_empty_list(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
assert inbound_from_db == []

View File

@@ -31,7 +31,8 @@ from app.dao.services_dao import (
dao_suspend_service,
dao_resume_service,
dao_fetch_active_users_for_service,
dao_fetch_service_by_inbound_number
dao_fetch_service_by_inbound_number,
dao_fetch_monthly_historical_stats_by_template
)
from app.dao.service_permissions_dao import dao_add_service_permission, dao_remove_service_permission
from app.dao.users_dao import save_model_user
@@ -1005,3 +1006,34 @@ def _assert_service_permissions(service_permissions, expected):
assert len(service_permissions) == len(expected)
assert set(expected) == set(p.permission for p in service_permissions)
def test_dao_fetch_monthly_historical_stats_by_template(notify_db, notify_db_session):
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered'
)
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
template_two = create_sample_template(notify_db, notify_db_session, template_name='2')
notification_history(created_at=datetime(2017, 10, 1), sample_template=template_one)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime.now(), sample_template=template_two)
result = sorted(dao_fetch_monthly_historical_stats_by_template(), key=lambda x: (x.month, x.year))
assert len(result) == 2
assert result[0].template_id == template_two.id
assert result[0].month == 4
assert result[0].year == 2016
assert result[0].count == 2
assert result[1].template_id == template_one.id
assert result[1].month == 10
assert result[1].year == 2017
assert result[1].count == 1

View File

@@ -0,0 +1,45 @@
import pytest
from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template
from app.models import StatsTemplateUsageByMonth
from tests.app.conftest import sample_notification, sample_email_template, sample_template, sample_job, sample_service
def test_create_stats_for_template(notify_db_session, sample_template):
assert StatsTemplateUsageByMonth.query.count() == 0
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10)
stats_by_month = StatsTemplateUsageByMonth.query.filter(
StatsTemplateUsageByMonth.template_id == sample_template.id
).all()
assert len(stats_by_month) == 1
assert stats_by_month[0].template_id == sample_template.id
assert stats_by_month[0].month == 1
assert stats_by_month[0].year == 2017
assert stats_by_month[0].count == 10
def test_update_stats_for_template(notify_db_session, sample_template):
assert StatsTemplateUsageByMonth.query.count() == 0
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10)
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 20)
insert_or_update_stats_for_template(sample_template.id, 2, 2017, 30)
stats_by_month = StatsTemplateUsageByMonth.query.filter(
StatsTemplateUsageByMonth.template_id == sample_template.id
).order_by(StatsTemplateUsageByMonth.template_id).all()
assert len(stats_by_month) == 2
assert stats_by_month[0].template_id == sample_template.id
assert stats_by_month[0].month == 1
assert stats_by_month[0].year == 2017
assert stats_by_month[0].count == 20
assert stats_by_month[1].template_id == sample_template.id
assert stats_by_month[1].month == 2
assert stats_by_month[1].year == 2017
assert stats_by_month[1].count == 30