From 242e67e4708cc0ab1d2866f5cf39123a4ae4a758 Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Tue, 5 Nov 2024 20:29:27 +0530 Subject: [PATCH] [fix] Use "time" argument for calculating time in "Chart._get_time" #612 Fixes #612 --- openwisp_monitoring/monitoring/base/models.py | 2 +- openwisp_monitoring/monitoring/tests/test_api.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/openwisp_monitoring/monitoring/base/models.py b/openwisp_monitoring/monitoring/base/models.py index 743025e1d..e538ea8ed 100644 --- a/openwisp_monitoring/monitoring/base/models.py +++ b/openwisp_monitoring/monitoring/base/models.py @@ -729,7 +729,7 @@ def _get_time(cls, time, start_date=None, end_date=None): return start_date if not isinstance(time, str): return str(time) - if time in cls._get_group_map().keys(): + if time in cls._get_group_map(time).keys(): days = int(time.strip('d')) now = timezone.now() if days > 3: diff --git a/openwisp_monitoring/monitoring/tests/test_api.py b/openwisp_monitoring/monitoring/tests/test_api.py index 3c89d07db..d07bdb8ca 100644 --- a/openwisp_monitoring/monitoring/tests/test_api.py +++ b/openwisp_monitoring/monitoring/tests/test_api.py @@ -670,6 +670,11 @@ def test_group_by_time(self): self.assertEqual(response.status_code, 200) self.assertIsInstance(response.data['charts'], list) + with self.subTest('Test with custom valid group time'): + response = self.client.get(path, {'time': '5d'}) + self.assertEqual(response.status_code, 200) + self.assertIsInstance(response.data['charts'], list) + with self.subTest('Test with invalid group time'): response = self.client.get(path, {'time': '3w'}) self.assertEqual(response.status_code, 400)