2018-06-04 17:29:58 +01:00
|
|
|
import uuid
|
2018-06-07 16:01:41 +01:00
|
|
|
from datetime import datetime, timedelta
|
2018-06-04 17:29:58 +01:00
|
|
|
|
2018-07-02 17:19:23 +01:00
|
|
|
from app.dao.complaint_dao import (
|
|
|
|
|
fetch_complaints_by_service,
|
|
|
|
|
fetch_count_of_complaints,
|
|
|
|
|
fetch_paginated_complaints,
|
|
|
|
|
save_complaint,
|
|
|
|
|
)
|
2024-02-21 10:24:18 -05:00
|
|
|
from app.enums import TemplateType
|
2018-06-04 17:29:58 +01:00
|
|
|
from app.models import Complaint
|
2024-05-23 13:59:51 -07:00
|
|
|
from app.utils import utc_now
|
2021-03-10 13:55:06 +00:00
|
|
|
from tests.app.db import (
|
|
|
|
|
create_complaint,
|
|
|
|
|
create_notification,
|
|
|
|
|
create_service,
|
|
|
|
|
create_template,
|
|
|
|
|
)
|
2018-06-04 17:29:58 +01:00
|
|
|
|
|
|
|
|
|
2018-07-02 17:19:23 +01:00
|
|
|
def test_fetch_paginated_complaints(mocker, sample_email_notification):
|
2023-08-29 14:54:30 -07:00
|
|
|
mocker.patch.dict("app.dao.complaint_dao.current_app.config", {"PAGE_SIZE": 2})
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 1, 1),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 1, 2),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 1, 3),
|
|
|
|
|
)
|
2018-07-02 17:19:23 +01:00
|
|
|
|
|
|
|
|
res = fetch_paginated_complaints(page=1)
|
|
|
|
|
|
|
|
|
|
assert len(res.items) == 2
|
|
|
|
|
assert res.items[0].created_at == datetime(2018, 1, 3)
|
|
|
|
|
assert res.items[1].created_at == datetime(2018, 1, 2)
|
|
|
|
|
|
|
|
|
|
res = fetch_paginated_complaints(page=2)
|
|
|
|
|
|
|
|
|
|
assert len(res.items) == 1
|
|
|
|
|
assert res.items[0].created_at == datetime(2018, 1, 1)
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def test_fetch_complaint_by_service_returns_one(
|
|
|
|
|
sample_service, sample_email_notification
|
|
|
|
|
):
|
|
|
|
|
complaint = Complaint(
|
|
|
|
|
notification_id=sample_email_notification.id,
|
|
|
|
|
service_id=sample_service.id,
|
|
|
|
|
ses_feedback_id=str(uuid.uuid4()),
|
|
|
|
|
complaint_type="abuse",
|
2024-05-23 13:59:51 -07:00
|
|
|
complaint_date=utc_now(),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2018-06-04 17:29:58 +01:00
|
|
|
|
|
|
|
|
save_complaint(complaint)
|
|
|
|
|
|
|
|
|
|
complaints = fetch_complaints_by_service(service_id=sample_service.id)
|
|
|
|
|
assert len(complaints) == 1
|
|
|
|
|
assert complaints[0] == complaint
|
|
|
|
|
|
|
|
|
|
|
2018-06-05 14:25:24 +01:00
|
|
|
def test_fetch_complaint_by_service_returns_empty_list(sample_service):
|
2018-06-04 17:29:58 +01:00
|
|
|
complaints = fetch_complaints_by_service(service_id=sample_service.id)
|
|
|
|
|
assert len(complaints) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_complaint_by_service_return_many(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
service_1 = create_service(service_name="first")
|
|
|
|
|
service_2 = create_service(service_name="second")
|
2024-02-21 10:24:18 -05:00
|
|
|
template_1 = create_template(service=service_1, template_type=TemplateType.EMAIL)
|
|
|
|
|
template_2 = create_template(service=service_2, template_type=TemplateType.EMAIL)
|
2018-06-04 17:29:58 +01:00
|
|
|
notification_1 = create_notification(template=template_1)
|
|
|
|
|
notification_2 = create_notification(template=template_2)
|
|
|
|
|
notification_3 = create_notification(template=template_2)
|
2023-08-29 14:54:30 -07:00
|
|
|
complaint_1 = Complaint(
|
|
|
|
|
notification_id=notification_1.id,
|
|
|
|
|
service_id=service_1.id,
|
|
|
|
|
ses_feedback_id=str(uuid.uuid4()),
|
|
|
|
|
complaint_type="abuse",
|
2024-05-23 13:59:51 -07:00
|
|
|
complaint_date=utc_now(),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
|
|
|
|
complaint_2 = Complaint(
|
|
|
|
|
notification_id=notification_2.id,
|
|
|
|
|
service_id=service_2.id,
|
|
|
|
|
ses_feedback_id=str(uuid.uuid4()),
|
|
|
|
|
complaint_type="abuse",
|
2024-05-23 13:59:51 -07:00
|
|
|
complaint_date=utc_now(),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
|
|
|
|
complaint_3 = Complaint(
|
|
|
|
|
notification_id=notification_3.id,
|
|
|
|
|
service_id=service_2.id,
|
|
|
|
|
ses_feedback_id=str(uuid.uuid4()),
|
|
|
|
|
complaint_type="abuse",
|
2024-05-23 13:59:51 -07:00
|
|
|
complaint_date=utc_now(),
|
|
|
|
|
created_at=utc_now() + timedelta(minutes=1),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2018-06-04 17:29:58 +01:00
|
|
|
|
|
|
|
|
save_complaint(complaint_1)
|
|
|
|
|
save_complaint(complaint_2)
|
|
|
|
|
save_complaint(complaint_3)
|
|
|
|
|
|
|
|
|
|
complaints = fetch_complaints_by_service(service_id=service_2.id)
|
|
|
|
|
assert len(complaints) == 2
|
2018-06-07 16:01:41 +01:00
|
|
|
assert complaints[0] == complaint_3
|
|
|
|
|
assert complaints[1] == complaint_2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fetch_count_of_complaints(sample_email_notification):
|
2023-08-29 14:54:30 -07:00
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 6, 7, 2, 00, 00),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 6, 7, 3, 00, 00),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 6, 7, 5, 00, 00),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 6, 7, 13, 00, 00),
|
|
|
|
|
)
|
|
|
|
|
create_complaint(
|
|
|
|
|
service=sample_email_notification.service,
|
|
|
|
|
notification=sample_email_notification,
|
|
|
|
|
created_at=datetime(2018, 6, 7, 23),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
count_of_complaints = fetch_count_of_complaints(
|
2024-02-21 10:24:45 -05:00
|
|
|
start_date=datetime(2018, 6, 7),
|
|
|
|
|
end_date=datetime(2018, 6, 7),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2023-05-10 08:39:50 -07:00
|
|
|
assert count_of_complaints == 5
|
2018-06-07 16:01:41 +01:00
|
|
|
|
|
|
|
|
|
2022-05-03 17:00:51 +01:00
|
|
|
def test_fetch_count_of_complaints_returns_zero(notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
count_of_complaints = fetch_count_of_complaints(
|
2024-02-21 10:24:45 -05:00
|
|
|
start_date=datetime(2018, 6, 7),
|
|
|
|
|
end_date=datetime(2018, 6, 7),
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2018-06-07 16:01:41 +01:00
|
|
|
assert count_of_complaints == 0
|