-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed N+1 queries issue in /api/v1/bed/ (#1908)
* Fixed N+1 queries issue in /api/v1/bed/ * Feat: PR review changes for issue #1338 Removed is_occupied property from Bed model as it is not used anywhere Annotated is_occupied field in the queryset instead of _is_occupied * Feat: PR review changes Changed is_occupied from SerializerMethodField to Boolean Field * Feat: PR Review Changes Annotated is_occupied field irrespective of action * Added the test case for listing beds API * add comment explaining number of queries --------- Co-authored-by: Aakash Singh <[email protected]>
- Loading branch information
1 parent
48f1d89
commit 8ca6e55
Showing
3 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from care.facility.models import Bed | ||
from care.utils.tests.test_utils import TestUtils | ||
|
||
|
||
class BedViewSetTestCase(TestUtils, APITestCase): | ||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
cls.state = cls.create_state() | ||
cls.district = cls.create_district(cls.state) | ||
cls.local_body = cls.create_local_body(cls.district) | ||
cls.super_user = cls.create_super_user("su", cls.district) | ||
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body) | ||
cls.asset_location = cls.create_asset_location(cls.facility) | ||
cls.user = cls.create_user("staff", cls.district, home_facility=cls.facility) | ||
cls.patient = cls.create_patient( | ||
cls.district, cls.facility, local_body=cls.local_body | ||
) | ||
|
||
def setUp(self) -> None: | ||
super().setUp() | ||
self.bed1 = Bed.objects.create( | ||
name="bed1", location=self.asset_location, facility=self.facility | ||
) | ||
self.bed2 = Bed.objects.create( | ||
name="bed2", location=self.asset_location, facility=self.facility | ||
) | ||
self.bed3 = Bed.objects.create( | ||
name="bed3", location=self.asset_location, facility=self.facility | ||
) | ||
|
||
def test_list_beds(self): | ||
# includes 3 queries for auth and 1 for pagination count | ||
with self.assertNumQueries(5): | ||
response = self.client.get("/api/v1/bed/") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |