mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
add tests for dao
This commit is contained in:
@@ -3,7 +3,6 @@ import pytest
|
||||
import uuid
|
||||
from datetime import (datetime, date)
|
||||
|
||||
import pytest
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import uuid
|
||||
import pytest
|
||||
|
||||
from sqlalchemy.orm.exc import FlushError, NoResultFound
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app import db
|
||||
from app.dao.services_dao import (
|
||||
dao_create_service,
|
||||
dao_add_user_to_service,
|
||||
@@ -8,7 +13,8 @@ from app.dao.services_dao import (
|
||||
dao_fetch_service_by_id,
|
||||
dao_fetch_all_services_by_user,
|
||||
dao_update_service,
|
||||
delete_service_and_all_associated_db_objects
|
||||
delete_service_and_all_associated_db_objects,
|
||||
dao_fetch_stats_for_service
|
||||
)
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.models import (
|
||||
@@ -20,13 +26,16 @@ from app.models import (
|
||||
Template,
|
||||
Job,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
Permission,
|
||||
User,
|
||||
InvitedUser,
|
||||
Service
|
||||
)
|
||||
from sqlalchemy.orm.exc import FlushError, NoResultFound
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from tests.app.conftest import (
|
||||
sample_notification as create_notification
|
||||
)
|
||||
|
||||
|
||||
def test_create_service(sample_user):
|
||||
@@ -362,3 +371,52 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions(sam
|
||||
|
||||
other_user_service_two_permissions = Permission.query.filter_by(service=service_two, user=other_user).all()
|
||||
assert len(other_user_service_two_permissions) == 8
|
||||
|
||||
|
||||
def test_fetch_stats_filters_on_service(sample_notification):
|
||||
service_two = Service(name="service_two",
|
||||
created_by=sample_notification.service.created_by,
|
||||
email_from="hello",
|
||||
active=False,
|
||||
restricted=False,
|
||||
message_limit=1000)
|
||||
dao_create_service(service_two, sample_notification.service.created_by)
|
||||
|
||||
stats = dao_fetch_stats_for_service(service_two.id)
|
||||
assert len(stats) == 0
|
||||
|
||||
|
||||
def test_fetch_stats_ignores_historical_notification_data(sample_notification):
|
||||
service_id = sample_notification.service.id
|
||||
|
||||
db.session.delete(sample_notification)
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
stats = dao_fetch_stats_for_service(service_id)
|
||||
assert len(stats) == 0
|
||||
|
||||
|
||||
def test_fetch_stats_counts_correctly(notify_db, notify_db_session, sample_template, sample_email_template):
|
||||
# two created email, one failed email, and one created sms
|
||||
create_notification(notify_db, notify_db_session, template=sample_email_template, status='created')
|
||||
create_notification(notify_db, notify_db_session, template=sample_email_template, status='created')
|
||||
create_notification(notify_db, notify_db_session, template=sample_email_template, status='technical-failure')
|
||||
create_notification(notify_db, notify_db_session, template=sample_template, status='created')
|
||||
|
||||
stats = dao_fetch_stats_for_service(sample_template.service.id)
|
||||
stats = sorted(stats, key=lambda x: (x.notification_type, x.status))
|
||||
assert len(stats) == 3
|
||||
|
||||
assert stats[0].notification_type == 'email'
|
||||
assert stats[0].status == 'created'
|
||||
assert stats[0].count == 2
|
||||
|
||||
assert stats[1].notification_type == 'email'
|
||||
assert stats[1].status == 'technical-failure'
|
||||
assert stats[1].count == 1
|
||||
|
||||
assert stats[2].notification_type == 'sms'
|
||||
assert stats[2].status == 'created'
|
||||
assert stats[2].count == 1
|
||||
|
||||
@@ -15,7 +15,9 @@ from tests.app.conftest import (
|
||||
sample_user as create_sample_user,
|
||||
sample_notification as create_sample_notification
|
||||
)
|
||||
from app.service.rest import format_statistics
|
||||
|
||||
|
||||
Row = collections.namedtuple('row', ('notification_type', 'status', 'count'))
|
||||
|
||||
|
||||
def test_get_service_list(notify_api, service_factory):
|
||||
@@ -1092,7 +1094,7 @@ def test_set_sms_sender_for_service_rejects_invalid_characters(notify_api, sampl
|
||||
assert result['message'] == {'sms_sender': ['Only alphanumeric characters allowed']}
|
||||
|
||||
|
||||
def test_get_detailed_service(notify_api, sample_service):
|
||||
def test_get_detailed_service(notify_api, sample_service, sample_notification):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
resp = client.get(
|
||||
'/service/{}?detailed=False'.format(sample_service.id),
|
||||
@@ -1105,15 +1107,12 @@ def test_get_detailed_service(notify_api, sample_service):
|
||||
assert 'statistics' in service.keys()
|
||||
assert set(service['statistics'].keys()) == set(['sms', 'email'])
|
||||
assert service['statistics']['sms'] == {
|
||||
'requested': 0,
|
||||
'requested': 1,
|
||||
'delivered': 0,
|
||||
'failed': 0
|
||||
}
|
||||
|
||||
|
||||
Row = collections.namedtuple('row', ('notification_type', 'status', 'count'))
|
||||
|
||||
|
||||
# email_counts and sms_counts are 3-tuple of requested, delivered, failed
|
||||
@pytest.mark.idparametrize('stats, email_counts, sms_counts', {
|
||||
'empty': ([], [0, 0, 0], [0, 0, 0]),
|
||||
@@ -1133,6 +1132,8 @@ Row = collections.namedtuple('row', ('notification_type', 'status', 'count'))
|
||||
], [4, 0, 4], [0, 0, 0]),
|
||||
})
|
||||
def test_format_statistics(stats, email_counts, sms_counts):
|
||||
from app.service.rest import format_statistics
|
||||
|
||||
ret = format_statistics(stats)
|
||||
|
||||
assert ret['email'] == {
|
||||
|
||||
Reference in New Issue
Block a user