Skip to content

Commit

Permalink
fixes validation preventing linking multiple cameras to a bed (#2559)
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad authored Oct 23, 2024
1 parent 8bac44a commit 3bd5e4e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
17 changes: 9 additions & 8 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def validate(self, attrs):
not facilities.filter(id=asset.current_location.facility.id).exists()
) or (not facilities.filter(id=bed.facility.id).exists()):
raise PermissionError
if AssetBed.objects.filter(asset=asset, bed=bed).exists():
raise ValidationError(
{"non_field_errors": "Asset is already linked to bed"}
)
if asset.asset_class not in [
AssetClasses.HL7MONITOR.name,
AssetClasses.ONVIF.name,
Expand All @@ -123,18 +127,15 @@ def validate(self, attrs):
{"asset": "Should be in the same facility as the bed"}
)
if (
asset.asset_class
in [
AssetClasses.HL7MONITOR.name,
AssetClasses.ONVIF.name,
]
asset.asset_class == AssetClasses.HL7MONITOR.name
and AssetBed.objects.filter(
bed=bed, asset__asset_class=asset.asset_class
).exists()
) and AssetBed.objects.filter(
bed=bed, asset__asset_class=asset.asset_class
).exists():
raise ValidationError(
{
"asset": "Bed is already in use by another asset of the same class"
}
{"asset": "Another HL7 Monitor is already linked to this bed."}
)
else:
raise ValidationError(
Expand Down
36 changes: 36 additions & 0 deletions care/facility/tests/test_asset_bed_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ def setUpTestData(cls):
)
cls.asset_location = cls.create_asset_location(cls.facility)
cls.asset = cls.create_asset(cls.asset_location)
cls.monitor_asset_1 = cls.create_asset(
cls.asset_location, asset_class=AssetClasses.HL7MONITOR.name
)
cls.monitor_asset_2 = cls.create_asset(
cls.asset_location, asset_class=AssetClasses.HL7MONITOR.name
)
cls.camera_asset = cls.create_asset(
cls.asset_location, asset_class=AssetClasses.ONVIF.name
)
cls.camera_asset_1 = cls.create_asset(
cls.asset_location, asset_class=AssetClasses.ONVIF.name, name="Camera 1"
)
cls.camera_asset_2 = cls.create_asset(
cls.asset_location, asset_class=AssetClasses.ONVIF.name, name="Camera 2"
)
cls.bed = cls.create_bed(cls.facility, cls.asset_location)

def test_link_disallowed_asset_class_asset_to_bed(self):
Expand All @@ -49,6 +61,30 @@ def test_link_asset_to_bed_and_attempt_duplicate_linking(self):
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(res.data["count"], 1)

def test_linking_multiple_cameras_to_a_bed(self):
data = {
"asset": self.camera_asset_1.external_id,
"bed": self.bed.external_id,
}
res = self.client.post("/api/v1/assetbed/", data)
self.assertEqual(res.status_code, status.HTTP_201_CREATED)
# Attempt linking another camera to same bed.
data["asset"] = self.camera_asset_2.external_id
res = self.client.post("/api/v1/assetbed/", data)
self.assertEqual(res.status_code, status.HTTP_201_CREATED)

def test_linking_multiple_hl7_monitors_to_a_bed(self):
data = {
"asset": self.monitor_asset_1.external_id,
"bed": self.bed.external_id,
}
res = self.client.post("/api/v1/assetbed/", data)
self.assertEqual(res.status_code, status.HTTP_201_CREATED)
# Attempt linking another hl7 monitor to same bed.
data["asset"] = self.monitor_asset_2.external_id
res = self.client.post("/api/v1/assetbed/", data)
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)


class AssetBedCameraPresetViewSetTestCase(TestUtils, APITestCase):
@classmethod
Expand Down

0 comments on commit 3bd5e4e

Please sign in to comment.