Skip to content

Commit

Permalink
Added warranty filter to Assets (#1635)
Browse files Browse the repository at this point in the history
* added warranty date filter for asset

* added filter test

* used present aware now instead of datetime.now()

* formatted using black

* removed field_name
  • Loading branch information
GokulramGHV authored Sep 26, 2023
1 parent 941fae2 commit 8cdaeb6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class AssetFilter(filters.FilterSet):
method="filter_in_use_by_consultation"
)
is_permanent = filters.BooleanFilter(method="filter_is_permanent")
warranty_amc_end_of_validity = filters.DateFromToRangeFilter()

def filter_in_use_by_consultation(self, queryset, _, value):
if value not in EMPTY_VALUES:
Expand Down
38 changes: 36 additions & 2 deletions care/facility/tests/test_asset_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.utils.timezone import datetime
from django.utils.timezone import now, timedelta
from rest_framework import status
from rest_framework.test import APITestCase

Expand Down Expand Up @@ -119,7 +119,7 @@ def test_asset_filter_in_use_by_consultation(self):
{
"consultation": consultation.external_id,
"bed": bed.external_id,
"start_date": datetime.now().isoformat(),
"start_date": now().isoformat(),
"assets": [asset1.external_id, asset2.external_id],
},
)
Expand All @@ -131,3 +131,37 @@ def test_asset_filter_in_use_by_consultation(self):
response = self.client.get("/api/v1/asset/?in_use_by_consultation=false")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["results"]), 1)

def test_asset_filter_warranty_amc_end_of_validity(self):
asset1 = Asset.objects.create(
name="asset1",
current_location=self.asset_location,
warranty_amc_end_of_validity=now().date(),
)
asset2 = Asset.objects.create(
name="asset2",
current_location=self.asset_location,
warranty_amc_end_of_validity=now().date() + timedelta(days=1),
)

response = self.client.get(
f"/api/v1/asset/?warranty_amc_end_of_validity_before={now().date() + timedelta(days=2)}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(
str(asset1.external_id), [asset["id"] for asset in response.data["results"]]
)
self.assertIn(
str(asset2.external_id), [asset["id"] for asset in response.data["results"]]
)

response = self.client.get(
f"/api/v1/asset/?warranty_amc_end_of_validity_after={now().date() + timedelta(days=1)}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(
str(asset2.external_id), [asset["id"] for asset in response.data["results"]]
)
self.assertNotIn(
str(asset1.external_id), [asset["id"] for asset in response.data["results"]]
)

0 comments on commit 8cdaeb6

Please sign in to comment.