Compare commits

...

3 Commits

Author SHA1 Message Date
Beverly Nguyen
e5885f674b Fixed pagination logic to prevent infinite next button 2025-07-29 10:25:43 -07:00
Beverly Nguyen
d65be41da5 fix test get_immediate_jobs for download availability check 2025-07-29 10:17:35 -07:00
Beverly Nguyen
eac2733a33 flake8 2025-07-29 09:57:59 -07:00
4 changed files with 58 additions and 7 deletions

View File

@@ -97,10 +97,12 @@ def handle_pagination(jobs, service_id, page):
else None
)
total_items = jobs.get("total", 0)
page_size = jobs.get("page_size", 50)
total_pages = (total_items + page_size - 1) // page_size
has_next_link = jobs.get("links", {}).get("next") is not None
next_page = (
generate_next_dict("main.all_jobs_activity", service_id, page)
if has_next_link and total_items > 50
if has_next_link and total_items > 50 and page < total_pages
else None
)
pagination = generate_pagination_pages(

View File

@@ -254,10 +254,13 @@ def get_notifications(service_id, message_type, status_override=None): # noqa
next_page = None
total_items = notifications.get("total", 0)
page_size = notifications.get("page_size", 50)
total_pages = (total_items + page_size - 1) // page_size
if (
"links" in notifications
and notifications["links"].get("next", None)
and total_items > 50
and page < total_pages
):
next_page = generate_next_dict(
"main.view_notifications", service_id, page, url_args

View File

@@ -297,7 +297,6 @@ def test_download_links_show_when_data_available(
"total": 1,
"page_size": 50
}
mock_jobs_empty = {"data": [], "total": 0, "page_size": 50}
mocker.patch("app.job_api_client.get_page_of_jobs", return_value=mock_jobs_with_data)
mocker.patch("app.job_api_client.get_immediate_jobs", return_value=[{"id": "job1"}])
@@ -563,7 +562,7 @@ def test_should_show_notifications_for_a_service_with_next_previous(
"app.notification_api_client.get_notifications_for_service",
return_value=notification_json(
service_one["id"], rows=50, with_links=True
) | {"total": 100},
) | {"total": 150},
)
page = client_request.get(
"main.view_notifications",
@@ -598,6 +597,35 @@ def test_should_show_notifications_for_a_service_with_next_previous(
assert "page 1" in prev_page_link.text.strip()
def test_doesnt_show_next_button_on_last_page(
client_request,
service_one,
active_user_with_permissions,
mock_get_service_statistics,
mock_get_service_data_retention,
mock_get_no_api_keys,
mocker,
):
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=notification_json(
service_one["id"], rows=50, with_links=True
) | {"total": 100},
)
page = client_request.get(
"main.view_notifications",
service_id=service_one["id"],
message_type="sms",
page=2,
)
next_page_link = page.find("a", {"rel": "next"})
prev_page_link = page.find("a", {"rel": "previous"})
assert next_page_link is None
assert prev_page_link is not None
def test_doesnt_show_pagination_when_50_or_fewer_items(
client_request,
service_one,

View File

@@ -50,6 +50,9 @@ def test_all_activity(
mock_get_page_of_jobs = mocker.patch(
"app.job_api_client.get_page_of_jobs", return_value=MOCK_JOBS
)
mocker.patch(
"app.job_api_client.get_immediate_jobs", return_value=[]
)
response = client_request.get_response(
"main.all_jobs_activity",
@@ -60,7 +63,11 @@ def test_all_activity(
assert response.data is not None, "Response data is None"
assert "All activity" in response.text
mock_get_page_of_jobs.assert_called_with(SERVICE_ONE_ID, page=current_page)
assert any(
call[0][0] == SERVICE_ONE_ID and call[1].get('page') == current_page
for call in mock_get_page_of_jobs.call_args_list
)
page = BeautifulSoup(response.data, "html.parser")
table = page.find("table")
assert table is not None, "Table not found in the response"
@@ -115,7 +122,6 @@ def test_all_activity(
failed_cell = cells[6].get_text(strip=True)
assert failed_cell == "5", f"Expected failed count '5', but got '{failed_cell}'"
mock_get_page_of_jobs.assert_called_with(SERVICE_ONE_ID, page=current_page)
def test_all_activity_no_jobs(client_request, mocker):
@@ -133,6 +139,9 @@ def test_all_activity_no_jobs(client_request, mocker):
"total": 0,
},
)
mocker.patch(
"app.job_api_client.get_immediate_jobs", return_value=[]
)
response = client_request.get_response(
"main.all_jobs_activity",
service_id=SERVICE_ONE_ID,
@@ -152,7 +161,10 @@ def test_all_activity_no_jobs(client_request, mocker):
assert (
expected_message == actual_message
), f"Expected message '{expected_message}', but got '{actual_message}'"
mock_get_page_of_jobs.assert_called_with(SERVICE_ONE_ID, page=current_page)
assert any(
call[0][0] == SERVICE_ONE_ID and call[1].get('page') == current_page
for call in mock_get_page_of_jobs.call_args_list
)
def test_all_activity_pagination(client_request, mocker):
@@ -182,13 +194,19 @@ def test_all_activity_pagination(client_request, mocker):
"total": 100,
},
)
mocker.patch(
"app.job_api_client.get_immediate_jobs", return_value=[]
)
response = client_request.get_response(
"main.all_jobs_activity",
service_id=SERVICE_ONE_ID,
page=current_page,
)
mock_get_page_of_jobs.assert_called_with(SERVICE_ONE_ID, page=current_page)
assert any(
call[0][0] == SERVICE_ONE_ID and call[1].get('page') == current_page
for call in mock_get_page_of_jobs.call_args_list
)
page = BeautifulSoup(response.data, "html.parser")
pagination_controls = page.find_all("li", class_="usa-pagination__item")