Skip to content

Commit

Permalink
Optimize queries. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hipek8 committed Jun 10, 2024
1 parent 051650b commit 66aecc4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
run: pip3 install psycopg2-binary

# Runs a set of commands using the runners shell
- name: Check missings migrations
- name: Check missing migrations
run: "${GITHUB_WORKSPACE}/scripts/check_missing_migrations.sh"

settings_test:
Expand Down
3 changes: 2 additions & 1 deletion src/ralph/back_office/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ class BackOfficeAssetViewSet(RalphAPIViewSet):
select_related = BackOfficeAssetAdmin.list_select_related + [
'service_env', 'service_env__service', 'service_env__environment',
'user', 'owner', 'property_of', 'office_infrastructure',
'budget_info'
'budget_info',
]
prefetch_related = base_object_descendant_prefetch_related + [
'user__groups', 'user__user_permissions',
'service_env__service__environments',
'service_env__service__business_owners',
'service_env__service__technical_owners',
'tags',
'content_type',
]
queryset = BackOfficeAsset.objects.all()
serializer_class = BackOfficeAssetSerializer
Expand Down
14 changes: 14 additions & 0 deletions src/ralph/back_office/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def test_get_back_office_assets_list(self):
response.data['count'], BackOfficeAsset.objects.count()
)

def test_get_back_office_assets_list_query_count(self):
self.bo_asset = BackOfficeAssetFactory.create_batch(
99,
warehouse=self.warehouse,
model=self.model,
)
url = reverse('backofficeasset-list')
with self.assertNumQueries(10):
response = self.client.get(url + "?limit=100", format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data['count'], BackOfficeAsset.objects.count()
)

def test_get_back_office_asset_details(self):
url = reverse('backofficeasset-detail', args=(self.bo_asset.id,))
response = self.client.get(url, format='json')
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/data_center/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def setUp(self):
def test_get_data_center_assets_list(self):
url = reverse('datacenterasset-list')
DataCenterAssetFullFactory.create_batch(18, rack=self.rack)
with self.assertNumQueries(17):
with self.assertNumQueries(18):
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/security/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Meta:


class VulnerabilityViewSet(RalphAPIViewSet):
queryset = Vulnerability.objects.all()
queryset = Vulnerability.objects.all().prefetch_related('tags')
serializer_class = VulnerabilitySerializer
filter_fields = [
'external_vulnerability_id',
Expand Down
10 changes: 10 additions & 0 deletions src/ralph/security/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ def test_get_vulnerability_list(self):
response.data['count'], Vulnerability.objects.count()
)

def test_get_vulnerability_list_query_count(self):
VulnerabilityFactory.create_batch(99)
url = reverse('vulnerability-list')
with self.assertNumQueries(5):
response = self.client.get(url + "?limit=100", format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data['count'], Vulnerability.objects.count()
)

def test_get_vulnerability_details(self):
url = reverse('vulnerability-detail', args=(self.vulnerability.id,))
response = self.client.get(url, format='json')
Expand Down

0 comments on commit 66aecc4

Please sign in to comment.