mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Merge pull request #1569 from GSA/feat/fe-tz-change
Updated endpoint to allow hourly updates to enable utc => local time conversion
This commit is contained in:
@@ -3,6 +3,7 @@ from datetime import date, datetime
|
||||
import pytest
|
||||
|
||||
from app.dao.date_util import (
|
||||
generate_hourly_range,
|
||||
get_calendar_year,
|
||||
get_calendar_year_for_datetime,
|
||||
get_month_start_and_end_date_in_utc,
|
||||
@@ -75,3 +76,46 @@ def test_get_month_start_and_end_date_in_utc(month, year, expected_start, expect
|
||||
)
|
||||
def test_get_calendar_year_for_datetime(dt, fy):
|
||||
assert get_calendar_year_for_datetime(dt) == fy
|
||||
|
||||
|
||||
def test_generate_hourly_range_with_end_date():
|
||||
start_date = datetime(2025, 2, 18, 12, 0)
|
||||
end_date = datetime(2025, 2, 18, 15, 0)
|
||||
result = list(generate_hourly_range(start_date, end_date=end_date))
|
||||
|
||||
expected = [
|
||||
datetime(2025, 2, 18, 12, 0),
|
||||
datetime(2025, 2, 18, 13, 0),
|
||||
datetime(2025, 2, 18, 14, 0),
|
||||
datetime(2025, 2, 18, 15, 0),
|
||||
]
|
||||
|
||||
assert result == expected, f"Expected {expected}, but got {result}"
|
||||
|
||||
|
||||
def test_generate_hourly_range_with_hours():
|
||||
start_date = datetime(2025, 2, 18, 12, 0)
|
||||
result = list(generate_hourly_range(start_date, hours=3))
|
||||
|
||||
expected = [
|
||||
datetime(2025, 2, 18, 12, 0),
|
||||
datetime(2025, 2, 18, 13, 0),
|
||||
datetime(2025, 2, 18, 14, 0),
|
||||
]
|
||||
|
||||
assert result == expected, f"Expected {expected}, but got {result}"
|
||||
|
||||
|
||||
def test_generate_hourly_range_with_zero_hours():
|
||||
start_date = datetime(2025, 2, 18, 12, 0)
|
||||
result = list(generate_hourly_range(start_date, hours=0))
|
||||
|
||||
assert result == [], f"Expected an empty list, but got {result}"
|
||||
|
||||
|
||||
def test_generate_hourly_range_with_end_date_before_start():
|
||||
start_date = datetime(2025, 2, 18, 12, 0)
|
||||
end_date = datetime(2025, 2, 18, 10, 0)
|
||||
result = list(generate_hourly_range(start_date, end_date=end_date))
|
||||
|
||||
assert result == [], f"Expected empty list, but got {result}"
|
||||
|
||||
86
tests/app/dao/test_services_get_specific_hours.py
Normal file
86
tests/app/dao/test_services_get_specific_hours.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from collections import namedtuple
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app.dao.services_dao import get_specific_hours_stats
|
||||
from app.enums import StatisticsType
|
||||
from app.models import TemplateType
|
||||
|
||||
NotificationRow = namedtuple("NotificationRow", ["notification_type", "status", "timestamp", "count"])
|
||||
|
||||
|
||||
def generate_expected_hourly_output(requested_sms_hours):
|
||||
return {
|
||||
hour: {
|
||||
TemplateType.SMS: {
|
||||
StatisticsType.REQUESTED: 1,
|
||||
StatisticsType.DELIVERED: 0,
|
||||
StatisticsType.FAILURE: 0,
|
||||
StatisticsType.PENDING: 0,
|
||||
},
|
||||
TemplateType.EMAIL: {
|
||||
StatisticsType.REQUESTED: 0,
|
||||
StatisticsType.DELIVERED: 0,
|
||||
StatisticsType.FAILURE: 0,
|
||||
StatisticsType.PENDING: 0,
|
||||
},
|
||||
}
|
||||
for hour in requested_sms_hours
|
||||
}
|
||||
|
||||
|
||||
def create_mock_notification(notification_type, status, timestamp, count=1):
|
||||
"""
|
||||
Creates a named tuple with the attributes required by format_statistics.
|
||||
"""
|
||||
return NotificationRow(
|
||||
notification_type=notification_type,
|
||||
status=status,
|
||||
timestamp=timestamp.replace(minute=0, second=0, microsecond=0),
|
||||
count=count
|
||||
)
|
||||
|
||||
|
||||
test_cases = [
|
||||
(
|
||||
[create_mock_notification(
|
||||
TemplateType.SMS,
|
||||
StatisticsType.REQUESTED,
|
||||
datetime(2025, 2, 18, 14, 15, 0),
|
||||
)],
|
||||
datetime(2025, 2, 18, 12, 0),
|
||||
6,
|
||||
generate_expected_hourly_output(["2025-02-18T14:00:00Z"]),
|
||||
),
|
||||
(
|
||||
[create_mock_notification(
|
||||
TemplateType.SMS,
|
||||
StatisticsType.REQUESTED,
|
||||
datetime(2025, 2, 18, 17, 59, 59),
|
||||
)],
|
||||
datetime(2025, 2, 18, 15, 0),
|
||||
3,
|
||||
generate_expected_hourly_output(["2025-02-18T17:00:00Z"]),
|
||||
),
|
||||
([], datetime(2025, 2, 18, 10, 0), 4, {}),
|
||||
(
|
||||
[
|
||||
create_mock_notification(TemplateType.SMS, StatisticsType.REQUESTED, datetime(2025, 2, 18, 9, 30, 0)),
|
||||
create_mock_notification(TemplateType.SMS, StatisticsType.REQUESTED, datetime(2025, 2, 18, 11, 45, 0)),
|
||||
],
|
||||
datetime(2025, 2, 18, 8, 0),
|
||||
5,
|
||||
generate_expected_hourly_output(["2025-02-18T09:00:00Z", "2025-02-18T11:00:00Z"]),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mocked_notifications, start_date, hours, expected_output", test_cases)
|
||||
def test_get_specific_hours(mocked_notifications, start_date, hours, expected_output):
|
||||
results = get_specific_hours_stats(
|
||||
mocked_notifications,
|
||||
start_date,
|
||||
hours=hours
|
||||
)
|
||||
assert results == expected_output, f"Expected {expected_output}, but got {results}"
|
||||
Reference in New Issue
Block a user