mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-27 09:28:03 -04:00
Merge branch 'master' into noti-stats-cleanup
This commit is contained in:
89
.travis.yml
89
.travis.yml
@@ -26,95 +26,6 @@ notifications:
|
||||
rooms:
|
||||
secure: vCmpAjdXkprqGzSnQzcco1lNiFuj/nRQdlVTGSnvg2HEqZUM5vzmWtQYw3WNS/bcgbVxV/nLgza17cdsmFqOuaZll4kDM0swnmk17eynImAHPFF/flGsNcw4oRV7WzWrq7j1TvGzJzxJkHYOjJFk0iMLM7+P76sWHI6Oa5NHQYH2l7a4hqzqmuUKaloHeZX/Ro2RmFE+/t/SfNr0pmtTRO4CfLfPUdUs9D8Mx0cyc1Z9Ke4Cq+R2Lsp8BwH0nye6pb9cI4h1YmUY/Xt8O1Z0QbncHZviWojSB2vV0V5WQPzqjjseAznJ1depK/LPOlRHlqddkEbGK28rDwsxbzY1q3DCZZiZZTXBD80HyNr/J4rfRAXhoCafwR57cqqIQ7G/fb5A/ckUM8TFKqUPh7wvu67OAiSWJmO78C8fumF3HxcoGp7DQOQPFLtWGuCfh2zjI9WbUhiHQwWl9/9qpgMEuXH7LOJAzgQbEp1A7XRmT2GupYEkGdFMwT8ojp8OXds08diEy1FNonWAtFZs8pO92urK862pCRB8WRQZWMjpcnif1Ht4QtwYFO9iVp5ygReTEoFuiQPMAypCVCzKgHe/W8LX55xzSnlUQqDU2NzN37LfC4ctS1h3BfIIU5KP9PmdgkKRUWcrFE3eMXPG36OdU4hRj4FMURzmAOZYhaSXuP8=
|
||||
deploy:
|
||||
- provider: s3
|
||||
access_key_id: AKIAJLWYN4T4D5WU4APA
|
||||
secret_access_key: &1
|
||||
secure: j89/yB4NUSUwgG4UKVBMuzkGW8UwFS3Fp9W/7sxFh0zXndMwEN8yl9OtaJ/K9gKds8FAKYCwdSJ90wztdEuDO7c1k60WtD08vbXx1TIEvk9N+VRCOAo5dKy3ZpoF8vQJYNP/luQyU5Ev8zm69nzW+40KaHosyS0b6/dvxy4luZnBiiX9bmR+12Mg3qT/NN7qf4SsImxc0OKl37NdUc03ygCPRPLhDD1bOgfLhDaZNby7DNbP3rKSB81T5cww8Ib2PkwjR9UTurD5JpIBJmZXhqVDtZ9cYUjrrmRPtHtzWBVt3QR7pQeqGupdLTlljV7o6FkAbwjYcEo72cMlwYcoDvBwGc0nyZCgQsQDkHRo8ez5WbXuDq3QvNNOCUh+HPCYSze3uWRIwY/Sb48dwLI1Y5ieQ5b+u2dusxRQbOHajPAO6I9Zl2IRuV6/lHhtfKfTF2BIKT2hfRdkMCIlTBGSPF6wZVhBd3dSTXJxwsarphDvfVilHI0tzz3gfnbmwAz/Z5KAASlCE73oX+0dvZDke1tNIURCM8MzfmK3BmRzh0V0zdET1wzRvhSzyvHSfOL6qRjxDfbvRRPQXD2EkFuW3RJlguOrX5k2eJ5eG0Da9hcI2XZ7VI7cK/UdLgRYjad8l6pKtYIQcxRw4EPb4OtsnMlzpzV9uFpObIZSgUgOoC4=
|
||||
local_dir: dpl_cd_upload
|
||||
skip_cleanup: true
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: &2
|
||||
repo: alphagov/notifications-api
|
||||
bucket: notify.works-notifications-api-codedeploy
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJLWYN4T4D5WU4APA
|
||||
secret_access_key: *1
|
||||
bucket: notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_admin_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJLWYN4T4D5WU4APA
|
||||
secret_access_key: *1
|
||||
bucket: notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJLWYN4T4D5WU4APA
|
||||
secret_access_key: *1
|
||||
bucket: notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_delivery_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: s3
|
||||
access_key_id: AKIAJBHLXFS5UHJ6PXLA
|
||||
secret_access_key: &1
|
||||
secure: wYEw/AMHxcfddKP7Hc3r08onLtkZyYBnm+xhxMum3rQjR6Otj4rQ9pROw9V3xOzPBq/nKPTkO6G+4UVrHlPwBmplfpeyK/PXLll8gcNPV0qWxyKs7uVBW6KAgsXm5utuAyLNSWINWpbinjtHCTg3g+JwVFj32/RTUYTKfqzKl/M91Cd1Ezs0olrUIqkONTWRbM3fwCce//77XpRRYIK6baZK4I/qXXJ4P52cj9iqFVF1x684082cxcROtREC0Aeucldvft4WuNtUGZlczcG00PhTP9+pkKhgsNB9q/ICc3aBtbhjdKtJjweNfaNzbL1f/UgG+xJdf7zl4LEpTdSjklJeelHh93UlPObOUsm+BUU9ZRLa8xDpuWxvOxDpSIwBAORkYTl1kxfwb2GJVACZHrmF3zl9Z+btPYGyu6gCSf+3ez0HWFR76DfpsZfMyFUF9mxrBfpQR6d7hYAOdOggOpTB3jCkzMtkDeeYedYErUnfP6CA0rrN5voTqq5RuOWjJPMnuL7BMyM2ZQ2z5QJaUP7FV8z20YV89QiHN83GpZBBqm4t0ai+OLE5tqxGGztfoYpashqPsU6131lkA6X1eSCi9k4TlB40Ll2vcLoW+NhQTYtRr2TcdUiu23CPzzzsi6DOabJFu6PyvZ9IC5nISud/eegbJyDczZfiHETJuBQ=
|
||||
local_dir: dpl_cd_upload
|
||||
skip_cleanup: true
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: &2
|
||||
repo: alphagov/notifications-api
|
||||
branch: staging
|
||||
bucket: staging-notify.works-notifications-api-codedeploy
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJBHLXFS5UHJ6PXLA
|
||||
secret_access_key: *1
|
||||
bucket: staging-notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJBHLXFS5UHJ6PXLA
|
||||
secret_access_key: *1
|
||||
bucket: staging-notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_admin_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: codedeploy
|
||||
access_key_id: AKIAJBHLXFS5UHJ6PXLA
|
||||
secret_access_key: *1
|
||||
bucket: staging-notify.works-notifications-api-codedeploy
|
||||
key: notifications-api-$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER-$TRAVIS_COMMIT.zip
|
||||
bundle_type: zip
|
||||
application: api
|
||||
deployment_group: notifications_delivery_api_deployment_group
|
||||
region: eu-west-1
|
||||
wait-until-deployed: true
|
||||
on: *2
|
||||
- provider: s3
|
||||
access_key_id: AKIAIFLN7IJIIQT6S37Q
|
||||
secret_access_key: &1
|
||||
|
||||
6
Makefile
6
Makefile
@@ -105,6 +105,9 @@ build-with-docker: prepare-docker-build-image ## Build inside a Docker container
|
||||
--name "${DOCKER_CONTAINER_PREFIX}-build" \
|
||||
-v `pwd`:/var/project \
|
||||
-v ${PIP_ACCEL_CACHE}:/var/project/cache/pip-accel \
|
||||
-e GIT_COMMIT=${GIT_COMMIT} \
|
||||
-e BUILD_NUMBER=${BUILD_NUMBER} \
|
||||
-e BUILD_URL=${BUILD_URL} \
|
||||
${DOCKER_BUILDER_IMAGE_NAME} \
|
||||
make build
|
||||
|
||||
@@ -114,6 +117,9 @@ test-with-docker: prepare-docker-build-image create-docker-test-db ## Run tests
|
||||
--name "${DOCKER_CONTAINER_PREFIX}-test" \
|
||||
--link "${DOCKER_CONTAINER_PREFIX}-db:postgres" \
|
||||
-e TEST_DATABASE=postgresql://postgres:postgres@postgres/test_notification_api \
|
||||
-e GIT_COMMIT=${GIT_COMMIT} \
|
||||
-e BUILD_NUMBER=${BUILD_NUMBER} \
|
||||
-e BUILD_URL=${BUILD_URL} \
|
||||
-v `pwd`:/var/project \
|
||||
${DOCKER_BUILDER_IMAGE_NAME} \
|
||||
make test
|
||||
|
||||
@@ -33,7 +33,6 @@ from app.dao.dao_utils import transactional
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_notification_statistics_for_service_and_day(service_id, day):
|
||||
# only used by stat-updating code in tasks.py
|
||||
return NotificationStatistics.query.filter_by(
|
||||
@@ -92,7 +91,6 @@ def create_notification_statistics_dict(service_id, day):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
table = NotificationHistory
|
||||
|
||||
if limit_days and limit_days <= 7: # can get this data from notifications table
|
||||
@@ -110,28 +108,18 @@ def dao_get_template_usage(service_id, limit_days=None):
|
||||
query_filter.append(table.created_at >= days_ago(limit_days))
|
||||
|
||||
return query.filter(*query_filter) \
|
||||
.join(Template)\
|
||||
.group_by(table.template_id, Template.name, Template.template_type)\
|
||||
.order_by(asc(Template.name))\
|
||||
.join(Template) \
|
||||
.group_by(table.template_id, Template.name, Template.template_type) \
|
||||
.order_by(asc(Template.name)) \
|
||||
.all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_service(service_id, limit_days=None):
|
||||
query_filter = [TemplateStatistics.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(TemplateStatistics.day >= days_ago(limit_days))
|
||||
return TemplateStatistics.query.filter(*query_filter).order_by(
|
||||
desc(TemplateStatistics.updated_at)).all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_template(template_id):
|
||||
return TemplateStatistics.query.filter(
|
||||
TemplateStatistics.template_id == template_id
|
||||
).order_by(
|
||||
desc(TemplateStatistics.updated_at)
|
||||
).all()
|
||||
def dao_get_last_template_usage(template_id):
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id)\
|
||||
.join(Template) \
|
||||
.order_by(desc(NotificationHistory.created_at)) \
|
||||
.first()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -6,11 +6,9 @@ from flask import (
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_get_template_usage,
|
||||
dao_get_template_statistics_for_service,
|
||||
dao_get_template_statistics_for_template
|
||||
)
|
||||
dao_get_last_template_usage)
|
||||
|
||||
from app.schemas import template_statistics_schema
|
||||
from app.schemas import notifications_filter_schema, NotificationWithTemplateSchema, notification_with_template_schema
|
||||
|
||||
template_statistics = Blueprint('template-statistics',
|
||||
__name__,
|
||||
@@ -47,6 +45,10 @@ def get_template_statistics_for_service_by_day(service_id):
|
||||
|
||||
@template_statistics.route('/<template_id>')
|
||||
def get_template_statistics_for_template_id(service_id, template_id):
|
||||
stats = dao_get_template_statistics_for_template(template_id)
|
||||
data = template_statistics_schema.dump(stats, many=True).data
|
||||
notification = dao_get_last_template_usage(template_id)
|
||||
if not notification:
|
||||
message = 'No template found for id {}'.format(template_id)
|
||||
errors = {'template_id': [message]}
|
||||
raise InvalidRequest(errors, status_code=404)
|
||||
data = notification_with_template_schema.dump(notification).data
|
||||
return jsonify(data=data)
|
||||
|
||||
29
migrations/versions/0047_ukvi_spelling.py
Normal file
29
migrations/versions/0047_ukvi_spelling.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 0047_ukvi_spelling
|
||||
Revises: 0046_organisations_and_branding
|
||||
Create Date: 2016-08-22 16:06:32.981723
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0047_ukvi_spelling'
|
||||
down_revision = '0046_organisations_and_branding'
|
||||
|
||||
from alembic import op
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.execute("""
|
||||
UPDATE organisation
|
||||
SET name = 'UK Visas & Immigration'
|
||||
WHERE id = '9d25d02d-2915-4e98-874b-974e123e8536'
|
||||
""")
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.execute("""
|
||||
UPDATE organisation
|
||||
SET name = 'UK Visas and Immigration'
|
||||
WHERE id = '9d25d02d-2915-4e98-874b-974e123e8536'
|
||||
""")
|
||||
@@ -5,7 +5,6 @@ from functools import partial
|
||||
import pytest
|
||||
|
||||
from freezegun import freeze_time
|
||||
from mock import ANY
|
||||
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
||||
|
||||
from app import db
|
||||
@@ -22,21 +21,20 @@ from app.models import (
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_update_notification,
|
||||
get_notification_with_personalisation,
|
||||
get_notification_for_job,
|
||||
get_notifications_for_job,
|
||||
delete_notifications_created_more_than_a_week_ago,
|
||||
dao_get_last_template_usage,
|
||||
dao_get_notification_statistics_for_service_and_day,
|
||||
update_notification_status_by_id,
|
||||
update_provider_stats,
|
||||
update_notification_status_by_reference,
|
||||
dao_get_template_statistics_for_service,
|
||||
get_notifications_for_service,
|
||||
dao_get_potential_notification_statistics_for_day,
|
||||
dao_get_template_statistics_for_template,
|
||||
dao_get_template_usage,
|
||||
dao_update_notification,
|
||||
delete_notifications_created_more_than_a_week_ago,
|
||||
get_notification_by_id,
|
||||
dao_get_template_usage
|
||||
get_notification_for_job,
|
||||
get_notification_with_personalisation,
|
||||
get_notifications_for_job,
|
||||
get_notifications_for_service,
|
||||
update_notification_status_by_id,
|
||||
update_notification_status_by_reference,
|
||||
update_provider_stats,
|
||||
)
|
||||
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
@@ -45,11 +43,9 @@ from tests.app.conftest import (sample_notification, sample_template, sample_ema
|
||||
|
||||
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
assert dao_get_last_template_usage.__wrapped__.__name__ == 'dao_get_last_template_usage' # noqa
|
||||
assert dao_get_template_usage.__wrapped__.__name__ == 'dao_get_template_usage' # noqa
|
||||
assert dao_get_notification_statistics_for_service_and_day.__wrapped__.__name__ == 'dao_get_notification_statistics_for_service_and_day' # noqa
|
||||
assert dao_get_potential_notification_statistics_for_day.__wrapped__.__name__ == 'dao_get_potential_notification_statistics_for_day' # noqa
|
||||
assert dao_get_template_statistics_for_service.__wrapped__.__name__ == 'dao_get_template_statistics_for_service' # noqa
|
||||
assert dao_get_template_statistics_for_template.__wrapped__.__name__ == 'dao_get_template_statistics_for_template' # noqa
|
||||
assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa
|
||||
assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa
|
||||
assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa
|
||||
@@ -63,6 +59,44 @@ def test_should_have_decorated_notifications_dao_functions():
|
||||
assert delete_notifications_created_more_than_a_week_ago.__wrapped__.__name__ == 'delete_notifications_created_more_than_a_week_ago' # noqa
|
||||
|
||||
|
||||
def test_should_be_able_to_get_template_usage_history(notify_db, notify_db_session, sample_service):
|
||||
with freeze_time('2000-01-01 12:00:00'):
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
notification = sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
results = dao_get_last_template_usage(sms.id)
|
||||
assert results.template.name == 'Template Name'
|
||||
assert results.template.template_type == 'sms'
|
||||
assert results.created_at == datetime(year=2000, month=1, day=1, hour=12, minute=0, second=0)
|
||||
assert results.template_id == sms.id
|
||||
assert results.id == notification.id
|
||||
|
||||
|
||||
def test_should_be_able_to_get_all_template_usage_history_order_by_notification_created_at(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
most_recent = sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
|
||||
results = dao_get_last_template_usage(sms.id)
|
||||
assert results.id == most_recent.id
|
||||
|
||||
|
||||
def test_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template(
|
||||
notify_db,
|
||||
notify_db_session):
|
||||
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
results = dao_get_last_template_usage(sms.id)
|
||||
assert not results
|
||||
|
||||
|
||||
def test_should_by_able_to_get_template_count_from_notifications_history(notify_db, notify_db_session, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
email = sample_email_template(notify_db, notify_db_session)
|
||||
@@ -425,7 +459,7 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
|
||||
assert data.template == notification_from_db.template
|
||||
assert data.template_version == notification_from_db.template_version
|
||||
assert data.created_at == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
assert {'name': 'Jo'} == notification_from_db.personalisation
|
||||
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
||||
|
||||
@@ -461,7 +495,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
||||
|
||||
stats = NotificationStatistics.query.filter(NotificationStatistics.service_id == sample_template.service.id).first()
|
||||
@@ -494,7 +528,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
||||
|
||||
stats = NotificationStatistics.query.filter(
|
||||
@@ -631,7 +665,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
assert Job.query.get(sample_job.id).notifications_sent == 1
|
||||
|
||||
notification_2 = Notification(**data)
|
||||
@@ -682,7 +716,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
assert job_1.id != job_2.id
|
||||
_assert_job_stats(job_id=job_1.id, sent=1, count=1)
|
||||
_assert_job_stats(job_id=job_2.id, sent=0, count=1)
|
||||
@@ -703,7 +737,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider):
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert data['created_at'] == notification_from_db.created_at
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
|
||||
|
||||
def test_get_notification(sample_notification):
|
||||
@@ -729,7 +763,7 @@ def test_save_notification_no_job_id(sample_template, mmg_provider):
|
||||
assert data['service'] == notification_from_db.service
|
||||
assert data['template'] == notification_from_db.template
|
||||
assert data['template_version'] == notification_from_db.template_version
|
||||
assert 'created' == notification_from_db.status
|
||||
assert notification_from_db.status == 'created'
|
||||
assert data.get('job_id') is None
|
||||
|
||||
|
||||
@@ -950,103 +984,6 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
|
||||
_assert_notification_stats(sample_template.service.id, sms_requested=3)
|
||||
|
||||
|
||||
@freeze_time("2016-03-30")
|
||||
def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(sample_template, sample_job):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
data = _notification_json(sample_template, job_id=sample_job.id)
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification, sample_template.template_type)
|
||||
|
||||
# move on one day
|
||||
with freeze_time('2016-03-31'):
|
||||
new_notification = Notification(**data)
|
||||
dao_create_notification(new_notification, sample_template.template_type)
|
||||
|
||||
# move on one more day
|
||||
with freeze_time('2016-04-01'):
|
||||
new_notification = Notification(**data)
|
||||
dao_create_notification(new_notification, sample_template.template_type)
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
|
||||
assert len(template_stats) == 3
|
||||
assert template_stats[0].day == date(2016, 4, 1)
|
||||
assert template_stats[1].day == date(2016, 3, 31)
|
||||
assert template_stats[2].day == date(2016, 3, 30)
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_stats_for_service_returns_stats_can_limit_number_of_days_returned(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# Make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
# Retrieve last week of stats
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=7)
|
||||
assert len(template_stats) == 8
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
# Final day of stats should be the same as today, eg Monday
|
||||
assert template_stats[0].day.isoweekday() == template_stats[7].day.isoweekday()
|
||||
assert template_stats[7].day == date(2016, 4, 2)
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_stats_for_service_returns_stats_returns_all_stats_if_no_limit(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
|
||||
assert len(template_stats) == 9
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[8].day == date(2016, 4, 1)
|
||||
|
||||
|
||||
@freeze_time('2016-04-30')
|
||||
def test_get_template_stats_for_service_returns_no_result_if_no_usage_within_limit_days(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# make 9 stats records from 1st to 9th April - no data after 10th
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
# Retrieve a week of stats - read date is 2016-04-30
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=7)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# Retrieve a month of stats - read date is 2016-04-30
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=30)
|
||||
assert len(template_stats) == 9
|
||||
|
||||
|
||||
def test_get_template_stats_for_service_with_limit_if_no_records_returns_empty_list(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id, limit_days=7)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
|
||||
@freeze_time("2016-01-10")
|
||||
def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, notify_db_session, sample_service):
|
||||
assert len(Notification.query.all()) == 0
|
||||
|
||||
@@ -13,7 +13,6 @@ from tests.app.conftest import sample_template as create_sample_template, sample
|
||||
def test_get_all_template_statistics_with_bad_arg_returns_400(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
@@ -40,7 +39,6 @@ def test_get_template_statistics_for_service(notify_db, notify_db_session, notif
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
@@ -77,7 +75,6 @@ def test_get_template_statistics_for_service_limited_by_day(notify_db, notify_db
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
@@ -144,7 +141,6 @@ def test_get_template_statistics_for_service_limited_by_day(notify_db, notify_db
|
||||
def test_returns_empty_list_if_no_templates_used(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
@@ -157,62 +153,47 @@ def test_returns_empty_list_if_no_templates_used(notify_api, sample_service):
|
||||
assert len(json_resp['data']) == 0
|
||||
|
||||
|
||||
def test_get_template_statistics_for_template_only_returns_for_provided_template(
|
||||
def test_get_template_statistics_by_id_returns_last_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notify_api,
|
||||
sample_service
|
||||
):
|
||||
template_1 = create_sample_template(
|
||||
sample_service):
|
||||
|
||||
template = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 1',
|
||||
service=sample_service
|
||||
)
|
||||
template_2 = create_sample_template(
|
||||
|
||||
notification_1 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 2',
|
||||
service=sample_service
|
||||
)
|
||||
|
||||
template_1_stats_1 = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
template_1_stats_2 = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 2)
|
||||
)
|
||||
template_2_stats = TemplateStatistics(
|
||||
template_id=template_2.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
|
||||
# separate commit to ensure stats_1 has earlier updated_at time
|
||||
db.session.add(template_1_stats_1)
|
||||
db.session.commit()
|
||||
|
||||
db.session.add_all([template_1_stats_2, template_2_stats])
|
||||
db.session.commit()
|
||||
service=sample_service,
|
||||
template=template)
|
||||
notification_2 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=template)
|
||||
notification_3 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=template)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template_1.id),
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['id'] == str(template_1_stats_2.id)
|
||||
assert json_resp['data'][1]['id'] == str(template_1_stats_1.id)
|
||||
json_resp = json.loads(response.get_data(as_text=True))['data']
|
||||
assert json_resp['id'] == str(notification_3.id)
|
||||
|
||||
|
||||
def test_get_template_statistics_for_template_returns_empty_if_no_statistics(
|
||||
@@ -221,36 +202,23 @@ def test_get_template_statistics_for_template_returns_empty_if_no_statistics(
|
||||
notify_api,
|
||||
sample_service
|
||||
):
|
||||
template_1 = create_sample_template(
|
||||
template = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 1',
|
||||
service=sample_service
|
||||
)
|
||||
template_2 = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 2',
|
||||
service=sample_service
|
||||
)
|
||||
|
||||
template_1_stats = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
db.session.add(template_1_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template_2.id),
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 404
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp['data'] == []
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message']['template_id'] == ['No template found for id {}'.format(template.id)]
|
||||
|
||||
Reference in New Issue
Block a user