Repository navigation
fix(environments): let the environments list take a page_size #8913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d5e8676
42c1b39
c08c154
7c850da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| from rest_framework.test import APIClient | ||
|
|
||
| from api_keys.models import MasterAPIKey | ||
| from app.pagination import CustomPagination | ||
| from audit.models import AuditLog, RelatedObjectType # type: ignore[attr-defined] | ||
| from core.constants import STRING | ||
| from environments.identities.models import Identity | ||
|
|
@@ -1395,3 +1396,114 @@ def test_create_environment__required_metadata_on_other_project__returns_201( | |
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_201_CREATED | ||
|
|
||
|
|
||
| def test_list_environments__returns_paginated_response( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| ) -> None: | ||
| # Given | ||
| environments = [ | ||
| Environment.objects.create(name=f"Env {i}", project=project) for i in range(11) | ||
| ] | ||
| another_project = Project.objects.create( | ||
| name="Other", organisation=project.organisation | ||
| ) | ||
| Environment.objects.create(name="Other Project Env", project=another_project) | ||
| url = reverse("api-v1:environments:environment-list") | ||
|
|
||
| # When | ||
| response = admin_client_new.get( | ||
| url, data={"project": project.id}, content_type="application/json" | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
| data = response.json() | ||
| assert data["count"] == 11 | ||
| assert len(data["results"]) == 11 | ||
| assert data["next"] is None | ||
| assert {environment["id"] for environment in data["results"]} == { | ||
| environment.id for environment in environments | ||
| } | ||
|
|
||
|
|
||
| def test_list_environments__page_size_param_is_respected( | ||
| admin_client_new: APIClient, | ||
| project: Project, | ||
| mocker: MockerFixture, | ||
| ) -> None: | ||
| # Given | ||
| environments = [ | ||
| Environment.objects.create(name=f"Env {i}", project=project) for i in range(5) | ||
| ] | ||
| mocker.patch.object(CustomPagination, "max_page_size", 3) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| another_project = Project.objects.create( | ||
| name="Other", organisation=project.organisation | ||
| ) | ||
| Environment.objects.create(name="Other Project Env", project=another_project) | ||
| url = reverse("api-v1:environments:environment-list") | ||
|
|
||
| # When | ||
| response = admin_client_new.get( | ||
| url, | ||
| data={"project": project.id, "page_size": 2, "page": 1}, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| content_type="application/json", | ||
| ) | ||
| page_2_response = admin_client_new.get( | ||
| url, | ||
| data={"project": project.id, "page_size": 2, "page": 2}, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| content_type="application/json", | ||
| ) | ||
| page_3_response = admin_client_new.get( | ||
| url, | ||
| data={"project": project.id, "page_size": 2, "page": 3}, | ||
| content_type="application/json", | ||
| ) | ||
| page_size_3_response = admin_client_new.get( | ||
| url, | ||
| data={"project": project.id, "page_size": 3}, | ||
| content_type="application/json", | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| max_page_size_response = admin_client_new.get( | ||
| url, | ||
| data={"project": project.id, "page_size": 10}, | ||
| content_type="application/json", | ||
| ) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_200_OK | ||
| assert page_2_response.status_code == status.HTTP_200_OK | ||
| assert page_3_response.status_code == status.HTTP_200_OK | ||
| assert page_size_3_response.status_code == status.HTTP_200_OK | ||
| assert max_page_size_response.status_code == status.HTTP_200_OK | ||
| data = response.json() | ||
| page_2_data = page_2_response.json() | ||
| page_3_data = page_3_response.json() | ||
| page_size_3_data = page_size_3_response.json() | ||
| max_page_size_data = max_page_size_response.json() | ||
| assert len(data["results"]) == 2 | ||
| assert data["count"] == 5 | ||
| assert data["next"] is not None | ||
| page_1_ids = {environment["id"] for environment in data["results"]} | ||
| page_2_ids = {environment["id"] for environment in page_2_data["results"]} | ||
| page_3_ids = {environment["id"] for environment in page_3_data["results"]} | ||
| assert len(page_2_data["results"]) == 2 | ||
| assert page_2_data["next"] is not None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| assert len(page_3_data["results"]) == 1 | ||
| assert page_3_data["next"] is None | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| page_size_3_ids = {environment["id"] for environment in page_size_3_data["results"]} | ||
| assert len(page_size_3_ids) == 3 | ||
| assert page_size_3_ids.issubset({environment.id for environment in environments}) | ||
| assert len(max_page_size_data["results"]) == 3 | ||
| assert max_page_size_data["next"] is not None | ||
| assert page_1_ids.isdisjoint(page_2_ids) | ||
| assert page_1_ids.isdisjoint(page_3_ids) | ||
| assert page_2_ids.isdisjoint(page_3_ids) | ||
| assert page_1_ids | page_2_ids | page_3_ids == { | ||
| environment.id for environment in environments | ||
| } | ||
| assert page_size_3_data["count"] == 5 | ||
| assert {environment["id"] for environment in page_size_3_data["results"]}.issubset( | ||
| {environment.id for environment in environments} | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { skipToken } from '@reduxjs/toolkit/query' | |
| import { BillingPeriod } from 'common/types/requests' | ||
| import { useGetOrganisationUsageQuery } from 'common/services/useOrganisationUsage' | ||
| import { useGetProjectsQuery } from 'common/services/useProject' | ||
| import { useGetAllEnvironmentsQuery } from 'common/services/useEnvironment' | ||
| import { useGetEnvironmentsQuery } from 'common/services/useEnvironment' | ||
| import { breakdownStatusOf, retryFailed } from './breakdownView' | ||
| import { | ||
| byScope, | ||
|
|
@@ -50,7 +50,7 @@ export const useGroupedBreakdown = ({ | |
| const projects = useGetProjectsQuery( | ||
| groupBy === 'project' && organisationId ? { organisationId } : skipToken, | ||
| ) | ||
| const environments = useGetAllEnvironmentsQuery( | ||
| const environments = useGetEnvironmentsQuery( | ||
| groupBy === 'environment' && projectId ? { projectId } : skipToken, | ||
|
Comment on lines
+53
to
54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ) | ||
|
|
||
|
|
@@ -67,7 +67,7 @@ export const useGroupedBreakdown = ({ | |
| return byScope( | ||
| grouped.currentData, | ||
| 'environment_id', | ||
| new Map(environments.currentData?.map(({ id, name }) => [id, name])), | ||
| new Map(environments.currentData?.results?.map(({ id, name }) => [id, name])), | ||
| 'Deleted environments', | ||
| ) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
sed -n '1390,1520p' api/tests/unit/environments/test_unit_environments_views.py cat api/app/pagination.pyRepository: Flagsmith/flagsmith
Length of output: 7849
Script executed:
Repository: Flagsmith/flagsmith
Length of output: 12255
Script executed:
Repository: Flagsmith/flagsmith
Length of output: 12255
Cover the 999-result default boundary.
The current test detects the former 10-result default, but not a change from 999 to 11. Consider a separate test with 1,000 environments that checks for 999 results and a next page.